Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
138 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here's a function I use to create triggers:

function createOnFormSubmitTriggerForSpreadsheet() {
  const ssid=SpreadsheetApp.getActive().getId();
  const resp=SpreadsheetApp.getUi().prompt("Create On Form Submit Trigger", "Enter Function Name", SpreadsheetApp.getUi().ButtonSet.OK_CANCEL);
  if(resp.getSelectedButton()==SpreadsheetApp.getUi().Button.OK) {
    let funcname=resp.getResponseText();

The next line prevents me from creating more than one trigger for the same function

    if(!isTrigger(funcname)) {//I use this to keep from creating more than one trigger

I recommend that you do the same

      ScriptApp.newTrigger(funcname).forSpreadsheet(ssid).onFormSubmit().create();   
    }
  }
}

function isTrigger(funcName){
  var r=false;
  if(funcName){
    var allTriggers=ScriptApp.getProjectTriggers();
    for (let i=0;i<allTriggers.length;i++){
      if(funcName==allTriggers[i].getHandlerFunction()){
        r=true;
        break;
      }
    }
  }
  return r;
}

And don't mean to be cruel but you code is a mess and you dont' need to describe your whole project we just want to see the minimum code that generates the problem. You said that you didn't know what's important and what's not. Well when you spend the time to create a minimum reproducible example you will have learned a lot more about your code and I often find that I end up solving the problem on my own.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...