Replace text in Slides

1,079 views
Skip to first unread message

Niall Hickey

unread,
Aug 23, 2022, 2:56:58 PM8/23/22
to Google Apps Script Community
Hi All,

I'm using a script to replace text in a slide with data from a sheet. Works great for shapes but I can't get it working for tables. It looks like I need to iterate through the whole sheet, then through each table, then through each cell.

Here is the code for the shapes (works):
 slides.forEach(function(slide){
     var shapes = (slide.getShapes());
     shapes.forEach(function(shape){
        /* shape.getText().replaceAllText('{{title}}',title); */
        /* shape.getText().replaceAllText('{{chart2}}',chart2); */
        /* Total Raised */
       shape.getText().replaceAllText('{{PTF}}',PTF);
       shape.getText().replaceAllText('{{ATF}}',ATF);
       shape.getText().replaceAllText('{{TFD}}',TFD);
       
       //shape.getText().replaceAllText('{{category}}',category);
       //shape.getText().replaceAllText('{{excerpt}}',excerpt);
     }); 
   })

And my attempted code for tables:
slides.forEach(function(slide){
     var xtables = (slide.getTables());
     xtables.forEach(function(table){
          var xcell = (table.getCell());
          xcell.forEach(function(cell){
       
        /* shape.getText().replaceAllText('{{title}}',title); */
        /* shape.getText().replaceAllText('{{chart2}}',chart2); */
        /* Total Raised */
       cell.getText().replaceAllText('{{PTF}}',PTF);
       cell.getText().replaceAllText('{{ATF}}',ATF);
       cell.getText().replaceAllText('{{TFD}}',TFD);
       });
       //shape.getText().replaceAllText('{{category}}',category);
       //shape.getText().replaceAllText('{{excerpt}}',excerpt);
     }); 
   })

Any help appreciated, thanks!

Clark Lind

unread,
Aug 24, 2022, 6:54:11 AM8/24/22
to Google Apps Script Community
In my experience, I have been able to replace text at the slide level without going into each table.  Did you try this first for the tables?

slides.forEach(function(slide){
    slide.replaceAllText('{{PTF}}',PTF);
    slide.replaceAllText('{{ATF}}',ATF);
    slide.getText().replaceAllText('{{TFD}}',TFD);
  })
}

Clark Lind

unread,
Aug 24, 2022, 7:04:06 AM8/24/22
to Google Apps Script Community
The last one should be:      slide.replaceAllText('{{TFD}}',TFD);
Reply all
Reply to author
Forward
0 new messages