0

The V8 Runtime has a problem with creating triggers by script so I need to convert to the old script template, but when trying to convert to Legacy, it doesn't save for two problems:

Original Script Part 1:

function installFunctions() {
  const excludedFunctions = ["onOpen", "installFunctions"];

  const menu = SpreadsheetApp.getUi().createMenu('Funções do GAS');
  for (let i in this) {
    if (typeof this[i] == "function" && !excludedFunctions.includes(i)) {
      menu.addItem(i, i);
    }
  }
  menu.addToUi();
}

function onOpen() {
  installFunctions();
}

Not saved because it contains a Missing ; after the for-loop launcher. error in this line of code:

  for (let i in this) {

Original Script Part 2:

function deleteTriggerWithName(name) {
  const trs = ScriptApp.getProjectTriggers().map(t => t.getHandlerFunction());
  const ts = ScriptApp.getProjectTriggers();
  ScriptApp.deleteTrigger(ts[trs.indexOf(name)]);
}

Not saved because it contains a Syntax Error in this line of code:

  const trs = ScriptApp.getProjectTriggers().map(t => t.getHandlerFunction());
Digital Farmer
  • 1,705
  • 5
  • 17
  • 67

1 Answers1

1

Modifications

  • Arrow functions are not supported in Rhino, so changed the map function into a normal for loop that adds items to an array.
  • It seems that while const is supported, using let in a for loop initialization is not. So replaced everything with var to be safe

Script

function installFunctions() {
  var excludedFunctions = ["onOpen", "installFunctions"];

  var menu = SpreadsheetApp.getUi().createMenu('Funções do GAS');
  for (var i in this) {
    if (typeof this[i] == "function" && !excludedFunctions.includes(i)) {
      menu.addItem(i, i);
    }
  }
  menu.addToUi();
}



function deleteTriggerWithName(name) {
  var trs = []

  for (var trigger in ScriptApp.getProjectTriggers()){
    trs.push(trigger.getHandlerFunction())
  }
  var ts = ScriptApp.getProjectTriggers();
  ScriptApp.deleteTrigger(ts[trs.indexOf(name)]);
}

This saved successfully in the script editor.

Reference

EDIT:

function deleteTriggerWithName(name) {
  var trs = []
  var projectTriggers = ScriptApp.getProjectTriggers()
  
  for (var i=0; i != projectTriggers.length; i++) {
    trs.push(projectTriggers[i].getHandlerFunction())
  }

  ScriptApp.deleteTrigger(projectTriggers[trs.indexOf(name)]);
}
iansedano
  • 6,169
  • 2
  • 12
  • 24
  • Hi mate: The error ```TypeError: Cannot find function getHandlerFunction on object 0.``` appears on that line of code ```trs.push(trigger.getHandlerFunction())``` , Could you help me modify it so that the function works? Thanks in advance – Digital Farmer Jun 21 '21 at 13:43
  • 1
    This would probably be best answered by asking another question, that said, I have made an edit, try that version of the function see if it works. – iansedano Jun 21 '21 at 14:03