我有一个费用表,我为每个相关用户复制到 Google Drive 文件夹中。工作表的每个副本都需要一个可安装的触发器,该触发器调用一个函数以在最后一行数据被编辑时插入额外的行。这些是使用 onEdit 事件触发的。手动创建(通过“ Edit Current Project's triggers
”)时,可安装的触发器工作正常。
为了简化设置新用户的过程,我编写了触发器的创建脚本。虽然脚本创建了触发器并且它看起来与手动创建的完全相同,但它只有在我手动打开触发器、重新选择要触发的函数并重新保存时才有效。
我可以在运行创建脚本时确认我是所有者。
我看到的行为向我表明,我的脚本中的函数名称有问题,但对我来说一切都很好。我真的很感激这方面的一些帮助....
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 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);
}
}