GAS Dynamic Chart 轴最小值和最大值,找不到错误

GAS Dynamic Chart axis min and max, cant find the error

为任何想要测试的人测试电子表格 https://docs.google.com/spreadsheets/d/1Q3z3hOuiixp7mWl0GUcN9Rc6HU1dS7ShJ996OyHugR0/edit?usp=sharing

编辑:我安装了一个 On Edit Trigger,现在它可以工作了,但是执行日志现在每次触发时都会反映 2 次执行,一次失败,一次成功,知道这是否正常吗?如果不是我该怎么办?

失败的执行说; 例外:您没有调用 SpreadsheetApp.openById 的权限。所需权限:https://www.googleapis.com/auth/spreadsheets 在 onEdit(onEdits:4:28)

我有一个包含 4 个图表的电子表格(只是折线图) 每个图表根据下拉列表更改数据,我还从旁边几个单元格中的数据计算我想要的最小值和最大值,代码基本上只是在我编辑单元格时尝试更新图表最小值和最大值使用下拉菜单,但我找不到错误在哪里。

function onEdit(e) {

  //run dynamic charts switch-case function
  const ss= SpreadsheetApp.openById("Sheet-ID");
  const sheet = ss.getSheetByName("Analysis2.0");
  var range = e.range;
  var target = range.getA1Notation();
  const charts = sheet.getCharts();


  for (var i in charts) {
    let chart = charts[i];
    const chartName = chart.getOptions().get("title");

    if (target == "H18") {
      switch (chartName) {
        case "#0 Dynamic e1RM Chart":
          Utilities.sleep(200);
          var vaxis0min = sheet.getRange("E19").getValue();
          var vaxis0max = sheet.getRange("F19").getValue();
          chart = chart.modify()
            .setOption('vAxis', { minValue: vaxis0min })
            .setOption('vAxis', { maxValue: vaxis0max })
            .build();
          sheet.updateChart(chart);
          break;
      }
    }
    if (target == "X18") {
          switch (chartName) {
            case "#1 Dynamic e1RM Chart":
              Utilities.sleep(200);
              var vaxis1min = sheet.getRange("U19").getValue();
              var vaxis1max = sheet.getRange("V19").getValue();
              chart = chart.modify()
                .setOption('vAxis', { minValue: vaxis1min })
                .setOption('vAxis', { maxValue: vaxis1max })
                .build();
              sheet.updateChart(chart);
              break;
          }
        }
    if (target == "AN18") {
          switch (chartName) {
            case "#2 Dynamic e1RM Chart":
              Utilities.sleep(200);
              var vaxis2min = sheet.getRange("AK19").getValue();
              var vaxis2max = sheet.getRange("AL19").getValue();
              chart = chart.modify()
                .setOption('vAxis', { minValue: vaxis2min })
                .setOption('vAxis', { maxValue: vaxis2max })
                .build();
              sheet.updateChart(chart);
              break;
          }
        }
    if (target == "BD8") {
          switch (chartName) {
            case "#3 Dynamic e1RM Chart":
              var vaxis3min = sheet.getRange("BA19").getValue();
              var vaxis3max = sheet.getRange("BB19").getValue();
              chart = chart.modify()
                .setOption('colors', ['#990000'])
                .setOption('vAxis', { minValue: vaxis3min })
                .setOption('vAxis', { maxValue: vaxis3max })
                .build();
              sheet.updateChart(chart);
              break;
          }
    }
  }
}

问题:

I installed an On Edit Trigger and now it works however the executions log now reflects 2 executions every time it triggers, one failed and one successful, any idea if this is normal? if not what should I do?

the failed execution says; Exception: You do not have permission to call SpreadsheetApp.openById. Required permissions: https://www.googleapis.com/auth/spreadsheets at onEdit(onEdits:4:28)

简单触发器无法访问需要身份验证的服务。由于 SpreadsheetApp.openById 需要身份验证,因此函数失败。

解决方案:

如果您的脚本绑定到电子表格,请将 SpreadsheetApp.openById 替换为 SpreadsheetApp.getActive(),这样脚本就不需要对当前文档之外的访问进行身份验证。

否则,只需删除简单触发器(重命名您的函数,以便只有安装的触发器运行 - 您可能需要在更改函数名称后重新安装触发器)。

参考: