我正在尝试使用office js在Excel中迭代表
下面是代码示例(与这里的文档中的代码相同https://github.com/OfficeDev/office-js-docs/blob/master/reference/excel/tablecollection.md):
Excel.run(function (ctx) {
var tables = ctx.workbook.tables;
tables.load('items');
return ctx.sync().then(function() {
console.log("tables Count: " + tables.count);
for (var i = 0; i < tables.items.length; i++)
{
console.log(tables.items[i].name);
}
});
}).catch(function(error) {
console.log("Error: " + error);
if (error instanceof OfficeExtension.Error) {
console.log("Debug info: " + JSON.stringify(error.debugInfo));
}
});
获取表的个数
Excel.run(function (ctx) {
var tables = ctx.workbook.tables;
tables.load('count');
return ctx.sync().then(function() {
console.log(tables.count);
});
}).catch(function(error) {
console.log("Error: " + error);
if (error instanceof OfficeExtension.Error) {
console.log("Debug info: " + JSON.stringify(error.debugInfo));
}
});
代码在windows Excel 2016上正常工作,但在Mac Excel 2016上,两个代码示例都会抛出错误类型:"General Exception",错误位置:" tablecollecon .count"
我们试图重现这个问题,但没有得到"一般例外"。我们在"PropertyNotLoaded"的第一个例子中得到一个错误,尽管这在样本稍微不正确的意义上是可以理解的,加载语句应该是tables.load('name, count')
而不是tables.load('items')
。但这仍然不能解释这个错误。第二个示例应该可以正常工作。
您是否在现有文档上运行此操作?那份文件有什么特别之处吗?如果在一个包含几个表的全新文档上运行这段代码,它能正常工作吗?还有,你的Mac版Office的版本号是多少?