1

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 :)

kaiak
  • 1,759
  • 1
  • 13
  • 11
  • From your question, I thought that in your question, to know your sample Spreadsheet and sample Slides might help thinking of the solution. So, can you provide the sample Spreadsheet and Slides? – Tanaike Apr 18 '23 at 08:10
  • @Tanaike slides added. Since I have multiple spreadsheets and multiple tables from which I create the variablesMap, I don't think it's very helpful to share the sheets without sharing full code. As I said before, the variablesMap is comprehensive and no variables are missing from it when logging. – kaiak Apr 18 '23 at 08:39
  • Thank you for replying. From your provided Google Slides, I proposed a modification point as an answer. Please confirm it. If that was not useful, I apologize. – Tanaike Apr 18 '23 at 08:58
  • @Tanaike I created a spreadsheet link and changed range accordingly :) – kaiak Apr 18 '23 at 09:30

1 Answers1

2

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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • I edited the code with your suggestion, but unfortunately it didn't help. The slides look exactly the same. Keys for 12 and 18 are in the real generalValues object, I have deleted them from this example only. Still lost on what's the problem. You seem to be correct, that the issue is with slides. – kaiak Apr 18 '23 at 09:55
  • As you have seen I have striped off most of the stuff from slides because of the PI. There are also some charts which are linked to a spreadsheet. Might that affect the outcome too? – kaiak Apr 18 '23 at 09:56
  • @kaiak Thank you for replying. About `but unfortunately it didn't help. The slides look exactly the same. Keys for 12 and 18 are in the real generalValues object`, I deeply apologize for this. As I have already mentioned in my 1st comment, it is required to know your Google Slides and Google Spreadsheet. Because, when I tested simple sample values and your Slides, no issue occurs. And I confirmed that the values of `slidesMap` could be replaced. And, when I tried to open your provided Spreadsheet, unfortunately, I cannot open it. I apologize for this. Can you confirm it again? – Tanaike Apr 18 '23 at 11:47
  • @kaiak About `There are also some charts which are linked to a spreadsheet.`, in only your showing script, it seems that your script doesn't use the charts. But, from your question, I cannot correctly imagine your current actual situation. So, I cannot know the modification points in your current actual script. I think that this is due to my poor skill. I deeply apologize for my poor skill again. – Tanaike Apr 18 '23 at 11:55
  • @kaiak Although, unfortunately, I cannot still confirm your provided Spreadsheet, when I tested your new Google Slides using my proposed modified script with a simple value of `generalValues`, no error occurs. I confirmed that the values of `slidesMap` could be correctly used and the placeholders could be replaced. I apologize for this. By the way, if you provide your spreadsheet, please also provide your script for correctly replicating your current issue. By this, I would like to confirm it. If you can cooperate in resolving your issue, I'm glad. Can you cooperate to do it? – Tanaike Apr 18 '23 at 12:15
  • @kaiak Unfortunately, I cannot still open your provided Spreadsheet. So, I would like to propose a simple sample value of `generalValues`. Please set the following script instead of `const generalValues = sheet.getRange('A1:B47').getValues();`. And, test it again. When I tested my proposed modified script using this sample value, the placeholders of the pages of keys in `slidesMap` could be replaced. Please test it. – Tanaike Apr 18 '23 at 13:10
  • @kaiak `const v1 = [["firstName","sample1"],["date","sample2"],["diversityScore","sample3"],["diversityDef","sample4"],["butyrateEfficiency","sample5"],["propionateEfficiency","sample6"],["bacteria_1_three","sample7"],["bacteria_1_three_def","sample8"],["bacteria_2_three","sample9"],["bacteria_2_three_def","sample10"],["bacteria_3_three","sample11"],["bacteria_3_three_def","sample12"],["bacteria_1","sample13"]];` – Tanaike Apr 18 '23 at 13:10
  • @kaiak `const v2 = [["bacteria_1_def","sample14"],["bacteria_2","sample15"],["bacteria_2_def","sample16"],["bacteria_3","sample17"],["bacteria_3_def","sample18"],["bacteria_10","sample19"],["bacteria_10_def","sample20"],["bacteria_11","sample21"],["bacteria_11_def","sample22"],["bacteria_12","sample23"],["bacteria_12_def","sample24"],["bacteria_13","sample25"],["bacteria_13_def","sample26"],["bacteria_14","sample27"],["bacteria_14_def","sample28"],["bacteria_15","sample29"],["bacteria_15_def","sample30"]];` – Tanaike Apr 18 '23 at 13:10
  • @kaiak `const generalValues = [...v1, ...v2];` – Tanaike Apr 18 '23 at 13:10
  • @kaiak Please test my proposed modified script using my proposed simple value. And, please tell me the result of it. – Tanaike Apr 18 '23 at 13:13
  • I ungrouped the groups on the slides manually and ran my original script. Everything works now! You were correct, the problem was with groups. Thank you very much! – kaiak Apr 18 '23 at 13:57