“范围的坐标或尺寸是无效的” - Google Apps脚本和JSON数据 [英] "The coordinates or dimensions of the range are invalid" - Google Apps Script and JSON Data

查看:184
本文介绍了“范围的坐标或尺寸是无效的” - Google Apps脚本和JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个与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.

我已经成功完成了一次,访问一些不同的数据当我的另一个脚本完全正常工作时,我收到错误消息范围的坐标或尺寸无效。所有访问的数据都是JSON,所以我有点困惑并且来自同一个源。我使用的代码是:

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" 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);

}

我已经将来自网络和这个在我的另一个剧本上工作。该错误出现在说:dataRange = sheet.getRange(1,1,rows.length,1);

I have combined pieces of code from around the web and this works on my other script. The error appears on the line that says: " 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属性适用于索引数组,并且不适用于Object,所以 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.

这篇关于“范围的坐标或尺寸是无效的” - Google Apps脚本和JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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