I have ran into a problem with a template I have been working on. I have a template file in a folder on a shared drive that has a script containing a create function and close function, triggered by this installed on edit ran from a drop down.
function onMyEdit(e) {
if(sh.getName()=="Sheet2" && e.range.columnStart==6 && e.range.rowStart==3 && e.value) {
switch(e.value) {
case 'Create':
Create();
break;
case 'Close':
Close();
}
e.range.setValue('');
}
}
The create function created a renamed copy of the template in a folder on the shared drive, adds a calendar event, and writes certain data to another spreadsheet in the drive acting as a data base. I manually installed a trigger for the onMyEdit function in the template file, but when I create the copy the trigger does not work so the close function will not run from the newly created sheet. I tried programming the create function to install a trigger on the new sheet. Useing this
var WOssID = WOss.getId(); //get the ID of the newly created work order spreadsheet
Logger.log(WOssID)
ScriptApp.newTrigger('onMyEdit')
.forSpreadsheet(WOssID)
.onEdit()
.create()
where WOss is the spreadsheet that I just created by copying the template, and I check the logger and the ID does match the new sheet. This does fix it so I can run the close function from the newly created sheet, but it seems to add a trigger to the template sheet not the new sheet, and it keeps adding a trigger every time I run the create function from the template file until it limits out at 20 and then it all breaks. I must be misunderstanding something because I don't understand why it is adding triggers to the template sheet when I have the ID for the newly created sheet being called. Also hoping to get this all to run on an Ipad so believe that means I can't use added on menus, onopen calls, or clickable buttons. Any help is appreciated.
UPDATE
So I now have a simple onEdit trigger that creates a installed onMyEdit trigger to my work order sheet on first edit and doesn't create multiple triggers in my template file. However I need to figure out a way to authorize it in the newly created sheet, without going into the script editor. If I open the newly created sheet and make an edit the new installed trigger shows up in the project triggers, but it won't run until I try to run it from the script editor first at which time it asks for authorization. Is there a way to tie the authorization to a checkbox that when clicked makes the popup appear to authorize the installed trigger so it can run?
function onEdit(){
var WOss = SpreadsheetApp.getActiveSpreadsheet();
var WOssID = WOss.getId();
var allTriggers =
ScriptApp.getUserTriggers(WOss)//ScriptApp.getProjectTriggers();
Logger.log(allTriggers.length)
if(allTriggers.length == '0' ){
ScriptApp.newTrigger('onMyEdit').forSpreadsheet(WOssID).onEdit().create()
}
else(allTriggers.length != '0')
{}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…