如何同步对 App Script 中内置的 Slides api 和 Slides class 的异步调用?

How to synchronize asynchronous calls to Slides api and Slides class built in App Script?

我正在尝试在 Google App Script 中创建一张幻灯片,然后向这张新创建的幻灯片添加一个文本框。第一次调用是幻灯片 api,第二次调用是使用内置方法进行的。但是,由于这两行的异步性质,第二行在第一行之前运行并导致明显的错误。

Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
var shape = presentation.getSlideById(newpageId).insertShape(SlidesApp.ShapeType.TEXT_BOX, 0, 0, width, height/4);

我的问题是:如何创建回调或使用承诺进行必要的调整?

我尝试了一种不同的方法,我将所有请求按时间顺序分组并且成功了:

 var requests = [{
    'createSlide': {
      'objectId': newpageId,
      'insertionIndex': current_index,
      'slideLayoutReference': {
        'predefinedLayout': 'BLANK'
      }
    }
  },
  {
    "createShape": {
      "objectId": `silaComment${questionNumber}${token}`,
      "elementProperties": {
        "pageObjectId": newpageId,
        "size": {
          "width": {
            "magnitude": 3000000,
            "unit": "EMU"
          },
          "height": {
            "magnitude": 3000000,
            "unit": "EMU"
          }
        },
        "transform": {
          "scaleX": 0.6807,
          "scaleY": 0.4585,
          "translateX": 6583050,
          "translateY": 1673950,
          "unit": "EMU"
        }
      },
      "shapeType": "WAVE"
    }
  },
  {
    "insertText": {
      "objectId": `silaComment${questionNumber}${token}`,
      "text": comment + '\nasked by ' + name,
      "insertionIndex": 0
    }
  }]; 
Slides.Presentations.batchUpdate({'requests': requests}, presentationId);

您可以稍等:

Utilities.sleep();

但这需要估计 api 调用执行时间。

Apps 脚本目前不支持 promises。即使是这样,对 api 的 batchUpdate 调用也没有 return 承诺。