简体   繁体   English

google apps脚本json解析错误

[英]google apps script json parse error

I've run into a small issue with my script. 我的脚本遇到了一个小问题。

I'm trying to fetch a json url and get the data from the json file into google spreadsheet. 我正在尝试获取json网址,并将json文件中的数据获取到Google电子表格中。 Previously I've used the =importData() function in Spreadsheet. 以前,我在Spreadsheet中使用了= importData()函数。 However, I noticed the function is sometimes unreliable and returns #N/A. 但是,我注意到该函数有时不可靠,并返回#N / A。 I want to create a script that I can schedule to run when I want it to. 我想创建一个脚本,可以计划在需要时运行该脚本。

There must be a mistake somewhere in the code? 代码中某处肯定有错误? Can you guys help me? 你们能帮我吗? The Error I get is: 我得到的错误是:

The coordinates or dimensions of the range are invalid. 范围的坐标或尺寸无效。 (line 25, file "Code") (第25行,文件“代码”)

Here's the link to the google spreadsheet file: https://docs.google.com/spreadsheets/d/15xQy8GDyHRMZXBPcnmSLYUl6GSHZLpTkfhvYR7-BjvQ/edit?usp=sharing 这是指向Google电子表格文件的链接: https : //docs.google.com/spreadsheets/d/15xQy8GDyHRMZXBPcnmSLYUl6GSHZLpTkfhvYR7-BjvQ/edit?usp=sharing

On Sheet3 is an example of what is looks like with the =importData() function. 在Sheet3上是= importData()函数的外观示例。 And also what I hope it would look like with this custom script. 以及我希望此自定义脚本的外观如何。

function FetchUrl() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()

  var url = sheet[0].getRange(1, 2).getValue();
  var response = UrlFetchApp.fetch(url);
  var json = response.getContentText();
  var dataAll = JSON.parse(json);
  Logger.log(dataAll);
  var dataset = dataAll;

  var rows = [],
      data;

  for (i = 0; i < dataset.lenght; i++){
    data = dataset[i];
    rows.push([data.title, data.fields, data.values]);
  }
  dataRange = sheet[1].getRange(2, 1, rows.length, 3);
  dataRange.setValues(rows);

}

Logger.log output Logger.log输出

[17-01-16 02:34:38:354 PST] {types=[1082, 20, 20, 20, 20, 20, 20, 20], type_names=[unknown, integer, integer, integer, integer, integer, integer, integer], values=[[2017-01-15, 3, 1, 3, 0, 2, 0, 0]], title=Course's all topics' starts yesterday: Get that job (en, -NG), fields=[date, findyourdreamjob, bethebestjobseeker, writethebestcv, findthebestjobs, getajobinterview, excelatjobinterviews, firstdaysonthejob]}

Thanks in advance! 提前致谢!

If you add the following line of code somewhere after you have declared 'var dataset =' Logger.log('dataset.length=' + dataset.length); 如果在声明了“ var数据集=”之后在某处添加以下代码,则Logger.log('dataset.length ='+ dataset.length);

running and inspecting Logger.log shows that the dataset.length is undefined. 运行并检查Logger.log时,发现dataset.length未定义。 (the output of the JSON.parse is an object, and so dataset is an object and not an array) (JSON.parse的输出是一个对象,因此数据集是一个对象而不是数组)

Thus furtherdown your script, the for loop will run zero times, since the length is undefined. 因此,由于未定义长度,因此进一步深入脚本,for循环将运行零次。

so your row output remains the empty array [] and hence your error message (I suppose, but I can't be sure this is the only reason). 因此您的行输出仍为空数组[],因此仍然显示错误消息(我想,但我不能确定这是唯一的原因)。

BTW, you have spelt length incorrectly. 顺便说一句,您拼写的长度错误。 But this does not give an error since the loop is skipped (again, I suppose). 但这不会产生错误,因为跳过了循环(再次,我想是)。

The following script give the result as per sheet 3, for your comparison. 下面的脚本按照表3给出结果,供您比较。

function FetchUrl2() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()

  var url = sheet[0].getRange(1, 2).getValue();
  var response = UrlFetchApp.fetch(url);
  var json = response.getContentText();
  var dataAll = JSON.parse(json);
  Logger.log(dataAll);  // inspecting through Logger.log shows dataAll is an object not an array.  So, in the lines below, the headers and values are extracted from this object and recreated into an array.

  var headers = dataAll.fields; 
  var values = dataAll.values[0]; 
  var output = [headers,values];
  Logger.log(output)
  var dataRange = sheet[1].getRange(2,1,output.length, output[0].length);
  dataRange.setValues(output);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM