简体   繁体   English

使用 Google Apps 脚本从母版幻灯片复制多张幻灯片

[英]Copying Multiple Slides from a Master Slide Desk using Google Apps Script

I have created a code which replaces placeholders on google slides.我创建了一个代码来替换谷歌幻灯片上的占位符。 The starting point of this project is a google form.这个项目的起点是一个谷歌表单。 Once the google form has been submitted - then the relevant data from google form is entered on the google slides template.一旦提交了谷歌表单 - 然后在谷歌幻灯片模板上输入来自谷歌表单的相关数据。 See below code.见下面的代码。 I am looking to create a question on the form where people will be able to select multiple slides to be included (2 Credential slides for example out of 10)我希望在表单上创建一个问题,人们可以在其中选择要包含的多张幻灯片(例如,10 张中的 2 张凭证幻灯片)

function PoD() {

  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("A-PoD").activate();

  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lr = ss.getLastRow()

  for (var i =2;i<lr;i++){

    if(ss.getRange(i, 1).getValue()){

  //Make a copy of the template file
  var documentId = DriveApp.getFileById('1REHMrl6kfzXbgSipvBDkNitkfsM8tJsUSAICggxNsHw').makeCopy().getId();

  var Name_of_programme = ss.getRange(i, 2).getValue();

  DriveApp.getFileById(documentId).setName("PwC's Academy_"+Name_of_client+"_"+Name_of_programme+"_"+Month);

  var FileName = Name_of_programme;

  //Get the document body as a variable
  var body = SlidesApp.openById(documentId);

  body.replaceAllText('{Name of programme}', Name_of_programme);


  var lastSlide = body.getSlides();
  lastSlide[5].remove();

I am looking to continue the script to include a function to select multiple slides.我希望继续脚本以包含选择多张幻灯片的功能。 I saw the below script to copy one slide but have not been able to figure out how to copy multiple slides easily.我看到了下面的脚本来复制一张幻灯片,但无法弄清楚如何轻松复制多张幻灯片。

var srcPresentationId = "### source fileId ###";
var copysrcSlideIndex = 0; // 0 means page 1.

var copydstSlideIndex = 0; // 0 means page 1.

var src = SlidesApp.openById(srcPresentationId).getSlides()[copysrcSlideIndex];
SlidesApp.getActivePresentation().insertSlide(copydstSlideIndex, src);

I want to give people the choice to select which slides to include on the google form as multiple choice.我想让人们选择将哪些幻灯片包含在谷歌表单中作为多项选择。

At the back end of the script, would I need to map the names of the slides with slide numbers?在脚本的后端,我是否需要用幻灯片编号映射幻灯片的名称? or can have include a unique reference in a text box on each slide and then select the relevant slide?或者可以在每张幻灯片的文本框中包含一个唯一的参考,然后选择相关的幻灯片? Thinking out loud here.在这里大声思考。 Any guidance would be appreciated.任何指导将不胜感激。

  • You want to copy the 5, 7, and 9 pages (indexes of 4, 6 and 8) in the master Google Slides to other Google Slides.您想将母版 Google 幻灯片中的 5、7 和 9 页(索引为 4、6 和 8)复制到其他 Google 幻灯片。
  • You want to achieve this using Google Apps Script.您想使用 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.请将此视为几种可能的答案之一。

Flow:流动:

  1. Retrieve all slides from the master Google Slides.从主 Google 幻灯片中检索所有幻灯片。
  2. Copy the required slide to the destination Google Slides in the loop.将所需幻灯片复制到循环中的目标 Google 幻灯片。

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

Pattern 2:模式2:

Sample script:示例脚本:

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

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.在这两种模式中,主 Google 幻灯片的第 5、7 和 9 页是从目标 Google 幻灯片的第 2 页复制的。
    • When var offset = 0 is used, the pages of 5, 7 and 9 of the master Google Slides are copied from 1st page in the destination Google Slides.当使用var offset = 0时,主 Google 幻灯片的第 5、7 和 9 页从目标 Google 幻灯片的第 1 页复制。

Reference:参考:

Thanks, @Tanaike, for the solutions!谢谢@Tanaike,提供解决方案!

With your code as a base, I was able to build a script to add slides to multiple slide decks in a single folder.以您的代码为基础,我能够构建一个脚本,将幻灯片添加到单个文件夹中的多个幻灯片组中。 I wanted to share for those who may need it.我想分享给可能需要它的人。 Code below...下面的代码...

function copySlides() {
  // Array of slide numbers to grab [1, 2, 3,...]
  var copyPageNumbers = [1];  // Please set the page number. This is not the index. So the 1st page is 1.
  // Which deck contains the slides to copy
  var masterGoogleSlidesId = '###';  // Please set the master Google Slides ID.

  // Folder containing Slides to copy to
  var folderID = '###';
  var folderToCopyTo = DriveApp.getFolderById(folderID);

  // Slide files in folder
  var slidesInFolder = folderToCopyTo.getFilesByType(MimeType.GOOGLE_SLIDES);

  var counter = 0;
  var total = 0;

  while (slidesInFolder.hasNext()) {

    var file = slidesInFolder.next();

    var destinationGoogleSlidesId = file.getId();  // Please set the destination Google Slides ID.
    var offset = 0;

    try {
      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]);
       });
      counter++;
    }
    catch (e) {
      console.log(destinationGoogleSlidesId);
      console.log(e);
    }

    total++;

  }

  console.log(counter + ' of ' + total + ' Slide Decks Updated');

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Google Apps Script 在来自 Google 表格的多行中拆分 Google 幻灯片表中的数据(基于字符限制)? - How to split data in a Google Slides Table (based on character limit) in multiple rows coming in from a Google Sheet using Google Apps Script? Google Apps 脚本将特定范围从任何选项卡复制到主工作表 - Google Apps Script Copying Specific Range from any tab to a Master Sheet 在使用谷歌应用脚本将数据从谷歌表格注入谷歌幻灯片期间将日期转换为 dd/mm/yyyy - Convert date to dd/mm/yyyy during injection of data from google sheets to google slides using google apps script 使用Google Apps脚本复制数据 - Data copying with Google Apps Script 我可以使用 Apps 脚本在 Google 幻灯片中按段落划分文本框吗? - Can I divide a text box by paragraph in Google Slides using Apps Script? 使用 Google Apps 脚本从 Google Drive 中的多个上传元素上传本地文件 - Upload Local Files from multiple upload element in Google Drive using Google Apps Script 如何使用 Google 表格中的 Google Apps 脚本从一个范围内创建多个选项卡? - How can multiple tabs be created from a range using Google Apps Script in a Google Sheet? 使用 Google Apps 脚本搜索没有结果 - No result from searching using Google Apps Script 如何使用 Google Script 从本地 Google Drive 中的图像文件夹创建 Google 幻灯片演示文稿? - How do I create a Google Slides presentation from a folder of images in a local Google Drive using Google Script? 使用 Google Apps 脚本,如何将每张演示文稿幻灯片下载为 pdf? - Using Google Apps Script, how can I download each presentation slide as a pdf?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM