2

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);
}
Alan Wells
  • 30,746
  • 15
  • 104
  • 152
Simon Breton
  • 2,638
  • 7
  • 50
  • 105
  • 1
    What line is the error coming from? Look in the Execution Transcript under the View menu. Put a 'Logger.log('scraped: ' + scraped)` statement right after the `scraped` line, then run the code and VIEW the LOGS. There is no `Parser` class in Apps Script. What is that? – Alan Wells Sep 30 '16 at 00:14
  • `Attribute provided with no value: url (line 10, file "Code")`. Here is the line10 : `var content = UrlFetchApp.fetch(url).getContentText();` I guess I has nothing to do with my urls.length... – Simon Breton Sep 30 '16 at 00:21
  • 2
    You might run `Logger.log(url)` and see if you're getting valid URLs. – Tom Woodward Sep 30 '16 at 02:03

2 Answers2

2

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.

Alan Wells
  • 30,746
  • 15
  • 104
  • 152
0

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

Dal
  • 165
  • 10