Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UrlFetchApp.fetch throws Heavy traffic | Timeout error

While downloading the pdf blob in google drive with UrlFetchApp.fetch method is causing two type of errors:

  1. </div></div>This file might be unavailable right now due to heavy traffic. <a href="">Try again</a>.</div> [Written in downloaded PDF]

  2. Exception: Timeout

Code Snippet:

function downloadasPDF(optSSId, optSheetId)
{
 var ss = (optSSId) ? SpreadsheetApp.openById(optSSId) : SpreadsheetApp.getActiveSpreadsheet();

 var preURL=ss.getUrl() //ss is an spreadsheet reference
 var url = preURL.replace(/edit.*/,'');
 var folder = DriveApp.getFolderById(FolderID);
 // Get array of all sheets in spreadsheet
 var sheets = ss.getSheets();

 for (var i=0; i<sheets.length; i++) {
   //Sheet length is 100+

   Utilities.sleep("5000")
   var sheet = sheets[i];

   // If provided a optSheetId, only save it.
   if (optSheetId && optSheetId !== sheet.getSheetId()) continue; 

   //additional parameters for exporting the sheet as a pdf
   var url_ext = 'export?exportFormat=pdf&format=pdf'   //export as pdf
     + '&gid=' + sheet.getSheetId()   //the sheet's Id
     + '&gridlines=false'  // hide gridlines

   var options = {
     headers: {
       'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken()
     },
     muteHttpExceptions: true,
   }
   var response = UrlFetchApp.fetch(url + url_ext, options);
   var blob = response.getBlob().setName(spreadsheet.getName() + ' - ' + sheet.getName() + '.pdf');
   folder.createFile(blob);
}

To counter above problem I am using:

Utilities.sleep(5000)

But still some files are causing error 1 mentioned above.

Question: Do we have any other better approach to handle two mentioned cases apart from sleep ?

Note: I am using G Suite Enterprise, Number of sheets to download are between 100-150 approx, 240 cells filled for each sheet & rest cells are empty.

like image 419
Abin Lakhanpal Avatar asked Jan 30 '26 10:01

Abin Lakhanpal


1 Answers

Use a exponential back off function to sleep exponentially on failure. Failure can be checked with .getResponseCode():

const response = (function exponentialBackoff(i) {
  Utilities.sleep(Math.pow(2, i) * 1000);
  const data = UrlFetchApp.fetch(url + url_ext, options);
  if (data.getResponseCode() !== 200) return exponentialBackoff(++i);
  else return data;
})(0);
like image 149
TheMaster Avatar answered Feb 01 '26 03:02

TheMaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!