context.sync - passing variables

The first four examples on this page are to illustrate how you can pass information/variables between multiple promises.
These examples are for illustration purposes only.
These examples pass a single variable through using the return value.
When all the code is inside one Excel.run function, there is actually no need to pass this information between the promises.
The last example shows you how to use function-level variables inside a single Excel.run function to share information between different promises.


Nested Syntax

This writes data to cell "A1", writes data to cell "B1", retrieves the value from cell "A1" and writes data to cell "C1".

Excel.run(function (context) { 
  let _workbook = context.workbook;
  let _sheet = _workbook.worksheets.add();
  let _range1 = _sheet.getRange('A1');
  _range1.values = [[10]];
  return context.sync()
    .then(function () {
      let _range2 = _sheet.getRange('B1');
      _range2.values = [[20]];
      let _rangepass = _sheet.getRange('A1').load("values");
      return context.sync(_rangepass)
        .then(function (_rangepassed) {
          let _number3 = Number(_rangepassed.values) * 3;
          let _range3 = _sheet.getRange('C1');
          _range3.values = [[_number3]];
          return context.sync();
        });
    });
}).catch(function (error) {
  console.error(JSON.stringify(error));
});

Invoke Chaining Syntax

This writes data to cell "A1", writes data to cell "B1", retrieves the value from cell "A1" and writes data to cell "C1".

Excel.run(function (context) { 
  let _workbook = context.workbook;
  let _sheet = _workbook.worksheets.add();
  let _range1 = _sheet.getRange("A1");
  _range1.values = [[10]];
  return context.sync()
    .then(function () {
      let _range2 = _sheet.getRange("B1");
      _range2.values = [[20]];
      let _rangepass = _sheet.getRange('A1').load("values");
      return context.sync(_rangepass);
    })
    .then(function (_rangepassed) {
      let _number3 = Number(_rangepassed.values) * 3;
      let _range3 = _sheet.getRange("C1");
      _range3.values = [[_number3]];
    })
    .then(context.sync);
}).catch(function (error) {
  console.error(JSON.stringify(error));
});

Reference Chaining Syntax

This writes data to cell "A1", writes data to cell "B1", retrieves the value from cell "A1" and writes data to cell "C1".

Excel.run(function (context) { 
  let _workbook = context.workbook;
  let _sheet = _workbook.worksheets.add();
  let _range1 = _sheet.getRange("A1");
  _range1.values = [[10]];
  return context.sync()
    .then(function () {
      let _range2 = _sheet.getRange("B1");
      _range2.values = [[20]];
      let _rangepass = _sheet.getRange('A1').load("values");
    })
    .then(context.sync(_rangepass)
    .then(function (_rangepassed) {
      let _number3 = Number(_rangepassed.values) * 3;
      let _range3 = _sheet.getRange("C1");
      _range3.values = [[_number3]];
    })
    .then(context.sync);
}).catch(function (error) {
  console.error(JSON.stringify(error));
});

Async Await Syntax

This writes data to cell "A1", writes data to cell "B1", retrieves the value from cell "A1" and writes data to cell "C1".

Excel.run(async (context) => { 
  let _workbook = context.workbook;
  let _sheet = _workbook.worksheets.add();
  let _range1 = _sheet.getRange('A1');
  _range1.values = [[10]];
  await context.sync();

  let _range2 = _sheet.getRange('B1');
  _range2.values = [[20]];
  let _rangepass = _sheet.getRange('A1').load("values");
  let _rangepassed = await context.sync(_rangepass);

  let _number3 = Number(_rangepassed.values) * 3;
  let _range3 = _sheet.getRange('C1');
  _range3.values = [[_number3]];
  await context.sync();

}).catch((error) => {
  console.error(JSON.stringify(error));
});

Using Local Variables with Nested Syntax

This writes data to cell "A1", writes data to cell "B1", retrieves the value from cell "A1" and writes data to cell "C1".

Excel.run(async (context) => { 
  let _workbook: Excel.Workbook;
  let _sheet: Excel.Worksheet;
  let _range1: Excel.Range;
  let _range2 : Excel.Range;
  let _range3 : Excel.Range;
  let _rangepass : Excel.Range;
  let _number3: Number;
  _workbook = context.workbook;
  _sheet = _workbook.worksheets.add();
  _range1 = _sheet.getRange('A1');
  _range1.values = [[10]];
  return context.sync()
    .then(function () {
      _range2 = _sheet.getRange('B1');
      _range2.values = [[20]];
      _rangepass = _sheet.getRange('A1').load("values");
      return context.sync()
        .then(function () {
          _number3 = Number(_rangepass.values) * 3;
          _range3 = _sheet.getRange('C1');
          _range3.values = [[_number3]];
          return context.sync();
        });
    });
}).catch((error) => {
  console.error(JSON.stringify(error));
});

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