在Sheets AppScript函数中调用insertImage的权限

huangapple go评论75阅读模式
英文:

Permission to call insertImage in a Sheets AppScript function

问题

异常:您没有调用insertImage的权限(第597行)。

以下是我的Code.gs:

function testChart() {
  var data = Charts.newDataTable()
    .addColumn(Charts.ColumnType.STRING, "年份")
    .addColumn(Charts.ColumnType.NUMBER, "销售额")
    .addColumn(Charts.ColumnType.NUMBER, "支出")
    .addRow(["2020", 1000, 500])
    .addRow(["2021", 2000, 1000])
    .addRow(["2022", 4000, 2000])
    .build();

  var chart = Charts.newColumnChart()
    .setTitle('销售额与支出')
    .setXAxisTitle('年份')
    .setYAxisTitle('金额(美元)')
    .setDimensions(600, 500)
    .setDataTable(data)
    .build();

  SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().insertImage(chart.getAs('image/png'), 1, 1);
}

以下是我的appsscript.json:

{
  "timeZone": "America/Chicago",
  "dependencies": {},
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "oauthScopes": [
    "https://www.googleapis.com/auth/script.container.ui",
    "https://www.googleapis.com/auth/spreadsheets.currentonly"
  ]
}

这里有什么我遗漏的吗?

英文:

I'm running into a problem when trying to insert a ColumnChart into a Google Sheet document as an image via an AppsScript. The error is:

> Exception: You do not have permission to call insertImage (line 597).

Here is my Code.gs:

function testChart() {
  var data = Charts.newDataTable()
    .addColumn(Charts.ColumnType.STRING, "Year")
    .addColumn(Charts.ColumnType.NUMBER, "Sales")
    .addColumn(Charts.ColumnType.NUMBER, "Expenses")
    .addRow(["2020", 1000, 500])
    .addRow(["2021", 2000, 1000])
    .addRow(["2022", 4000, 2000])
    .build();

  var chart = Charts.newColumnChart()
    .setTitle('Sales & Expenses')
    .setXAxisTitle('Year')
    .setYAxisTitle('Amount (USD)')
    .setDimensions(600, 500)
    .setDataTable(data)
    .build();

  SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().insertImage(chart.getAs('image/png'), 1, 1);
}

And here is my appsscript.json:

{
  "timeZone": "America/Chicago",
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "oauthScopes": [
    "https://www.googleapis.com/auth/script.container.ui",
    "https://www.googleapis.com/auth/spreadsheets.currentonly"
  ]
}

Is there something here I'm missing?

答案1

得分: 2

自定义功能无法调用需要授权的服务。正如Tanaike在评论中建议的那样,您将不得不通过其他方式调用该功能,例如通过按钮、自定义菜单项、侧边栏或触发器。

请查看支持自定义功能的服务

英文:

An Apps Script custom function cannot call services that require authorization. As Tanaike suggested in the comments, you will have to call the function through some other means, such as a button, a custom menu item, a sidebar or a trigger.

See Custom function supported services.

huangapple
  • 本文由 发表于 2023年1月9日 07:03:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定