【问题标题】:Iterating through tables in Excel mac using office js is not working使用 office js 遍历 Excel mac 中的表不起作用
【发布时间】:2016-10-17 08:16:52
【问题描述】:

我正在尝试使用 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",错误位置:"TableCollection.count"

【问题讨论】:

    标签: excel macos ms-office office-js


    【解决方案1】:

    我们试图重现该问题,但没有得到“一般例外”。我们在“PropertyNotLoaded”的第一个示例中遇到了一个错误,尽管从示例稍微不正确的意义上来说这是可以理解的,加载语句应该是tables.load('name, count') 而不是tables.load('items')。但这仍然不能解释错误。第二个样本应该可以正常工作。

    您是在现有文档上运行它吗?那份文件有什么特别之处吗?如果您针对包含几个表的全新文档运行此代码,那么它会工作吗?另外,您的 Office for Mac 版本号是多少?

    【讨论】:

    • 我在 Mac 客户端上遇到了类似的问题。对于新工作簿
    • 对于调用var tables = ctx.workbook.tables; tables.load(); 的新工作簿会引发错误,而不是返回成功,tables.count = 0 和空的 tables.items 集合。错误是{"errorLocation":"TableCollection.count"}。就我而言,当我使用 API 添加表时,即使表存在,错误仍然会发生。解决方法的唯一方法是保存工作簿并重新打开它。然后 tables.load() 代码工作正常。我只在新工作簿上看到了问题。
    • Mac 版本 15.30 (170107)
    • 让我找人试试,然后跟进。
    • 我在 Mac 上也遇到了这个错误,但前提是没有表。如果表存在,则工作正常。在 Windows 上运行良好。 macOS 10.12.5。 Excel 15.36 (170702)。
    猜你喜欢
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 2022-11-19
    • 2018-08-28
    • 2020-03-12
    相关资源
    最近更新 更多