0

I am using the google slides API to automatically update a presentation which has some pretty detailed tables. I have a table with several cells, and within each cell I have 3 different Lines with different formatting. For example, the top line has a bigger font, the second line is smaller and green, etc. As this presentation should update automatically everyday with my inputed data, I really need to set up requests based off of the TextRun lengths rather than fixed start and end indexes (these start and end indexes will change everyday as the text will update, so I cant manually edit the index everyday)

How can I either replace the text only within a TextRun or replace the text on a line by line or paragraph basis?

I am adding an image here. You can see that in the first line I have one large bold number that needs to be updated, and in the second line I have 3 items that need to be updated. 1. Y/Y 2. Q/Q 3. M/M I want to be able to do this without requiring every text to be the same size.

Screenshot of table in slide

Here is the batch update request I have set up right now. I just cant figure out how I can get the range to always be within a textRun, rather than for the entire cell.

    reqs = [
{
  "deleteText": {
    "objectId": "g2f8579c174_1_6",
    "cellLocation": {
      "rowIndex": 1,
      "columnIndex": 1,
    },
    "textRange": {
        "type": "ALL",
    }
  }
},
{
  "insertText": {
    "objectId": "g2f8579c174_1_6",
    "cellLocation": {
      "rowIndex": 1,
      "columnIndex": 1
    },
    "text": "texttexttext",
    "insertionIndex": 0
  }
}

]

Alex
  • 193
  • 1
  • 2
  • 10

1 Answers1

0

I would do a read of the presentation (either presentations.get or presentations.pages.get), find the runs and indices you want to update, then create a batchUpdate request to update those runs as you need. You can use InsertTextRequest to add your new content followed by DeleteTextRequest to get rid of the old content. It should preserve styling automatically.

Maurice Codik
  • 598
  • 2
  • 6
  • So i found the indices, but since I would be injecting new numbers automatically daily (which may have more or less characters, like 1.325 vs 1.1), the character count of each number would in turn change the start and end indices. From what I understand, then I would have to manually go through and find the new indices again, which is not feasible on a daily basis. – Alex Mar 13 '18 at 13:54
  • Since your code is inserting text itself, you should know how long each piece is and can write code to compute the indices to pass into an UpdateTextStyleRequest. For example, in your screenshot, your text is: "1.0\n+28% Y/Y ...", so you can style characters 0-3 (the "1.0") with bold/black foreground/font 24pt, characters 4-8 (the "+28%") with green foreground, and so on. – Maurice Codik Mar 15 '18 at 01:11
  • Ahh that worked beautifully. Thanks so much for the help! – Alex Mar 22 '18 at 13:57