问题
I have an Expenses sheet that I copy to the Google Drive folder for each of my relevant users. Each copy of the sheet needs an installable trigger that calls a function to insert additional rows when the last row of data gets edited. These are triggered using the onEdit event. The installable triggers work fine when created manually (via "Edit Current Project's triggers
").
To simplify the process of setting up new users, I have scripted the creation of my trigger. Although the script creates the trigger and it appears exactly the same as the manually created one, it only works if I manually open up the trigger, re-select the function to trigger and re-save it.
I can confirm I am the owner when running the creation script.
The behaviour I'm seeing suggests to me that there is something incorrect with the function name in my script however it all looks fine to me. I'd really appreciate some help with this....
function createInsertRowsTrigger(){
Logger.log('Id='+ss.getId());
createInstallableTrigger("onEditOfLastRow",ss.getId());
}
function createInstallableTrigger(funcName,ssId) {
if(!isTrigger(funcName)) {
ScriptApp.newTrigger(funcName).forSpreadsheet(ssId).onEdit().create();
}
}
function isTrigger(funcName){
Logger.log('In isTrigger');
var r=false;
if(funcName){
Logger.log('Function found');
var allTriggers=ScriptApp.getProjectTriggers();
for(var i=0;i<allTriggers.length;i++){
if(funcName==allTriggers[i].getHandlerFunction()){
r=true;
Logger.log('trigger already exists');
break;
}
}
}
return r;
}
The full code is here....
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('SCLGA Expenses Menu')
.addItem('Approve Expenses', 'ApproveExpenses')
.addToUi();
resetValidationRules();
}
//===========================================================================================
// global
var ss = SpreadsheetApp.getActive();
//===========================================================================================
function onEditofLastRow(e) {
var value = ss.getActiveSheet().getRange(3, 6).getValue();// row number of the last but one row
editedCol =e.range.getSheet().getActiveCell().getColumn();
editedRow =e.range.getSheet().getActiveCell().getRow();
// if the last cell in the last row is edited...create some more rows...
if(editedCol==7 && editedRow== value-1){
showAlert('Looks like you need some more rows... ');
insertRows();
}
}
function createInsertRowsTrigger(){
Logger.log('Id='+ss.getId());
createInstallableTrigger("onEditOfLastRow",ss.getId());
}
function createInstallableTrigger(funcName,ssId) {
if(!isTrigger(funcName)) {
ScriptApp.newTrigger(funcName).forSpreadsheet(ssId).onEdit().create();
}
}
function isTrigger(funcName){
Logger.log('In isTrigger');
var r=false;
if(funcName){
Logger.log('Function found');
var allTriggers=ScriptApp.getProjectTriggers();
for(var i=0;i<allTriggers.length;i++){
if(funcName==allTriggers[i].getHandlerFunction()){
r=true;
Logger.log('trigger already exists');
break;
}
}
}
return r;
}
function showAlert(msg) {
var ui = SpreadsheetApp.getUi();
var result = ui.alert(msg);
}
function insertRows() {
// this function inserts some new rows and copies relevant formulas and formats
// this value tells us where the next data section starts
var value = ss.getActiveSheet().getRange(3, 6).getValue();
var rowsToAdd = 5;
var firstRow = value-rowsToAdd;
var sh = ss.getActiveSheet();
var lCol = sh.getLastColumn();
var range = sh.getRange(firstRow, 1, rowsToAdd, lCol);
// copy formulas
var formulaRange1 = sh.getRange(firstRow, 1, rowsToAdd, 1);
var formulaRange2 = sh.getRange(firstRow, 8, rowsToAdd, lCol-8);
var formulas1 = formulaRange1.getFormulasR1C1();
var formulas2 = formulaRange2.getFormulasR1C1();
// insert rows
sh.insertRowsAfter(firstRow+rowsToAdd-1, rowsToAdd);
// define new range
newRange1 = sh.getRange(firstRow+rowsToAdd, 1, rowsToAdd, 1);
newRange2 = sh.getRange(firstRow+rowsToAdd, 8, rowsToAdd, lCol-8);
// set formulas
newRange1.setFormulasR1C1(formulas1);
newRange2.setFormulasR1C1(formulas2);
// copy formatting for range 2
range.copyFormatToRange(sh, 1, lCol-8, firstRow+rowsToAdd, firstRow+rowsToAdd+rowsToAdd-1);
resetValidationRules();
resetTransactionUploadFormulas();
}
function resetTransactionUploadFormulas(){
var sheet = ss.getSheetByName('Transactions to load');
sourceFormulas = sheet.getRange(2,1,1,12).getFormulasR1C1();
for (i=3; i<=250; i++)
newRange=sheet.getRange(i,1,1,12).setFormulasR1C1(sourceFormulas);
}
function resetValidationRules(){
var value = ss.getActiveSheet().getRange(3, 6).getValue();
var firstRow = 5;
var sh = ss.getActiveSheet();
var lCol = sh.getLastColumn();
var range = sh.getRange(firstRow, 1, 245, lCol);
// Apply validation rules to all rows
for(i = 1; i <= 250; i++){
currentRowNumber = 4+i;
var cell = SpreadsheetApp.getActive().getRange('F'+currentRowNumber);
var eventRange = sh.getRange("L"+currentRowNumber+":BB"+currentRowNumber)
var rule = SpreadsheetApp.newDataValidation().requireValueInRange(eventRange).build();
if (value != currentRowNumber && value+1 != currentRowNumber)
cell.setDataValidation(rule);
}
}
来源:https://stackoverflow.com/questions/59324950/installable-trigger-created-using-scriptapp-newtrigger-has-to-be-re-saved-befo