使用 appscript 的 Google 幻灯片形状复制问题

问题描述 投票:0回答:1

我在 Google Slide 中创建了 1 个形状,它在页面上创建了我想要从 index.html 创建的多个副本。

我真正的问题是,10 份副本适合一页。 11. 复制过程应该从第 2 页继续,但在此功能中,复制过程从第 1 页继续。

注意代码中提供了复制位置,相同的位置对于第 2、3、4 页也有效。

function copyShapes(copyCount) {
  const presentation = SlidesApp.getActivePresentation();
  let slide = presentation.getSlides()[0];
  const group = slide.getGroups()[0];


  const duplicatedGroup = group.duplicate();

 
  group.remove();

  const xIncrement = 10; // x artış oranı
  const yIncrement = 5;  // y artış oranı
  const initialX = 0;
  const initialY = 0.5;
  const maxCopiesPerPage = 10;

  for (let i = 0; i < copyCount; i++) {
    if (i % maxCopiesPerPage === 0) {
      // Yeni bir sayfa ekle ve slide değişkenini güncelle
      if (i > 0) {
        slide = presentation.appendSlide(SlidesApp.PredefinedLayout.BLANK);
      }
    }

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

   
    const newGroup = duplicatedGroup.duplicate();
    newGroup.setLeft(x).setTop(y);
  }


  duplicatedGroup.remove();
}

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

虽然我不确定是否能正确理解你的预期结果,但从你的回复来看,下面的修改如何?在此修改中,您的函数

copyShapes
被修改。

修改后的脚本:

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);
    }
  }
}
  • 当我使用您提供的 Google Slide 测试此修改后的脚本时,我确认当我将 12 与
    copyCount
    一起使用时,2 个组将放入第二张幻灯片中。
© www.soinside.com 2019 - 2024. All rights reserved.