I have a Google script that runs on a trigger on form submit.
When the form is submitted a new row is added to a linked spreadsheet, and the script runs some actions to create a new folder and Google doc.
I'd like to amend this by adding new rows via Zapier and not the form sumbission.
Adding the new Rows with Zapier is easy,
However, in setting the script trigger to the 'On change' event from 'On form submit', I get the error;
TypeError: Cannot read property '0' of undefined
I think in using the e.values
that the script is expecting these from the form submission, but is failing because they are absent in Zapier added rows.
Problem:
I don't know how to resolve the error and allow the script to run in the same way when a form is submitted as when a new row is added from Zapier?
https://i.stack.imgur.com/vJuRw.png
What I have in place;
function autoFillGoogleDocFromForm(e) {
var Timestamp = e.values[0];
var NAME = e.values[1];
var file = DriveApp.getFileById('1234');
var folder = createNewFolder();
var copy = file.makeCopy(NAME, folder);
var newId = copy.getId();
try{
var doc = DocumentApp.openById(newId).addEditor(email);
}
catch(error){
var doc = DocumentApp.openById(newId);
}
var body = doc.getBody();
body.replaceText('{{Name}}', Name);
doc.saveAndClose();
}
<!--This creates a Google drive folder -->
function createNewFolder() {
var ss = SpreadsheetApp.getActive();
var names = ss.getSheetByName("Sheet1");
var POSNAME = names.getRange(names.getLastRow(),
2).getValue();
var folders = DriveApp.getFoldersByName(NAME);
while (folders.hasNext()) {
folder = folders.next();
if(folder.getName()==NAME){
return folder;
}
}
var parentFolder=DriveApp.getFolderById("1234");
return parentFolder.createFolder(NAME);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…