Search code examples
javascriptgoogle-apps-scriptgoogle-sheetsgoogle-slides

Google Slides template isn't being filled in with Apps Script


function fillTemplate() {
  var Presentation = SlidesApp.openById("id");
  var sh = SpreadsheetApp.openById("id").getSheetByName("Sheet1");
  var values = sh.getDataRange().getValues();

  values.forEach(function(column){
    var templateVariable= column[0];
    var templateValue= column[1];
    Presentation.replaceAllText(templateVariable,templateValue)
  });
}


The code is supposed to replace variables inside a slide with information inside a google sheet. it shows no errors, however there is no change in the slide itself. is there something i got wrong?

i tried refreshing the google slide and trying more elaborate ways of changing the text including tables but all doesn't work. I was expecting the text that i entered in the google sheet to automatically fill in the slide variables.

(google sheet and slide template underneath) (don't ask about the text) slide template google sheet values


Solution

  • Modification points:

    • In your script, values is a 2-dimensional array like [[col1, col2,,,]]. So, column of values.forEach(function(column){ is a row value. And also, in this case, the header row is used in the 1st loop.

    When these points are reflected in your script, it becomes as follows.

    Modified script:

    function fillTemplate() {
      var Presentation = SlidesApp.openById("id");
      var sh = SpreadsheetApp.openById("id").getSheetByName("Sheet1");
      var [header, ...values] = sh.getDataRange().getDisplayValues();
      values.forEach(row =>
        row.forEach((col, j) => Presentation.replaceAllText(header[j], col))
      );
    }
    

    Note:

    • Unfortunately, I cannot know the row numbers in your Spreadsheet from your image. So, in this modification, it is supposed that the 1st row is the header row. Please be careful about this.

    References: