Home > OS >  Loop in Google App Script, loop stops, loop not complete all
Loop in Google App Script, loop stops, loop not complete all

Time:07-22

First, I have to apologize for my poor English skill I am using Google Apps Script

I have a Google Sheet where column A has 50 link Url (50 rows) my below script works absolutely fine when it run demo.

However, the problem is that my loop stops but no error message when i set my trigger run 5 minutes/time and check history execution times.

The loop does not complete all 50 times, sometimes once, sometimes 49 times.

I expect it to be running till the last row (50) regardless of the result. Can you help?

My Script

async function getJSON() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
const data = sheet.getRange("A1:A50").getValues()

for (var i = 1; i <=data.length; i  ) {
  try {
  console.log(i)
const url = sheet.getRange(i, 1).getValue()    
const content = await UrlFetchApp.fetch(url).getContentText("UTF-8");
const obj = JSON.parse(content);
 ....
  ...

} catch (e) 
 { continue;
}
}
}

CodePudding user response:

The point I was trying to make is if you do not include a console.log() or Logger.log() in your catch block you wouldn't know if or what error occurs.

Script without console.log()

function test() {
  let a = 1;
  for( let i=0; i<5; i   ) {
    try {
      console.log(a/(b-i));
    }
    catch(err) {
      continue;
    }
  }
}

Execution log

7:43:21 AM  Notice  Execution started
7:43:22 AM  Notice  Execution completed

Script with console.log()

function test() {
  let a = 1;
  for( let i=0; i<5; i   ) {
    try {
      console.log(a/(b-i));
    }
    catch(err) {
      console.log(err)
      continue;
    }
  }
}

Execution log

7:45:17 AM  Notice  Execution started
7:45:18 AM  Info    [ReferenceError: b is not defined]
7:45:18 AM  Info    [ReferenceError: b is not defined]
7:45:18 AM  Info    [ReferenceError: b is not defined]
7:45:18 AM  Info    [ReferenceError: b is not defined]
7:45:18 AM  Info    [ReferenceError: b is not defined]
7:45:17 AM  Notice  Execution completed
  • Related