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.
// 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]);
return deck;
}
function createCopyOfSlidesTemplate() {
//
var TEMPLATE_ID = "1bcIS7K-CH9KH0IixCKehFniCgAReZEH3m84pd5UxhFg";
// 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();
}
I'm despairing and really don't know what to do. I have also tried the following:
change dataRange.setValues(updatedContents); to dataRange.offset(sheetContents.length, 0, updatedContents.length, updatedContents[0].length).setValues(updatedContents);