I've been trying to figure out how to get the UrlFetchApp.fetch method to work correctly on an array of 100+ url's that I need it to fetch for a script. I either hit error 429's (too many requests), or if I put any kind of sleep between iterations the amount of time the URLFetchApp takes is consistently increasing until each call takes 8+ seconds and I hit the timeout limit of 6 minutes. Here is my code as it stands right now. Any input would be appreciated.
function migrateSheetsToFiles() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssId = ss.getId();
var parentfolder = DriveApp.getFileById(ssId).getParents().next();
var mySheets = ss.getSheets();
var fileID = ss.getId();
var FetchArray = [];
var sheetnameArray = [];
var responseArray = [];
var params = {
method : "GET",
headers : {"authorization": "Bearer " + ScriptApp.getOAuthToken()}
};
for(var i=0; i<mySheets.length; i++){
var currentSheet = mySheets[i];
var sheetname = currentSheet.getName();
sheetnameArray[i] = sheetname;
var currentSheetId = currentSheet.getSheetId();
var url = "https://docs.google.com/spreadsheets/d/" + fileID + "/export?format=xlsx&gid=" + currentSheetId;
FetchArray[i] = url;
}
for (i=0; i<FetchArray.length; i++) {
responseArray = UrlFetchApp.fetch(FetchArray[i], params);
Utilities.sleep(1000);
}
for (i=0; i<FetchArray.length; i++) {
parentfolder.createFile(responseArray[i]).setName(sheetnameArray[i]);
}
}