context.sync
The more calls you make to this method the slower your code will run.
You should only call this method when you absolutely have to.
Updating the Office Object
The sync() method available on the request context synchronizes the state between the JavaScript proxy objects and real objects in Office application.
This method can be used to execute the instructions that have been added to the queue.
This method can be used to retrieve properties from the loaded Office objects.
This method returns a promise, which is resolved when synchronization is complete.
You call a context.sync to indicate the completion of interacting with the Office application.
You call a context.sync when you are waiting to get the result back from an object.load
Helper functions never call context.sync
One context.sync
Write data to a worksheet
Excel.run(function(context) {
var _workbook = context.workbook;
var _sheet = _workbook.worksheets.add();
var _values = [['Header1', 100], ['Header2', 200]];
var _range = _sheet.getRange('A1:B2');
_range.values = _values;
return context.sync();
}).catch(function(error) {
console.error(JSON.stringify(error)); } );
});
Two context.sync - Nesting
Write data to a worksheet
Multiply the numbers by 2 and write this data to the worksheet
Excel.run(function (context) {
var _workbook = context.workbook;
var _sheet = _workbook.worksheets.add();
var _values = [['Header1', 100], ['Header2', 200]];
var _range = _sheet.getRange('A1:B2');
_range.values = _values;
return context.sync()
.then(function() {
_values[0][1] = _values[0][1] * 2;
_values[1][1] = _values[1][1] * 2;
var _range3 = _sheet.getRange('C1:D2');
_range3.values = _values;
return context.sync() //optional but should never be omitted
});
}).catch(function(error) {
console.error(JSON.stringify(error)); } );
});
Three context.sync - Nesting
Excel.run(function (context) {
return context.sync()
.then(function() {
return context.sync()
.then(function() {
return context.sync() // optional but should never be omitted
})
});
}).catch(function(error) {
console.error(JSON.stringify(error)); } );
});
© 2020 Better Solutions Limited. All Rights Reserved. © 2020 Better Solutions Limited TopPrevNext