“范围的坐标或尺寸无效". [英] "The coordinates or dimensions of the range are invalid"
问题描述
我正在研究与REST API链接的Google Apps脚本,并将数据放入Google表格中.
I am working on a Google Apps Script that links with a REST API and puts the data into a Google Sheet.
我已经成功完成了一次,但是访问一些不同的数据后我得到了错误消息
I have successfully done this once, but upon accessing some different data I get the error message
范围的坐标或尺寸无效"
"The coordinates or dimensions of the range are invalid"
当它们在我的其他脚本上可以正常工作时.访问的所有数据都是JSON,所以我有点困惑,并且来自同一来源.我正在使用的代码是:
when they work perfectly fine on my other script. All data accessed is JSON so I am bit confused and is from the same source. The code I am using is:
function stats () {
var logIn = {
"Authorization" : "Basic " + Utilities.base64Encode("XXXX" + ':' + "XXXX")
};
var url = "XXXXX";
var params = {
"method":"GET",
"headers":logIn, };
var response = UrlFetchApp.fetch(url, params);
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("XXXX");
var dataAll = JSON.parse(response.getContentText()); //
var dataSet = dataAll;
var rows = [],
data;
for (i = 0; i < dataSet.length; i++) {
data = dataSet[i];
rows.push([XXXX]); //your JSON entities here
}
dataRange = sheet.getRange(1, 1, rows.length, 1);
dataRange.setValues(rows);
}
我已经组合了网上的代码,并且可以在其他脚本上使用.错误出现在此行:
I have combined pieces of code from around the web and this works on my other script. The error appears on this line:
dataRange = sheet.getRange(1, 1, rows.length, 1);
我认为问题出在我正在访问的数据上,但是我不知道如何更改脚本使其正常工作.
I believe the issue is with the data I am accessing but I do not know how to alter the script for it to work.
有效的JSON数据如下所示:
The JSON data that works is shown like:
{
id: XXX,
group: XX,
text: "XXXX?",
creation_date: XXXX,
created_by: "XXXXX",
tags: [
"XXXX"
]
}
导致错误的数据显示为:
And the data that is causing the error is shown as:
{
2016-02-29: {
XXX: 0,
XXX: 0
},
我不得不对很多私人信息"XXXX"进行道歉".任何帮助,将不胜感激.
I have had to 'XXXX' out a lot of the private information - apologies. Any help would be appreciated.
推荐答案
JavaScript的length属性适用于索引数组,不适用于对象,因此dataSet.length
返回undefined
并且循环永远不会执行.
Javascript's length property is for indexed arrays and does not apply to Objects so dataSet.length
returns undefined
and the loop never executes.
要获取对象的长度,可以按此处所述使用Object.keys(dataSet).length
>.
To get the length of the object you can use Object.keys(dataSet).length
as outlined here.
这篇关于“范围的坐标或尺寸无效".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!