Chain Promises in Office.js with Excel.run ()

Im working with new office.js. I use the Excel.run function, which returns a promise. I have a question about the promises pattern implemented by the library.

All samples show this pattern.

Excel.run( function (ctx) {

  //set up something

  return ctx.sync().then (function () {
    //call another function somewhere to chain operations
  });

}).then ( function () {
  //do something else if you want
}).catch (function (error) {
  handle errors
});

The problem is ctx.sync (). then () is contained in Excel.run () The way it is presented, you cannot use the promises chain according to the promises specification, because you lose the context object if you try to process then () outside of Excel.run () Thus, the pattern seems to facilitate nested function calls, which should eliminate promises.

What I want to do is a sequence of several calls together by chaining as follows:

Excel.run( function (ctx) {
  return ctx.sync();
}).then ( function (ctx) {
  return ctx.sync();
}).then ( function (ctx) {
  return ctx.sync();
}).then ( function (ctx) {
  return ctx.sync();
}).catch (function (error) {

});

Is it possible?

+4
2

, Excel.run OM ​​. Excel.run , , .

, , . , Excel "" ".context". , , :

Excel.run(function (ctx) {
    var worksheet = ctx.workbook.worksheets.getActiveWorksheet();
    return ctx.sync(worksheet);
}).then(function(worksheet) {
    worksheet.name = "Test"
    return worksheet.context.sync();
}).catch(function(e) {
    console.log(e)  
});

, Excel.run, .

- Range, . , , ( ?) , , ). Excel.run Backing Range, Excel. Excel.run , . , :

Excel.run(function (ctx) {
    var range = ctx.workbook.getSelectedRange();
    return ctx.sync(range);
}).then(function(range) {
    range.format.fill.color = "red";
    return ctx.sync();
}).catch(function(e) {
    console.log(e)  
})

"InvalidObjectPath".

, ctx.trackedObjects. , , - , , , . , - Excel.

var range;
Excel.run(function (ctx) {
    range = ctx.workbook.getSelectedRange();
    ctx.trackedObjects.add(range);
    return ctx.sync(range);
}).then(function(range) {
    range.format.fill.color = "red";
    return range.context.sync();
}).then(function() {
    // Attempt to clean up any orphaned references
    range.context.trackedObjects.remove(range);
    range.context.sync(); // don't need to await it, since it just the final cleanup call
}).catch(function(e) {
    console.log(e);
})

: , , , Excel.run. , "". , Excel.run(, promises Excel.run, - ). , , , , (, ), onclick .. Excel.run, .

PS. , : , ctx.sync() Excel.run, - . promises .. ,

Excel.run(function (ctx) {
    var range = ctx.workbook.worksheets.getActiveWorksheet().getRange("A1:C3");
    range.load("values");
    return ctx.sync()
        .then(function () {
            // Some set of actions against the OM, now that the "values"
            // property has been loaded and can be read from the "range" object.
        })
        .then(ctx.sync)
        .then(function () {
            // Another set of actions against the OM, presumably after doing
            // another load-requiring operation (otherwise could have
            // been part of the same .then as above)
        })
        .then(ctx.sync)
        .then(function() {
            // One final set of actions
        });     
}).catch(function(error) {
    console.log("Error: " + error);
});
+9

, Excel.RequestContext.sync , Excel.run , . promises, Excel.run, trackedObjects , , Excel.Run.

Excel.run, , RequestContext.

+1

Source: https://habr.com/ru/post/1609021/


All Articles