Simon Breton Simon Breton - 2 months ago 12
Javascript Question

error : "Cannot convert Array to Object" Data Scraping script

I'm new to Javascript and even newer to google script, so please be comprehensive :)

I'm trying to build a little script to scrap some data from a bunch of URL. I' using

Parser library
. Here is what I've:

function getArray() {
var newData = new Array();
var sheet = SpreadsheetApp.openById('my_id').getSheetByName('Sheet4');
var urls = sheet.getRange(1,1,5,5).getValues();
var fromText = '<span class="nb-shares">';
var toText = '</span>';

for(i in urls){
var url = urls[i];
var content = UrlFetchApp.fetch(url).getContentText();
var scraped = Parser
.data(content)
.from(fromText)
.to(toText)
.build();
newData.push(scraped);}
var sheet2 = SpreadsheetApp.openById('my_id').getSheetByName('Sheet5');
sheet2.getRange(5, 1, newData.length, newData[1].length).setValues(newData);
}


It return me the following error :
Cannot convert Array to Object


What I'm trying to do is looping on an URLs array so I can scrap some data from each one of these URL and return the results in my sheet.

Answer

Try changing newData.push(scraped) to newData.push([scraped])