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. Previously I've used the =importData() function in Spreadsheet. However, I noticed the function is sometimes unreliable and returns #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")
Here's the link to the google spreadsheet file: 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. 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
[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!