首页 > 解决方案 > 从 V8 Runtime 转换为 Rhino Runtime (Legacy) 我需要进行哪些脚本调整?

问题描述

V8 Runtime 在通过脚本创建触发器时存在问题,因此我需要转换为旧的脚本模板,但是在尝试转换为 Legacy 时,它不会保存两个问题:

原始脚本第 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();
}

Missing ; after the for-loop launcher.未保存,因为它在这行代码中包含错误:

  for (let i in this) {

原始脚本第 2 部分:

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

未保存,因为它Syntax Error在这行代码中包含 a:

  const trs = ScriptApp.getProjectTriggers().map(t => t.getHandlerFunction());

标签: google-apps-scriptgoogle-sheets

解决方案


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)]);
}

推荐阅读