google-apps-scriptgoogle-slides

Replace placeholders within shape groups Google slide using appscript


function copyShapes(copyCount) {
  const presentation = SlidesApp.getActivePresentation();
  const slide = presentation.getSlides()[0];
  let group = slide.getGroups()[0];
  const xIncrement = 10;
  const yIncrement = 5.5;
  const initialX = 0.25;
  const initialY = 1;
  const maxCopiesPerPage = 10;
  for (let i = 0; i < copyCount; i++) {
    if (i % maxCopiesPerPage === 0) {
      if (i > 0) {
        group = presentation.appendSlide(SlidesApp.PredefinedLayout.BLANK).insertGroup(group);
      }
    } else {
      const positionIndex = i % maxCopiesPerPage;
      const row = Math.floor(positionIndex / 2);
      const col = positionIndex % 2;
      const x = cmToPoints(initialX + col * xIncrement);
      const y = cmToPoints(initialY + row * yIncrement);
      group.duplicate().setLeft(x).setTop(y);
    }
  }
}

First of all, there is 1 figure group in Google Slide. Here, how many copies I want, the copies of the shape group are created according to their positions. but there are placeholders in the group, {{NAME1}} and {{PRICE1}}, {{NAME1}}, {{NAME2}},{{NAME3}} and so on for as many placeholders as I want to create copies. {{PRICE1}},{{PRICE2}},{{PRICE3}} Let the copies change as, for example, if I want 4 copies{{PRICE1}},{{PRICE2}},{{PRICE3}},{{PRICE4}} And {{NAME1}}, {{NAME2}},{{NAME3}},{{NAME4}} Let it change one by one like this

https://docs.google.com/presentation/d/1qFbwRUB4aCnfpMGIhMc_eCpKEMTZPkqHH52khxDJh6o/edit#slide=id.p

AND

https://script.google.com/u/0/home/projects/13-f6TrIUFYpN1qtkITIgX4-zd1fDtTTsJkDSMgkSiOjX_aibaVjQhG3Z/edit


Solution

  • Access the Textboxes Accordingly

    When I examined your sample group, I noticed that the textboxes were not on the same group. I advise grouping multiple textboxes within shapes together so that they may be easier to access. One was deeper than the other. Upon examining the levels, I came up with the following script to access and increment the numbers when duplicating the shapes.

    Script

    function copyShapes(copyCount) {
      const presentation = SlidesApp.getActivePresentation();
      const xIncrement = 10;
      const yIncrement = 5;
      const initialX = 0;
      const initialY = 0.5;
      const maxCopiesPerPage = 10;
      var slide = presentation.getSlides()[0];
      for (let i = 0; i < copyCount; i++) {
        var slideNumber = Math.floor(i / maxCopiesPerPage);
        var group = slide.getGroups()[0];
        const positionIndex = i % maxCopiesPerPage;
        const row = Math.floor(positionIndex / 2);
        const col = positionIndex % 2;
        const x = cmToPoints(initialX + col * xIncrement);
        const y = cmToPoints(initialY + row * yIncrement);
        if (i % maxCopiesPerPage === 0) {
          if (i > 0) {
            group = presentation.appendSlide(SlidesApp.PredefinedLayout.BLANK).insertGroup(group);
            slide = presentation.getSlides()[slideNumber];
          }
        } else {
          group.duplicate().setLeft(x).setTop(y);
        }
        var newGroup = slide.getGroups()[i % maxCopiesPerPage];
        newGroup.getChildren()[0].asGroup().getChildren()[4].asShape().getText().setText("{{NAME" + (i + 1) + "}}"); //Name was deeper than Price
        newGroup.getChildren()[1].asShape().getText().setText("{{PRICE" + (i + 1) + "}}");
      }
    }
    

    Output

    This was my output when I set the number of duplicates to 10: output

    References: