使用 appscript 替换形状组 Google 幻灯片中的占位符

问题描述 投票:0回答:1
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);
    }
  }
}

首先,Google Slide 中有 1 个图形组。这里我想要多少个副本,形状组的副本是根据位置来创建的。但该组中有占位符,{{NAME1}} 和 {{PRICE1}}、{{NAME1}}、{{NAME2}}、{{NAME3}} 等,我想要创建副本的占位符数量不限。 {{PRICE1}}、{{PRICE2}}、{{PRICE3}} 让副本更改为,例如,如果我想要 4 个副本{{PRICE1}},{{PRICE2}},{{PRICE3}},{{PRICE4}} 和 {{NAME1}}、{{NAME2}}、{{NAME3}}、{{NAME4}} 就这样一件一件的改变吧

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

并且

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

google-apps-script google-slides
1个回答
0
投票

相应地访问文本框

当我检查您的样本组时,我注意到文本框不在同一组中。一个比另一个更深。在检查级别后,我想出了以下脚本来在复制形状时访问和增加数字。

脚本

function copyShapes(copyCount) {
  const presentation = SlidesApp.getActivePresentation();
  const slide = presentation.getSlides()[0];
  let group = slide.getGroups()[0];
  const xIncrement = 10;
  const yIncrement = 5;
  const initialX = 0;
  const initialY = 0.5;
  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);
      
      //new code starts here
      var newGroup = slide.getGroups()[i];
      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) + "}}");
    }
  }
}

输出

这是我将重复项数设置为 10 时的输出: output

© www.soinside.com 2019 - 2024. All rights reserved.