context.sync - Multiple

If you do not want to nest the 'context.sync' you can use chaining instead.
There are two different ways you can layout out your code when you use chaining.
The first layout we are calling Invoke and the other layout we are calling Reference.
These layouts are useful when you are calling 'context.sync' multiple times, in this case three times.


Invoke Chaining

When you use this layout you must always return 'context.sync()' out of the batch function.
The body of each '.then' must either be fully synchronous or return a promise.

Excel.run(function (context) { 

   return context.sync()
       .then(function() {
           return context.sync()
       })
       .then(function() {
           return context.sync()
       });

}).catch(function(error) {
    console.error(JSON.stringify(error)); } );
});

Passing Variables
There are a couple of options:
(1) You can declare your variables at the very top above the first 'context.sync()'
(2) Pass individual variables through using the return value

Excel.run(function (context) { 

   return context.sync()
       .then(function() {
            var _rangePassed = _sheet.getRange('A1:B2');
            return context.sync(_rangePassed)
       })
       .then(function(_rangePassed) {
            if (_rangePassed.values[0][0] = "text") {
            }
       })
       .then(context.sync);

}).catch(function(error) {
    console.error(JSON.stringify(error)); } );
});

(3) Pass multiple variables through (as an object) using the return value

Excel.run(function (context) { 

   return context.sync()
       .then(function() {
            var _rangePassed1 = _sheet.getRange('A1:B2');
            var _rangePassed2 = _sheet.getRange('C1:D2');

            return context.sync({_rangePassed1, _rangePassed2})
       })
       .then(function(objectPassed) {
            if (objectPassed._rangePassed1.values[0][0] = objectPassed._rangePassed2.values[0][0]) {
            }
       })
       .then(context.sync);

}).catch(function(error) {
    console.error(JSON.stringify(error)); } );
});


Reference Chaining

There is only one return statement when you use this layout.
When you use this layout it is easy to quickly see how many times you are calling 'context.sync'.

Excel.run(function (context) { 

   return context.sync()
       .then(function() {
       })
       .then(context.sync)
       .then(function() {
       })
       .then(context.sync); // optional but should never be omitted

}).catch(function(error) {
    console.error(JSON.stringify(error)); } );
});

Passing Variables
There are a couple of options:
(1) You can declare your variables at the very top above the first 'context.sync()'
(2) Pass individual variables through using the return value

Excel.run(function (context) { 

   return context.sync()
       .then(function() {
            var _rangePassed = _sheet.getRange('A1:B2');
            return _rangePassed;
       })
       .then(context.sync)
       .then(function(_rangePassed) {
            if (_rangePassed.values[0][0] = "text") {
            }
       })
       .then(context.sync);

}).catch(function(error) {
    console.error(JSON.stringify(error)); } );
});

(3) Pass multiple variables through (as an object) using the return value

Excel.run(function (context) { 

   return context.sync()
       .then(function() {
            var _rangePassed1 = _sheet.getRange('A1:B2');
            var _rangePassed2 = _sheet.getRange('C1:D2');

            return {_rangePassed1, _rangePassed2};
       })
       .then(context.sync)
       .then(function(objectPassed) {
            if (objectPassed._rangePassed1.values[0][0] = objectPassed._rangePassed2.values[0][0]) {
            }
       })
       .then(context.sync);

}).catch(function(error) {
    console.error(JSON.stringify(error)); } );
});


© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited TopPrevNext