且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Google Apps脚本从主幻灯片台复制多张幻灯片

更新时间:2023-02-14 08:30:23

  • 您要将主Google幻灯片中的5、7和9页(索引分别为4、6和8)复制到其他Google幻灯片.
  • 您想使用Google Apps脚本实现这一目标.
  • 我可以像上面那样理解.如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一.

    I could understand like above. If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

  1. 从主Google幻灯片中检索所有幻灯片.
  2. 在循环中将所需的幻灯片复制到目标Google幻灯片.

模式1:

示例脚本:

在运行脚本之前,请设置变量.

Pattern 1:

Sample script:

Before you run the script, please set the variables.

function myFunction() {
  var copyPageNumbers = [5, 7, 9];  // Please set the page number. This is not the index. So the 1st page is 1.
  var masterGoogleSlidesId = "###";  // Please set the master Google Slides ID.
  var destinationGoogleSlidesId = "###";  // Please set the destination Google Slides ID.
  var offset = 1;

  var src = SlidesApp.openById(masterGoogleSlidesId);
  var dst = SlidesApp.openById(destinationGoogleSlidesId);
  var slides = src.getSlides();
  var page = 0;
  slides.forEach(function(slide, i) {
    if (copyPageNumbers.indexOf(i + 1) != -1) {
      dst.insertSlide(offset + page, slide);
      page++;
    }
  });
}

模式2:

示例脚本:

function myFunction() {
  var copyPageNumbers = [5, 7, 9];  // Please set the page number. This is not the index. So the 1st page is 1.
  var masterGoogleSlidesId = "###";  // Please set the master Google Slides ID.
  var destinationGoogleSlidesId = "###";  // Please set the destination Google Slides ID.
  var offset = 1;

  var src = SlidesApp.openById(masterGoogleSlidesId);
  var dst = SlidesApp.openById(destinationGoogleSlidesId);
  var slides = src.getSlides();
  copyPageNumbers.forEach(function(p, i) {
    dst.insertSlide(offset + i, slides[p - 1]);
  });
}

注意:

  • 在两种模式下,主Google幻灯片的5、7和9页均从目标Google幻灯片的第二页复制.
    • 使用var offset = 0时,将从目标Google幻灯片的第一页复制Google幻灯片母版的5、7和9页.
    • Note:

      • In both patterns, the pages of 5, 7 and 9 of the master Google Slides are copied from 2nd page in the destination Google Slides.