Office.js加载项无法使用动态列

时间:2018-03-01 19:31:08

标签: javascript excel knockout.js office-js

我正在尝试使用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);
    });
}

1 个答案:

答案 0 :(得分:1)

当你等待更好的答案时,有几件事情:

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

  2. 尽管如此,您应该尝试尽可能多地使用特定于主机的API。一位同事建议Range.getCell()和Range.getResizedRange()方法可能适合您的场景。特别是,如果你链接它们:ctx.sync

  3. 我对最后连续4次public class ListCaseNotesTest extends CamelSpringTestSupport { @Override protected AbstractApplicationContext createApplicationContext() { AnnotationConfigApplicationContext acc = new AnnotationConfigApplicationContext(); acc.register(CamelController.class); return acc; } @Autowired VisitsController visitsController; @Test public void testListCaseNotes() throws Exception{ ListNotesKey listNotesKey = new ListNotesKey(); listNotesKey.setUsername("caseworker"); listNotesKey.setRelatedIDs("[\"1000\",\"1001\",\"1002\"]"); listNotesKey.setRelatedType(ListNotesKey.RelatedTypeEnum.CASE); // invoke camel String response = visitsController.sendMessage("listCaseNotes", SOR_CONFIG, payload); 来电感到困惑。