I have been working on importing a large amount of API data into a Google Sheet to build up a database. I have a list of over 25,000 links in cells inside of a sheet that I am looking to import into the data. I have a working set of code in Apps Script that has been able to quickly and efficiently import ten links, but when I have tried the full set, I have gotten errors like:
Exception: Service invoked too many times in a short time: urlfetch. Try Utilities.sleep(1000) between calls."
This will show up eleven seconds after running the code. I have also tried adding Utilities.sleep(1000)
to the code in many places to no avail. Here is the code that I am working with:
function dataImport() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Import1");
var countRow = 1;
var exportSheet = ss.getSheetByName("Data1");
var reqs = sheet.getRange("A2:A" + sheet.getLastRow()).getDisplayValues().reduce((ar, [url]) => {
if (url) {
ar.push({ url, muteHttpExceptions: true });
}
return ar;
}, []);
var responses = UrlFetchApp.fetchAll(reqs);
Utilities.sleep(1000)
var temp = responses.reduce((ar, r) => {
if (r.getResponseCode() == 200) {
var { id, firstName, lastName, fullName, displayName, shortName, weight, height, position: { abbreviation }, team: { $ref }, birthPlace: { city }, birthPlace: { state, country }, experience: { years, displayValue }, jersey, active } = JSON.parse(r.getContentText());
ar.push([id, firstName, lastName, fullName, displayName, shortName, weight, height, abbreviation, $ref, city, state, country, years, displayValue, jersey, active]);
}
return ar;
}, []);
var res = [['IDs', 'First Name', 'Last Name', 'Full Name', 'Display Name', 'Short Name', 'Weight', 'Height', 'Position', 'Team URL', 'City', 'State', 'Country', 'Years', 'Class', 'jersey', 'Active'], ...temp];
exportSheet.getRange(countRow, 1, res.length, res[0].length).setValues(res);
}
I had previously posted a question about this database as I was running into a different issue (Apps Script API Import via Links in Google Sheet Cells but Map Properties Undefined), then when the quantity became an issue, it was suggested that I post a new question to help others who may be running into this issue. Once again, this code has been able to work with a limited dataset; it just seems like the very large quantity is causing many issues.
Like I mentioned in the comments of the previous question, I'll mention it here. When I tried adding the Utilities.sleep(1000)
command, most of the time I ran into the same error never the less, except in one location, to which I have not been able to replicate, so my apologies, I got this error:
Exception: Service Spreadsheets failed while accessing document with id 18tRBBcVwChuSJ0G5_A09I7EoQnji6nmRvOWcX5ndALE
Upon researching the original error, I found a few questions relating to it, but none of them worked for what I was looking to do:
How do I use Google app script UrlFetchApp.fetchAll on >1000 requests without getting "Service invoked too many times in a short time" error?: This one featured a looping set of code, but it was being run as a Sheets Extension, and doesn't match how my code is importing the data. This question, while it got comments, didn't receive an answer that worked for the person asking. They also didn't try the command the error code suggested.
How do I fetch a column of URLs in Google Apps Script/Javascript?: This deals with a far smaller base dataset, more similar to the test run in my previous question, where it was suggested to use a FetchAll
command, which is currently how my code is setup and isn't working with the larger set of data.
At the end of this, I'll have the link to a copy sheet with all of the link data in case that is helpful for anyone. I also have put the code I have been using in the AppScript of that sheet. If I could get help figuring out the best way of doing this, it would be greatly appreciated!