While downloading the pdf blob in google drive with UrlFetchApp.fetch
method is causing two type of errors:
</div></div>This file might be unavailable right now due to heavy traffic. <a href="">Try again</a>.</div> [Written in downloaded PDF]
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.