My script quits when I use openById().
The script is supposed to detect when a user has modified two adjacent columns in a master list spreadsheet (i.e. user enters the name and date of a job on a list of jobs). It then invokes a function to create a new spreadsheet (a copy of an existing template spreadsheet). The new spreadsheet isn't being created.
I've stripped my code down to the bare bones and narrowed down the problem using the logger. I'm not quite sure what's going on here, but nothing after my variable assignment of openById() is getting logged.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = e.range;
//var newId;
//var newName;
//I've removed the last two variables for debugging purposes. They'll be used to name the new spreadsheet.
Logger.log("1");
//Check if edit occurred in relevant range
if((range.getColumn() !== 1) && (range.getColumn() !== 2)) return;
Logger.log("2");
//Check if both columns were filled after edit
if((range.getColumn() == 1) && (range.offset(0,1).isBlank() == true)) return;
if((range.getColumn() == 2) && (range.offset(0,-1).isBlank() == true)) return;
Logger.log("3");
//Temporarily removed but this is for naming the new spreadsheet
/*if(range.getColumn() == 1) newName = range.offset(0,7).getValue();
if(range.getColumn() == 2) newName = range.offset(0,6).getValue();*/
//Check whether the edits occurred on the jobs list or receptions list (indicated by '2' or '3' in L1)
if(sheet.getRange('L1').getValue() == 2) newJob();
if(sheet.getRange('L1').getValue() == 3) newReception();
Logger.log("11");
//Once again, only temporarily removed.
/*SpreadsheetApp.openById(newId).rename(newName);*/
}
function newJob() {
Logger.log("4");
var templateSS = SpreadsheetApp.openById("Spreadsheet ID Redacted");
Logger.log("5");
var newSS = templateSS.copy("Untitled Job");
Logger.log("6");
var originalFolder = DriveApp.getFolderById("Folder ID Redacted");
Logger.log("7");
var newSSFile = DriveApp.getFileById(newSS.getId());
Logger.log("8");
//Copy file to the correct directory and delete the instance created in root
originalFolder.addFile(newSSFile);
Logger.log("9");
DriveApp.getRootFolder().removeFile(newSSFile);
Logger.log("10");
return(newSSFile);
}
/*Didn't bother pasting newReception here because it's nearly identical to newJob*/
The logger should log all numbers 1-11. It's only logging 1,2,3,4. Something is stopping the script after point number 4 and before point number 5.
onEdit(e)
trigger for this function. You'll need to use an installable trigger for this if you're trying to access anything outside of the spreadsheet your trigger is running for. Check the installable triggers documentation for the scope of each simple/installable trigger and how to create them. – ross