[英]How can I automatically insert images for each row of Google Sheets document to a Slides template (mail merge) using Apps Script?
I want that Apps Script to automatically generate a new set of slides using data from a Sheets document which has rows of the different information I want inserted into a Slides template replacing the placeholder tags.我希望 Apps 脚本使用 Sheets 文档中的数据自动生成一组新的幻灯片,其中包含我想要插入到幻灯片模板中的不同信息行,以替换占位符标签。 I want it to do it instantly for each row inside the table with one action, so if there are 10 rows, 10 sets of Slides documents will be generated.
我希望它通过一个操作立即为表格中的每一行执行此操作,因此如果有 10 行,将生成 10 组幻灯片文档。
The text replacement works, however I'm not sure how to replace, for example, a placeholder tag with "{{image}}"文本替换有效,但我不确定如何替换,例如,用“{{image}}”替换占位符标签
The Image is a generated Qr code in column (N) with an sheet addon (QR Code for Classroom Attendance)and for each row separate.图像是在列 (N) 中生成的 Qr 代码,带有工作表插件(课堂出勤的 QR 代码)并且每一行都是单独的。 For example 10 rows with different qr codes.
例如 10 行不同的二维码。 This addon writes the generate QR code in the column N. As I said for each I have a different Qr code.
这个插件在 N 列中写入生成的 QR 码。正如我所说的,每个我都有不同的 Qr 码。
function mailMergeSlidesFromSheets() {
// Load data from the spreadsheet
var dataRange = SpreadsheetApp.getActive().getDataRange();
var sheetContents = dataRange.getValues();
// Save the header in a variable called header
var header = sheetContents.shift();
// Create an array to save the data to be written back to the sheet.
// We'll use this array to save links to Google Slides.
var updatedContents = [];
// Add the header to the array that will be written back
// to the sheet.
updatedContents.push(header);
// For each row, see if the 4th column is empty.
// If it is empty, it means that a slide deck hasn't been
// created yet.
sheetContents.forEach(function(row) {
if(row[14] === "") {
// Create a Google Slides presentation using
// information from the row.
var slides = createSlidesFromRow(row);
var slidesId = slides.getId();
// Create the Google Slides' URL using its Id.
var slidesUrl = `https://docs.google.com/presentation/d/${slidesId}/edit`;
// Add this URL to the 4th column of the row and add this row
// to the updatedContents array to be written back to the sheet.
row[14] = slidesUrl;
updatedContents.push(row);
}
});
// Write the updated data back to the Google Sheets spreadsheet.
dataRange.setValues(updatedContents);
}
function createSlidesFromRow(row) {
// Create a copy of the Slides template
var deck = createCopyOfSlidesTemplate();
// Rename the deck using the firstname and lastname of the student
deck.setName(row[4] + " " + row[9] + row[3]);
// Replace template variables using the student's information.
deck.replaceAllText("{{id}}", row[0]);
deck.replaceAllText("{{tag}}", row[3]);
deck.replaceAllText("{{besetzung}}", row[4]);
deck.replaceAllText("{{beginn}}", row[5]);
deck.replaceAllText("{{ende}}", row[6]);
deck.replaceAllText("{{halle}}", row[7]);
deck.replaceAllText("{{stand}}", row[8]);
deck.replaceAllText("{{firma}}", row[2]);
deck.replaceAllText("{{veranstaltung}}", row[9]);
deck.insertImage("{{image}}", row[13]);
return deck;
}
function createCopyOfSlidesTemplate() {
//
var TEMPLATE_ID = "19PKvWoDtbeVHcqm4DnWUxRx1OBO817uG3cL5Ox-dQoo";
// Create a copy of the file using DriveApp
var copy = DriveApp.getFileById(TEMPLATE_ID).makeCopy();
// Load the copy using the SlidesApp.
var slides = SlidesApp.openById(copy.getId());
return slides;
}
function onOpen() {
// Create a custom menu to make it easy to run the Mail Merge
// script from the sheet.
SpreadsheetApp.getUi().createMenu("⚙️ Create BWN by Pavlos")
.addItem("Create Slides","mailMergeSlidesFromSheets")
.addToUi();
}
replaces the placeholder tags with the desired text
// I am not sure how to insert something similar for images and charts in the code here
// I've tried variations of the below, none of which have worked
// picture.find("{{image}}").replace(image);
// picture.findText("{{image}}").replace(image);
// picture.getText("{{image}}").replaceWithImage(image);
// picture.getText().findText("{{image}}").replace(image);
I believe your goal is as follows.我相信你的目标如下。
{{Image}}
on 1st page of Google Slides with the image.{{Image}}
。"https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl="&M2
."https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl="&M2
。 In this case, how about directly using the values of column "M" as follows?在这种情况下,如何直接使用“M”列的值如下? When your script is modified, please modify it as follows.
当您的脚本被修改时,请修改如下。
deck.insertImage("{{image}}", row[13]);
deck.getSlides()[0].getShapes().find(s => s.getText().asString().trim().toUpperCase() == "{{IMAGE}}").replaceWithImage(`https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=${row[12]}`);
In this modification, it supposes that row[14] === ""
is true and the column "M" has the value of https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=${value}
.在此修改中,它假设
row[14] === ""
为真,并且列 "M" 的值为https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=${value}
. Please be careful about this.请注意这一点。
It seems that there are a lot of rows you want to process.似乎有很多行要处理。 So, if the maximum execution time (6 minutes) is over, please separate the rows for processing.
所以,如果超过最大执行时间(6分钟),请分行处理。 In your this question, you want to replace
{{Image}}
with the image.在你的这个问题中,你想用图像替换
{{Image}}
。 So, this answer is for it.所以,这个答案是为了它。 Please be careful about this.
请注意这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.