3

I am not able to find the right way to update text of existing objects using the google slides API.

Currently I am deleting the slide and creating it again.

0

2 Answers 2

3

How about using presentations.batchUpdate? By using this, the texts in the figures and on slide can be modified. The sample script is as follows. When you use this, please enable Slide API at API console and Advanced Google Services.

Sample script :

This sample modifies "sample text" on the slide of pageObjectIds to "updated text".

var presentationId = "### presentationId ###";
var resource = {
  "requests": [
    {
      "replaceAllText": {
        "containsText": {
          "text": "sample text"
        },
        "replaceText": "updated text",
        "pageObjectIds": ["### pageObjectIds ###"] // If this is not defined, the text is searched from all slides.
      }
    }
  ]
};
Slides.Presentations.batchUpdate(resource, presentationId);

If I misunderstand your question, I'm sorry.

Sign up to request clarification or add additional context in comments.

3 Comments

Have you ever tried to use a deleteText request in the same batchupdate with an insertText to change the text. I coulfn't get it to work that way so I had to run them in separate batches. I specified a range type of all but it keeping telling that startIndex had to be smaller that endIndex even though I hadn't used them at all.
@Cooper Unfortunately, I cannot understand about use a deleteText request in the same batchupdate with an insertText to change the text.. I apologize for this. In order to correctly understand about your question, can you provide the sample situation? By this, I would like to try to understand it.
@Cooper the range in table cells in a slide is tricky. In order to see if the cell was empty before using insertText txtBack = tblArr[0].getCell(r, c).getText().asString(); if ( txtBack.length > 1) . Without the asString the txtBack was "txtBack: TextRange txtBack.length: 9.0" With the asString an empty cell has a length of 1!
3

Inserting, deleting, or replacing text docs is what you're looking for.

There are two ways you can replace text in a presentation using the Slides API:

  1. By performing a global search-and-replace.
  2. By explicitly deleting and adding text. Both ways use the batchUpdate method, but with different request types.

Global search and replace

Use ReplaceAllTextRequest to do a global search-and-replace throughout your presentation.

The Text Merging section of the Merging Data guide provides an example of how you can use this request type.

Replacing text within a shape

The Slides API lets you modify the text content of a shape. You can remove individual ranges of text, and you can insert text at a specific location.

Use InsertTextRequest and DeleteTextRequest to perform these operations.

Comments

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.