cm_ cm_ - 14 days ago 9
JSON Question

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

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.

Answer

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.