如何在Javascript中连接Excel事件

时间:2022-07-28 21:06:28

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

在客户端Web应用程序中,我想:

  1. open an Excel spreadsheet,
  2. 打开Excel电子表格,
  3. export some application data to Excel,
  4. 将一些应用程序数据导出到Excel,
  5. allow the user to work with it, and
  6. 允许用户使用它,并且
  7. when they are done, read the (potentially changed) data back into my application.
  8. 完成后,将(可能已更改的)数据读回我的应用程序。

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.

我希望用户能够获得流畅的体验,并通过连接到BeforeClose事件来检测他们何时完成excel,但我发现我无法在javascript / HTML中连接到Excel的事件。

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?

有没有人有什么建议?

3 个解决方案

#1


1  

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.

在做了一些研究后,我发现我无法在javascript中将事件连接到动态ActiveX对象(即由新的ActiveXObject构造函数创建的对象)。

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.

一个想法是我创建一个包装Windows窗体用户控件,该控件将托管在Web应用程序的标记内。用户控件将调用Excel并接收事件,并将事件提升回javascript,我可以使用

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.

即使它确实有效,我对这个解决方案也不是特别满意。有太多层 - 从Silverlight控件调用javascript意味着我的数据必须跨越3个边界并返回:Silverlight - > Javascript - >托管Winform用户控件 - > Excel。

It would be nice to eliminate some of these boundaries.

消除这些边界是很好的。

#2


0  

I think also your second approach using a Windows From control hosted in IE will not work.

我认为使用IE中托管的Windows From控件的第二种方法也行不通。

IE behaves different as a scripting host. There are certain limitations as a blog post by Eric Lippert mentions:

IE作为脚本主机表现不同。 Eric Lippert在博客文章中提到了一些限制:

Implementing Event Handling, Part Two

实现事件处理,第二部分

#3


-1  

I don't believe this is possible. The reason being, when you call the following code:

我不相信这是可能的。原因是,当您调用以下代码时:

var excel = new ActiveXObject("Excel.Application");

You're actually opening up Excel. So with the following line:

你实际上是在打开Excel。所以使用以下行:

workbook.BeforeClose = BeforeCloseEventHandler;

Its like you're telling the Excel application to run Javascript, which isn't possible. I've tried researching alternatives, like creating an event object, defining the code behind it, then assigning it to workbook.BeforeClose, but I would run into the same problem: Excel events can't be detected by javascript. Mainly because it runs as a seperate process.

就像你告诉Excel应用程序运行Javascript一样,这是不可能的。我已经尝试过研究替代方案,比如创建一个事件对象,定义它背后的代码,然后将它分配给workbook.BeforeClose,但我会遇到同样的问题:javascript无法检测到Excel事件。主要是因为它作为一个单独的过程运行。

So here's some more alternatives you may consider:

所以这里有一些你可以考虑的替代品:

  1. Save the Excel data on the user's computer, then when the user loses excel, have them click somewhere that calls your 1st method, which reads that file and displays it.
  2. 将Excel数据保存在用户的计算机上,然后当用户丢失excel时,让他们单击调用第一个方法的某个位置,该方法读取该文件并显示它。
  3. Read the data from the excel file and then display it.
  4. 从excel文件中读取数据,然后显示它。
  5. Don't close your excel object (this will probably leave excel open as a process on your computer), and have a timer event in javascript. Every 5 seconds, check if Excel is still open, and if it is not open, read the file and display it.
  6. 不要关闭你的excel对象(这可能会在你的计算机上将excel作为一个进程打开),并在javascript中有一个计时器事件。每隔5秒,检查Excel是否仍处于打开状态,如果未打开,请阅读该文件并显示。

Sorry I couldn't be anymore help, and I'm not too sure if any of those alternatives would work, but good luck!

对不起我不能再帮忙了,我不太确定这些替代方案是否有用,但祝你好运!

#1


1  

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.

在做了一些研究后,我发现我无法在javascript中将事件连接到动态ActiveX对象(即由新的ActiveXObject构造函数创建的对象)。

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.

一个想法是我创建一个包装Windows窗体用户控件,该控件将托管在Web应用程序的标记内。用户控件将调用Excel并接收事件,并将事件提升回javascript,我可以使用

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.

即使它确实有效,我对这个解决方案也不是特别满意。有太多层 - 从Silverlight控件调用javascript意味着我的数据必须跨越3个边界并返回:Silverlight - > Javascript - >托管Winform用户控件 - > Excel。

It would be nice to eliminate some of these boundaries.

消除这些边界是很好的。

#2


0  

I think also your second approach using a Windows From control hosted in IE will not work.

我认为使用IE中托管的Windows From控件的第二种方法也行不通。

IE behaves different as a scripting host. There are certain limitations as a blog post by Eric Lippert mentions:

IE作为脚本主机表现不同。 Eric Lippert在博客文章中提到了一些限制:

Implementing Event Handling, Part Two

实现事件处理,第二部分

#3


-1  

I don't believe this is possible. The reason being, when you call the following code:

我不相信这是可能的。原因是,当您调用以下代码时:

var excel = new ActiveXObject("Excel.Application");

You're actually opening up Excel. So with the following line:

你实际上是在打开Excel。所以使用以下行:

workbook.BeforeClose = BeforeCloseEventHandler;

Its like you're telling the Excel application to run Javascript, which isn't possible. I've tried researching alternatives, like creating an event object, defining the code behind it, then assigning it to workbook.BeforeClose, but I would run into the same problem: Excel events can't be detected by javascript. Mainly because it runs as a seperate process.

就像你告诉Excel应用程序运行Javascript一样,这是不可能的。我已经尝试过研究替代方案,比如创建一个事件对象,定义它背后的代码,然后将它分配给workbook.BeforeClose,但我会遇到同样的问题:javascript无法检测到Excel事件。主要是因为它作为一个单独的过程运行。

So here's some more alternatives you may consider:

所以这里有一些你可以考虑的替代品:

  1. Save the Excel data on the user's computer, then when the user loses excel, have them click somewhere that calls your 1st method, which reads that file and displays it.
  2. 将Excel数据保存在用户的计算机上,然后当用户丢失excel时,让他们单击调用第一个方法的某个位置,该方法读取该文件并显示它。
  3. Read the data from the excel file and then display it.
  4. 从excel文件中读取数据,然后显示它。
  5. Don't close your excel object (this will probably leave excel open as a process on your computer), and have a timer event in javascript. Every 5 seconds, check if Excel is still open, and if it is not open, read the file and display it.
  6. 不要关闭你的excel对象(这可能会在你的计算机上将excel作为一个进程打开),并在javascript中有一个计时器事件。每隔5秒,检查Excel是否仍处于打开状态,如果未打开,请阅读该文件并显示。

Sorry I couldn't be anymore help, and I'm not too sure if any of those alternatives would work, but good luck!

对不起我不能再帮忙了,我不太确定这些替代方案是否有用,但祝你好运!