[英]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.
任何指导将不胜感激。
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.
请将此视为几种可能的答案之一。
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++;
}
});
}
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]);
});
}
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 页复制。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.