如何使用 Excel JavaScript 加载项访问 WorkbookCreated 对象?
How to access WorkbookCreated object with Excel JavaScript Add-In?
Excel JS 加载项 Api 文档 (Excel.WorkbookCreated) 说:
The WorkbookCreated object is the top level object created by Application.CreateWorkbook.
A WorkbookCreated object is a special Workbook object.
这让我相信一个WorkbookCreated对象是用来访问一个新创建的工作簿的,但是没有关于在创建一个新工作簿后访问WorkbookCreated对象的解释或例子。
我能够使用以下代码成功创建一个新工作簿:
// dbg() is a function to output text to the html window within the excel plugin.
function makeWorkbook() {
Excel.run(async context => {
Excel.createWorkbook().then(() => {
dbg('created');
});
return context.sync().then(() => {
dbg('sync completed');
});
});
}
此功能后我只能访问新创建的工作簿运行,新工作簿由用户手动选择然后另一个命令运行。
有没有办法在没有用户干预的情况下在上面的 makeWorkbook() 函数中访问新创建的工作簿?
恐怕我们用于生成 API 参考文档的系统会将 createWorkbook
方法放在一个很长的页面的末尾附近。 This link 会直接跳转到它。从示例中可以看出,您需要使用 JavaScript FileReader 对象。
更新:Create a workbook 上有更多官方文档。关键句是"When this method is called, the new workbook is immediately opened and displayed in a new instance of Excel. Your add-in remains open and running with the previous workbook."
加载项基本上存在于沙盒 JavaScript 引擎中,该引擎嵌入在打开加载项的特定 Excel 实例中。加载项无法访问 Excel 的另一个实例,就像出于安全原因它无法访问您计算机上的任何其他进程一样。因此,无法从使用 Excel.createWorkbook
.
创建它的加载项中访问新创建的工作簿
Excel JS 加载项 Api 文档 (Excel.WorkbookCreated) 说:
The WorkbookCreated object is the top level object created by Application.CreateWorkbook.
A WorkbookCreated object is a special Workbook object.
这让我相信一个WorkbookCreated对象是用来访问一个新创建的工作簿的,但是没有关于在创建一个新工作簿后访问WorkbookCreated对象的解释或例子。
我能够使用以下代码成功创建一个新工作簿:
// dbg() is a function to output text to the html window within the excel plugin.
function makeWorkbook() {
Excel.run(async context => {
Excel.createWorkbook().then(() => {
dbg('created');
});
return context.sync().then(() => {
dbg('sync completed');
});
});
}
此功能后我只能访问新创建的工作簿运行,新工作簿由用户手动选择然后另一个命令运行。
有没有办法在没有用户干预的情况下在上面的 makeWorkbook() 函数中访问新创建的工作簿?
恐怕我们用于生成 API 参考文档的系统会将 createWorkbook
方法放在一个很长的页面的末尾附近。 This link 会直接跳转到它。从示例中可以看出,您需要使用 JavaScript FileReader 对象。
更新:Create a workbook 上有更多官方文档。关键句是"When this method is called, the new workbook is immediately opened and displayed in a new instance of Excel. Your add-in remains open and running with the previous workbook."
加载项基本上存在于沙盒 JavaScript 引擎中,该引擎嵌入在打开加载项的特定 Excel 实例中。加载项无法访问 Excel 的另一个实例,就像出于安全原因它无法访问您计算机上的任何其他进程一样。因此,无法从使用 Excel.createWorkbook
.