No Permission to call setValues() - Google Apps Script, JSON data [duplicate]

时光毁灭记忆、已成空白 提交于 2019-12-25 17:13:04

问题


This is a Google Apps Script that pulls JSON data into Google Sheets.

The code is:

function pullJSON() {

  var url="https://api.myjson.com/bins/4610d"; // publicly available json

  var response = UrlFetchApp.fetch(url); // 
  var sheet =  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");

  var dataAll = JSON.parse(response.getContentText()); 
  var dataSet = dataAll;

  var rows = [],
      data;

  for (i = 0; i < Object.keys(dataSet).length; i++) {
    data = dataSet[Object.keys(dataSet)[i]];
    rows.push([data.name]); //JSON entities here
  }

  dataRange = sheet.getRange(1, 1, rows.length, 1); 
  dataRange.setValues(rows);
}

In Google Sheets, I called the function by entering =pullJSON() into a cell. It shows the error:

"You do not have permission to call setValues(line 20)" which is referring to the line : dataRange.setValues(rows);

and produces "undefined" result in Cells A1,A2,A3.

The JSON data source is simply this:

{"id":1,"name":"A green door","price":12.5}

The purpose of doing these is to be able to convert the JSON data into table form in google sheets.

Much of the above closely followed the content in this thread: "The coordinates or dimensions of the range are invalid" - Google Apps Script and JSON Data

Will be very grateful for any guidance on this question please.


回答1:


A custom function cannot affect cells other than those it returns a value to. In other words, a custom function cannot edit arbitrary cells, only the cells it is called from and their adjacent cells. To edit arbitrary cells, use a custom menu to run a function instead.

from the documentation: https://developers.google.com/apps-script/guides/sheets/functions

I don't know your exact use-case but it looks like it would be possible to run your function from a custom menu.



来源:https://stackoverflow.com/questions/40806930/no-permission-to-call-setvalues-google-apps-script-json-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!