【问题标题】:Office.js Add-in not working with dynamic columnsOffice.js 加载项不适用于动态列
【发布时间】:2018-03-02 06:30:35
【问题描述】:

我正在尝试使用 Office.js 将数据绑定到 Excel 电子表格,但是这些列是一个日期范围,每个项目可能会有所不同。下面是我用来处理静态列的代码,它非常适合。但是,随着列范围的变化,我收到此错误:提供的数据对象与当前选择的大小不匹配。包含动态列的属性是 self.VisibleBudgetDownloadColumns()。我尝试了几种不同的方法,但是关于如何处理这个问题的例子有限。我本质上是在寻找一种更新列和数据的方法。提前致谢!

Excel.run(function (ctx) {
        var activeWorksheet = ctx.workbook.worksheets.getActiveWorksheet();
        var currentRows = GetRows(self.BudgetDownload(), self.BudgetDownload().length, self.VisibleBudgetDownloadColumns(), self.VisibleBudgetDownloadColumns().length);

        return ctx.sync()
            .then(function () {
                Office.context.document.bindings.getByIdAsync(bindingID,
                    function (asyncResult) {
                        // if binding exists, delete and update rows
                        if (asyncResult.status == Office.AsyncResultStatus.Succeeded) {
                            asyncResult.value.deleteAllDataValuesAsync();
                            asyncResult.value.addRowsAsync(currentRows,
                                function (asyncResult) {
                                    if (asyncResult.status === Office.AsyncResultStatus.Failed) {
                                        self.showErrorMessageBar(asyncResult.error.message);
                                    }
                                }
                            );
                        }
                        else { // create new binding
                            var td = new Office.TableData();
                            td.rows = currentRows;
                            td.headers = ko.utils.arrayMap(self.VisibleBudgetDownloadColumns(),
                                function (item) {
                                    return item.DisplayName;
                                });

                            Office.context.document.setSelectedDataAsync(td,
                                function (result) {
                                    if (result.status === Office.AsyncResultStatus.Failed) {
                                        self.showErrorMessageBar(result.error.message);
                                    }
                                    else {
                                        Office.context.document.bindings.addFromSelectionAsync(Office.BindingType.Table, { id: bindingID },
                                            function (asyncResult) {
                                                if (asyncResult.status === Office.AsyncResultStatus.Failed) {
                                                    self.showErrorMessageBar(asyncResult.error.message);
                                                }
                                                else {
                                                    asyncResult.value.setDataAsync(td, { coercionType: Office.CoercionType.Table },
                                                        function (result) {
                                                            if (result.status === Office.AsyncResultStatus.Failed) {
                                                                self.showErrorMessageBar(result.error.message);
                                                            }
                                                        }
                                                    );
                                                }
                                            }
                                        );
                                    }
                                }
                            );
                        }
                    }
                );
            });
    }).catch(function (error) {
        overlay.hide();
        self.showErrorMessageBar(error);
    });
}

【问题讨论】:

    标签: javascript excel knockout.js office-js


    【解决方案1】:

    在您等待更好的答案时有几件事:

    1. 您的外部结构是来自特定于主机的 Excel.js API 的 Excel.run,但其中的大部分逻辑来自共享(也称为通用)API。当您需要同时使用这两组 API 时,对 Shared API 的调用应该包装在一个 Promise-returning 函数中。例如,请参阅此文件中的 getDocumentFilePath 函数:Home.js in Sample 并在同一文件中向上滚动以查看其调用方式。

    2. 也就是说,您应该尝试使用特定于主机的 API 尽可能多地完成工作。一位同事建议 Range.getCell() 和 Range.getResizedRange() 方法可能适合您的场景。特别是,如果您将它们链接起来:getCell(0,0).getResizedRange(…)

    3. 我对最后连续 4 个ctx.sync 调用感到困惑。

    【讨论】:

    • 谢谢瑞克。我对上面的代码进行了一些编辑,并将共享 API 包装在一个 Promise 中。现在还从底部删除了额外的 ctx.sync 调用。
    猜你喜欢
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 2018-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多