Simon Breton
Simon Breton

Reputation: 2876

"Attribute provided with no value" error - UrlFetchApp

I have the following error :

Attribute provided with no value

The error is from line:

var content = UrlFetchApp.fetch(url).getContentText();

Here is my code :

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

  for(var i = 0; i < urls.length; i++){
    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(1, 1, newData.length, newData[1].length).setValues(newData);
}

Upvotes: 2

Views: 5075

Answers (2)

Alan Wells
Alan Wells

Reputation: 31300

The getValues() method returns a 2 dimensional array. You are not getting the value out of the inner array.

Currently:

var url = urls[i];

Should be:

var url = urls[i][0];

You need to add the [0] index for the inner array.

Upvotes: 2

Dal
Dal

Reputation: 164

If your suspicion is that urls is not a proper array than you can just console.log(urls) after you define it and see if it is an array or something else.

Or test like this console.log(Array.isArray(urls))

Upvotes: 0

Related Questions