UrlFetchApp.fetch will abor the script?

25 views
Skip to first unread message

Alan C

unread,
Oct 2, 2020, 11:12:44 PM10/2/20
to Google Apps Script Community
Hi,

I create a spreadsheet with 3 sheets, "Links", "Valid Links" and "Invalid links", then use the following code to check each row in the "Links" sheet, as below:

function myFunction() {
  var rows = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Links").getDataRange().getValues();
  
  rows.forEach(function(row, index) 
  {
    if (index !== 0) 
    { 
      var url = row[1];
      var page = UrlFetchApp.fetch(url).getContentText();
      var number = page.match("sample.com");
      
      if (!number)
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Valid Links").appendRow(url);
      else
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Invalid Links").appendRow(url);
    }
  });
}

However, when I debug to UrlFetchApp.fetch(url), the script suddenly aborts. No error or exception are shown. Why?

Alan C

unread,
Oct 4, 2020, 7:11:30 PM10/4/20
to Google Apps Script Community
The bug is fixed. It is caused by row[1] which I think it refers to column A but actually it refers to column B. See https://stackoverflow.com/questions/64194308/urlfetchapp-fetch-will-abort-the-script/64194545?noredirect=1#comment113526950_64194545

Thanks to all of you.

CBMServices Web

unread,
Oct 4, 2020, 8:17:33 PM10/4/20
to google-apps-sc...@googlegroups.com
Yes, array indexes start at 0 and not 1. Versus row numbers in Google spreadsheets start at 1.  So need to be careful to know whether you are dealing with an array or with the spredsheet.


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/0ad33705-0cf4-4e1e-83f8-d2cf803a4fdco%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages