简体   繁体   English

Office-js Excel插件:何时返回context.sync()

[英]Office-js Excel addin : when to return context.sync()

I have trouble to understand when to use context.sync() . 我很难理解何时使用context.sync()

Here is a basic example but which resumes my lack of understanding: 这是一个基本示例,但又恢复了我的理解:

Excel.run(function (context){
    const wb = context.workbook;
    const ws = wb.worksheets.getActiveWorksheet();

    // should never happened, but is it correct to check like this ?
    if (wb === null) {
        // IS IT CORRECT TO DO THIS ?
        // I just want to exit the function
        // return; would be enough ? What's going on in the callstack?
        return context.sync();
    }
    ws.load("name");
    return context.sync().then(function() {
        var name = wb.name;
        // do stuff
        var range = ws.getRangeByIndexes(1,1,10,10);
        return context.sync().then(function() {
             ws.names.add("NEWRANGE", range);
             // mandatory return context.sync() to refresh Excel object ?
             // doesn't work otherwise for me
             return context.sync();
        });
    }
}).catch(function(error) {
  // do stuff
}

If somebody could explain, it would be more than welcome :) 如果有人可以解释,那就不胜感激了:)

Cheers. 干杯。

I think it'll help if you think of these objects as proxy objects. 我认为将这些对象视为代理对象会有所帮助。 They are only a representation of the real object and not all properties will be available on the proxy object because they don't need to be available. 它们只是真实对象的表示,并非所有属性都可以在代理对象上使用,因为它们不需要可用。 Similarly, changes made to the proxy object won't update the real object. 同样,对代理对象所做的更改不会更新实际对象。 context.sync() is used to sync the proxy objects with the real objects. context.sync()用于将代理对象与实际对象同步。

Looking at your code, the first context.sync() is unnecessary because you don't need to retrieve anything or make any changes. 查看您的代码,不需要第一个context.sync(),因为您无需检索任何内容或进行任何更改。 Actually the entire condition if (wb === null) is unnecessary because context.workbook cannot be null. 实际上, if (wb === null)不需要整个条件if (wb === null)因为context.workbook不能为null。

As soon as you try to ws.load("name"); 一旦您尝试ws.load("name"); , you need a context.sync() because you've tried to access a property on the proxy object that needs to be loaded from the real object. ,则需要一个context.sync(),因为您试图访问代理对象上的一个属性,该属性需要从真实对象中加载。

When you call var range = ws.getRangeByIndexes(1,1,10,10); 当您调用var range = ws.getRangeByIndexes(1,1,10,10); , you don't need a context.sync() because you're just grabbing another proxy object but no changes have been made and no properties are accessed. ,则不需要context.sync(),因为您只是在抓取另一个代理对象,但未进行任何更改,也没有访问任何属性。

But since ws.names.add("NEWRANGE", range); 但是由于ws.names.add("NEWRANGE", range); is a real change, you'll need a context.sync() to reflect the change on the real object. 是真正的更改,您需要一个context.sync()才能将更改反映到实际对象上。 Technically, the last context.sync() is not necessary because Excel.run will actually call context.sync() after running everything inside the Excel.run(). 从技术上讲,最后一个context.sync()是不必要的,因为在运行Excel.run()内的所有内容后,Excel.run实际上会调用context.sync()。 That said, it's good practice to have an ending context.sync() anyway. 话虽如此,还是有一个结尾context.sync()是一个好习惯。

You can also batch independent operations in one context.sync(). 您也可以在一个context.sync()中批处理独立的操作。 Since var range = ws.getRangeByIndexes(1,1,10,10); 由于var range = ws.getRangeByIndexes(1,1,10,10); has nothing to do with ws.names.add("NEWRANGE", range); ws.names.add("NEWRANGE", range); , you can actually put them behind a single context.sync(). ,您实际上可以将它们放在单个context.sync()之后。

I'd also suggest switching to using TypeScript to keep your code cleaner and easier to understand. 我还建议切换到使用TypeScript,以使您的代码更简洁,更易于理解。 Try using ScriptLab in Excel. 尝试在Excel中使用ScriptLab。 There are a lot of samples that should help you understand context.sync() and office-js in general. 一般而言,有许多示例可以帮助您理解context.sync()和office-js。

Lastly, here's the code that you could have written to do the same thing. 最后,这是您可能编写的用于完成相同操作的代码。

Excel.run(function (context) {
    const wb = context.workbook;
    const ws = wb.worksheets.getActiveWorksheet();

    ws.load("name");
    return context.sync().then(function () {
        var name = wb.name;
        // do stuff
        var range = ws.getRangeByIndexes(1, 1, 10, 10);
        ws.names.add("NEWRANGE", range);
        return context.sync();
    });
}).catch(function (error) {
    // do stuff
});

Oh and you should take a look at Michael's book as Cindy suggested. 哦,您应该看看辛迪建议的迈克尔的书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM