2

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.

Rohan Sharma
  • 1,416
  • 2
  • 14
  • 19

2 Answers2

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.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • 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 Feb 12 '21 at 23:07
  • @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. – Tanaike Feb 13 '21 at 00:37
  • @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! – aNewb Aug 20 '21 at 23:01
2

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.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56