1

I am trying to create a slide in Google App Script and then add a text box to this newly created slide. The first call is to the Slides api and the second call is made using inbuilt methods. However, because of the asynchronous nature of these two lines, the 2nd is operating before the 1st and causing an obvious error.

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

My question would be: How to create a callback or use a promise to make the needed adjustments?

2 Answers 2

1

I tried a different approach, I grouped all the request in chronological order and it worked:

 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);
Sign up to request clarification or add additional context in comments.

Comments

1

You could simply wait:

Utilities.sleep();

But this requires estimating the api call execution time.

Apps script currently doesn't support promises. Even if it did, the batchUpdate call to the api doesn't return a promise.

3 Comments

Are you sure OP is correct about batchupdate being async? I doubt it. From documentation: In any case, the updates in your request are guaranteed to be applied together atomically.
@Kos The documentation says "All updates in the request" are applied atomically. I'm not sure about async of batchUpdate, but I've seen the same disconnect between built in Sheets class after sheets api request. Apps script is either reading a cached copy of the Sheets class or the atomic operation of api returns data before the actual atomic application of all updates. Either way, I'm sure there's a sync issue.
reading a cached copy - probably this, you right

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.