google-apps-scriptgoogle-slides

Google slide shape copy problem using appscript


There is 1 shape that I created in Google Slide, and it creates as many copies on the page as I want to create from index.html.

My real question is, 10 copies fit on 1 page. 11. The copying process should continue from the 2nd page, but in this function, the copying process continues from the 1st page.

Note copying positions are available in the code, the same positions are also valid for the 2nd, 3rd and 4th pages.

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


Solution

  • Although I'm not sure whether I could correctly understand your expected result, from your reply, how about the following modification? In this modification, your function copyShapes is modified.

    Modified script:

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