当我使用openById()时,我的脚本退出。
该脚本应该能够检测用户何时修改了主列表电子表格中的两个相邻列(即用户在作业列表中输入作业的名称和日期)。然后,它调用一个函数来创建新的电子表格(现有模板电子表格的副本)。尚未创建新的电子表格。
我已经将代码精简化,并使用记录器缩小了问题范围。我不太确定这里发生了什么,但是在我的openById()变量赋值被记录之后,什么也没有。
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*/
记录器应记录所有1-11。它仅记录1,2,3,4。在第4点之后和第5点之前停止了脚本。
答案 0 :(得分:0)
您将无法使用simple trigger(它没有正确的授权)来执行此操作,为此,请对功能onEdit
使用installable trigger。这将提示您输入需要正确运行的授权。
下面的脚本应该为您设置好,只需运行一次即可,一切都很好。
function createSpreadsheetEditTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger('onEdit')
.forSpreadsheet(ss)
.onEdit()
.create();
}