I have a container bound Google apps script attached to a Google Sheet that uses the Slides API to pull data and replace values from the sheet to perform a "mail merge" on a select Google Slides presentation using a custom menu element.
The issue is, that when copying the sheet, the script functionality is copied, yet Google generates a new project for the script which does not have the Slides API enabled in the project API console.
My goal is to encapsulate the parameters for the "mail-merge" into the slides presentation to allow for those with no programming experience to make a copy of the sheet, update the slide template ID and destination drive folder (within the sheet) to allow for the dynamic generation of presentations. Everything is working great with the exception of having to manually navigate to enable the Slides API for the copied project. Is there a way to either enable the API for the new project on the fly or associate the copied container script to an existing project with the console APIs enabled?
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('WF Slides')
.addItem('Make slide deck', 'makeSlideDeck')
.addToUi();
}
function makeSlideDeck() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var metaSheet = sheet.getSheetByName('Meta');
var replaceSheet = sheet.getSheetByName('Replace');
var fileName = metaSheet.getDataRange().getCell(2,3).getValue();
var templateId = metaSheet.getDataRange().getCell(3,3).getValue();
var driveId = metaSheet.getDataRange().getCell(4,3).getValue();
var file = DriveApp.getFileById(templateId).makeCopy(fileName,
DriveApp.getFolderById(driveId));
var newPresentationId = file.getId();
var range = replaceSheet.getDataRange();
var numRows = range.getNumRows();
var replacements = [];
var requests = [];
var i0 = 0;
for (var i = 2; i <= numRows; i++) {
var val1 = range.getCell(i,2).getValue();
var val2 = range.getCell(i,3).getValue();
var val3 = range.getCell(i,4).getValue();
replacements[i0] = [val1, val2, val3];
i0++;
}
for (var i = 0; i <= replacements.length - 1; i++) {
if(replacements[i][0] == 'text') {
requests[i] = appendRequestText(replacements[i][1], replacements[i][2]);
} else {
requests[i] = appendRequestImage(replacements[i][1], replacements[i][2]);
}
}
Slides.Presentations.batchUpdate({'requests': requests}, newPresentationId);
SpreadsheetApp.getUi()
.alert('Your presentation has been generated.');
}
function appendRequestText(find, replace) {
return {
'replaceAllText': {
'containsText': {'text': find, 'matchCase': true},
'replaceText': replace
}
};
}
function appendRequestImage(find, replace) {
return {
'replaceAllShapesWithImage': {
'imageUrl': replace,
'replaceMethod': 'CENTER_INSIDE',
'containsText': {
'text': find,
'matchCase': true
}
}
};
}