Home > Enterprise >  batchUpdate method throws errors while updating Google Slides
batchUpdate method throws errors while updating Google Slides

Time:12-26

I am trying to create a presentation and update it on Google Apps Scripts. The creation is successful. However when I try to update the title or add a new shape or text it throws errors. Is there any other update method? Also is it possible to update the presentation after modifying the texts without updating all of the presentation? I don't want to create an add-on I just want to be able to update the slides with executing the scripts.


Code:

function createAndUpdatePresentation() {
    const createdPresentation = Slides.Presentations.create({"title": "MyNewPresentation"});
    const link = `https://docs.google.com/presentation/d/${createdPresentation.presentationId}/edit`;
    Logger.log(`Created presentation is on: ${link}`);

    const request = {
      requests: [
        {
          updateTitle: {
            title: 'My Updated Presentation'
          }
        }
      ]
    };

    const updatedPresentation = 
      Slides.Presentations.batchUpdate(request, createdPresentation.presentationId);

    const updatedLink = `https://docs.google.com/presentation/d/${updatedPresentation.presentationId}/edit`;
    Logger.log(`Updated presentation is on: ${updatedLink}`);
}

Error: GoogleJsonResponseException: API call to slides.presentations.batchUpdate failed with error: Invalid JSON payload received. Unknown name "updateTitle" at 'requests[0]': Cannot find field.

CodePudding user response:

Here are two ways to edit a new presentation, one using SlidesApp and the second using Slides API.

function newPresentation1() {
  try {
    let presentation = Slides.Presentations.create({'title': 'MyNewPresentation'});
    presentation = SlidesApp.openById(presentation.presentationId);
    let slide = presentation.getSlides()[0];
    let element = slide.getPageElements()[0];
    element.asShape().getText().setText("Hello")
  }
  catch(err) {
    console.log(err)
  }
}

function newPresentation2() {
  try {
    let presentation = Slides.Presentations.create({'title': 'MyNewPresentation'});
    let pageElement = presentation.slides[0].pageElements[0].objectId;
    let request = { insertText: { objectId: pageElement,
                                  text: "Good bye" }
                  };
    Slides.Presentations.batchUpdate( { requests: [ request ] }, presentation.presentationId );
  }
  catch(err) {
    console.log(err)
  }
}

Reference

  • Related