无权调用setValues() - Google Apps脚本,JSON数据 [英] No Permission to call setValues() - Google Apps Script, JSON data

查看:190
本文介绍了无权调用setValues() - Google Apps脚本,JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个Google Apps脚本,可将JSON数据提取到Google表格中。



代码如下:

  function pullJSON(){

var url =https://api.myjson.com/bins/4610d; //公开可用json

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

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

var rows = [],
data; (i = 0; i< Object.keys(dataSet).length; i ++){
data = dataSet [Object.keys(dataSet)[i]]的

;
rows.push([data.name]); // JSON实体在这里
}

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

在Google表格中,我通过输入= pullJSON()细胞。它显示错误:

您没有权限调用setValues(第20行),它指向的行是:dataRange.setValues(rows);

p>

,并在单元格A1,A2,A3中产生未定义结果。



JSON数据源就是这样的:

  {id:1,name:绿色的门,价格:12.5} 

这样做的目的是能够将JSON数据转换为谷歌表格中的表格形式。



以上大部分内容都紧跟着这个主题的内容:
该范围的坐标或尺寸是无效的 - Google Apps脚本和JSON数据



非常感谢有关此问题的任何指导。

自定义函数不能影响除返回
值以外的单元格。换句话说,一个自定义函数不能编辑任意的
单元格,只能调用它调用的单元格及其相邻单元格。至
编辑任意单元格,使用自定义菜单来运行一个函数。


来自文档: https://developers.google.com/apps-script/guides/sheets/functions



我不知道您的确切用例,但它看起来可以从自定义菜单运行您的函数。


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.

解决方案

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.

这篇关于无权调用setValues() - Google Apps脚本,JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆