电子表格图表功能无权限

Spreadsheet chart function no permission

我正在查看文档 here

图表下有一些在 GAS 中生成图表的示例代码:

function newChart() {
      // Generate a chart representing the data in the range of A1:B15.
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheets()[0];

      var chart = sheet.newChart()
         .setChartType(Charts.ChartType.BAR)
         .addRange(sheet.getRange('A1:B15'))
         .setPosition(5, 5, 0, 0)
         .build();

      sheet.insertChart(chart);
    }

我在 A1:B15 范围内为活动 sheet 添加了虚拟数据。然后,在单元格 D2 中输入 =newChart()

错误是 returned "You do not have permission to call insertChart (line 12)." 我用谷歌搜索了那个错误并找到了 this 文档。里面有一句话"If your custom function throws the error message You do not have permission to call X service., the service requires user authorization and thus cannot be used in a custom function."

示例也来自文档。

  1. 为什么文档会向我提供一个不适合自定义函数的示例?
  2. 我如何获得 "work" - return 图表的功能?

在电子表格中,转到“工具”并选择“脚本编辑器”。在这里单击 运行 菜单并从下拉列表中选择 newChart。授权脚本,公式应该有效。