javascript - Excel 任务 Pane - bindingDataChanged 事件永远不会结束

标签 javascript excel task pane

我正在开发 Excel 任务 Pane 应用程序。基本上,它的作用是在用户键入时更改具有无效格式(邮政编码为 r1243)的单元格的背景颜色。我创建了一个绑定(bind)并添加了一个 BindingDataChanged 事件处理程序来监听数据更改。如果有任何更改,请验证格式并更改背景颜色。代码如下。

这些代码可以正常工作,但问题是,即使数据没有任何更改,onBindingDataChanges 似乎也永远不会结束。可能出什么问题了?

function listenChanges() {
    Office.select("bindings#customerTableBinding").addHandlerAsync(Office.EventType.BindingDataChanged, onBindingDataChanged, function (asyncResult) {
        if (asyncResult.status === "failed") {
            app.showNotification('Error: ' + asyncResult.error.message);
        } else {
            app.showNotification('New event handler added for binding.');
        }
    });
}

function onBindingDataChanged(eventArgs) {
    eventArgs.binding.getDataAsync({coercionType: 'table'},
     function (asyncResult) {
         if (asyncResult.status == 'succeeded') {
             var table = new Office.TableData();
             table.headers = asyncResult.value.headers;
             table.rows = asyncResult.value.rows;

             var column = getColumns(table);
             var rows = getRows(table, column);

             eventArgs.binding.setFormatsAsync([{ cells: Office.Table.Data, format: { backgroundColor: "white" } }], function (asyncResult) { });

             for (var i = 0; i < rows.length; i++) {
                 eventArgs.binding.setFormatsAsync([{ cells: { row: rows[i], column: column }, format: { backgroundColor: "red", fontStyle: "bold" } }], function (asyncResult) { });
             }
         }
         else
             app.showNotification("The error message is " + asyncResult.error.message);
     });
}

function getColumns(table) {
    var column;
    for (var i = 0; i < table.headers.length; i++) {
        for (var j = 0; j < table.headers[i].length; j++) {
            if (table.headers[i][j] == "PostalCode") {
                column = j;
            }
        }
    }
    return column;
}

function getRows(table, column) {
    var rows = new Array();
    for (var i = 0; i < table.rows.length; i++) {
        if (!isValidUSZip(table.rows[i][column])) {
            rows.push(i);
        }
    }
    return rows;
}

function isValidUSZip(zip) {
    return /^\d{5}(-\d{4})?$/.test(zip);
}

最佳答案

eventArgs.binding.setFormatsAsync([{ cells: Office.Table.Data, format: { backgroundColor: "white"} }], function (asyncResult) { });

每次更改事件触发时,您都将背景设置为白色。这将触发更改事件再次触发(导致无限循环)。要解决此问题,您可以在触发数据事件操作时删除处理程序,然后在更新后将其添加回来。

function addHandlerChanged() {
    if (selfBinding) {
        selfBinding.addHandlerAsync(
           Office.EventType.BindingDataChanged, dataChangeEvent);
    }
}

function removeHandlerChanged() {
    if (selfBinding) {
        selfBinding.removeHandlerAsync(
           Office.EventType.BindingDataChanged, { handler: dataChangeEvent }, function (result) {
           });
    }
};

function dataChangeEvent(eventArgs) {    
  //remove handler
  removeHandlerChanged();

   // do you're table format update here

   //then add the handler back 

   addHandlerChanged();
}

关于javascript - Excel 任务 Pane - bindingDataChanged 事件永远不会结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31751728/

相关文章:

vba - 格式化函数错误

c# - How to split worksheets into separate workbooks using c#//如何使用EPPLus复制整个工作表

javascript - 使用 Angular 获取 Excel 文档并保存到磁盘

c# - SQLite 异步任务和 Wait 运算符 - 任务会以正确的顺序执行吗?

android - 如何知道 Android 任务的可见性变化?

javascript - 我无法在 .then() Promise 中返回对象

javascript - 让 JSON 与 Google Maps API 3 一起工作

c# - 什么都不做的异步方法

javascript - 在javascript中获取没有0索引的日期时间

javascript - ASP.NET MVC 和 Angular : After session expires, Angular Factory 返回登录页面的 HTML 而不是 JSON 对象