Search code examples
javascriptnode.jsgoogle-apps-scriptgoogle-slides-api

Google app scripts not replacing all text in a slide


I have a map of slide numbers with an array of associated variables.

const slidesMap = {
  0: ['firstName', 'date'],
  10: ['diversityScore', 'diversityDef', 'butyrateEfficiency', 'propionateEfficiency'],
  15: ['bacteria_1_three', 'bacteria_1_three_def', 'bacteria_2_three', 'bacteria_2_three_def', 'bacteria_3_three', 'bacteria_3_three_def'],
  16: ['bacteria_1', 'bacteria_1_def', 'bacteria_2', 'bacteria_2_def', 'bacteria_3', 'bacteria_3_def'],
  18: ['bacteria_10', 'bacteria_10_def', 'bacteria_11', 'bacteria_11_def', 'bacteria_12', 'bacteria_12_def', 'bacteria_13', 'bacteria_13_def','bacteria_14', 'bacteria_14_def', 'bacteria_15', 'bacteria_15_def'],
};

I have a map of variables generated from a spreadsheet,

const variablesMap = new Map();
 const generalValues = sheet.getRange('A1:B47').getValues();

  generalValues.forEach(row => {
    variablesMap.set(row[0], row[1]);
  });

Then I populate the necessary slides with necessary variables.

for (const page in slidesMap) {
    const variables = slidesMap[page];

    let newSlide = slides[page];

    let shapes = newSlide.getShapes();
    shapes.forEach(shape => {
      variables.forEach(variable => {
        shape.getText().replaceAllText(`{{${variable}}}`,variablesMap.get(variable), true);
      });
    }); 
  }

When I log the variables in the loop, I have everything I need both in variablesMap and in slides map. Also the page numbers are correct.

What I get, is half populated slides. For example on slide 15 4/6 variables are populated. {{bacteria_1_three_def}} and {{bacteria_3_three}} are still as placeholders.

What am I missing?

On slide 18 it's even worse. all the ..._def are populated, but bacteria_... are as placeholders.

I am loosing my head.

I tried logging, everything seems correct.

link to slides

Solution: As @Tanaike hinted, the problem was in grouped elements. I ungrouped all the elements manually on the slides and the script worked just fine :)


Solution

  • When I saw your provided Google Slides, I noticed that your slides have several groups including shapes. In this case, all placeholders including groups cannot be modified with only shapes.forEach(shape => {,,,}) in your script. I thought that this might be the reason for your current issue.

    In this case, I thought that it is required to check the groups using the script. When your script is modified, how about the following modification?

    From:

    let newSlide = slides[page];
    
    let shapes = newSlide.getShapes();
    shapes.forEach(shape => {
      variables.forEach(variable => {
        shape.getText().replaceAllText(`{{${variable}}}`,variablesMap.get(variable), true);
      });
    }); 
    

    To:

    let newSlide = slides[page];
    
    let shapes = newSlide.getShapes();
    shapes.forEach(shape => {
      variables.forEach(variable => {
        shape.getText().replaceAllText(`{{${variable}}}`,variablesMap.get(variable), true);
      });
    });
    
    // I added below script.
    newSlide.getGroups().forEach(g => {
      g.getChildren().forEach(c => {
        if (c.getPageElementType() == SlidesApp.PageElementType.SHAPE) {
          variables.forEach(variable => {
            c.asShape().getText().replaceAllText(`{{${variable}}}`, variablesMap.get(variable), true);
          });
        }
      });
    });
    

    Note:

    • In this modification, it supposes that your values of generalValues are correct values. Please be careful about this.

    • By the way, the keys of 12 and 18 are not included in your slidesMap. By this, the placeholders on pages 13 and 17 of Google Slides are not replaced. Please be careful about this.

    References: