Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I hook up to Excel events in Javascript

In a client-side web application, I would like to:

  1. open an Excel spreadsheet,
  2. export some application data to Excel,
  3. allow the user to work with it, and
  4. when they are done, read the (potentially changed) data back into my application.

I would like the user to have a fluid experience and detect when they are done with excel by hooking up to the BeforeClose event, but I find that I am unable to hook up to Excel's events in javascript/HTML.

function BeforeCloseEventHandler(cancel) {
    // TODO: read values from spreadsheet
    alert("Closing...");
}

function openExcel() {
    var excel = new ActiveXObject("Excel.Application");
    var workbook = excel.Workbooks.Add();
    var worksheet = workbook.Worksheets(1);
    worksheet.Cells(1, 1).Value = "First Cell";
    worksheet.Cells(1, 2).Value = "Second Cell";
    workbook.BeforeClose = BeforeCloseEventHandler;  // THIS DOESN'T WORK
    excel.Visible = true;
    excel.UserControl = true;
}

Does anyone have any suggestions?

like image 365
user53564 Avatar asked Jan 12 '09 19:01

user53564


People also ask

Can you automate Excel with JavaScript?

JADE gives you the power to use JavaScript to automate everything within the workbook scope of Excel without making it macro enabled. Because the JavaScript you write is living in a browser, it does not have access to any operating system features on the host computer.

Does Excel support JavaScript?

An Excel add-in interacts with objects in Excel by using the Office JavaScript API, which includes two JavaScript object models: Excel JavaScript API: Introduced with Office 2016, the Excel JavaScript API provides strongly-typed objects that you can use to access worksheets, ranges, tables, charts, and more.

How do I open an Excel file in JavaScript?

To get more information about the data stored inside the excelData variable related to the excel file, you can print the variable using the console. log() function. Copy const XLSX = require('xlsx') ; const parseExcel = (filename) => { const excelData = XLSX. readFile(filename); return Object.


1 Answers

After doing some research, I have discovered that I cannot hook up events to dynamic ActiveX objects (i.e., the ones that are created by the new ActiveXObject constructor) in javascript.

One idea is that I create a wrapper Windows Form user control that would be hosted inside of an <object> tag in the web app. The user control would call Excel and receive events, and raise events back to javascript, which I could hook up to using the <script for="..." event="..."> mechanism. Not sure that this will work, but I will try it.

Even if it does work, I am not particularly happy about this solution. There are too many layers--the javascript is being called from a silverlight control meaning that my data has to cross 3 boundaries there and back: Silverlight -> Javascript -> Hosted Winform User Control -> Excel.

It would be nice to eliminate some of these boundaries.

like image 94
user53564 Avatar answered Sep 22 '22 00:09

user53564