I must be having a misunderstanding as to how this method works. I am trying to create an add-on that copies a large number of templates then fill placeholders in the templates. There are about 6 fields it must replace for each template but this may increase depending on the office's needs. It works on a small number of templates but if I try to apply the data to 20 or so copied templates, I get timed out with the error.
"Exception: Service invoked too many times in a short time: urlfetch. Try Utilities.sleep(1000) between calls."
My goal is to scale this up to be able to handle 200-300 templates. I was under the impression that using fetchAll on an array of requests would count as a single request. Is this wrong? I can certainly add a timer but if it triggers with multiple requests, I worry I will time out on Google's script runtime quota.
function copyTemplates(copiedTemplateIds, destinationFolderId) {
let copyRequests = []
copiedTemplateIds.forEach(id => {
let title = getTitle(enteredInfo.name, DriveApp.getFileById(id).getName())
copyRequests.push({
"url": `https://www.googleapis.com/drive/v3/files/${id}/copy`,
"method": "POST",
"headers": {
"Authorization": "Bearer " + token
},
"payload": `{"parents":"${destinationFolderId}","name":"${title}"}`,
"contentType": "application/json",
})
})
let responses = UrlFetchApp.fetchAll(copyRequests);
responses.forEach(response => {
copiedTemplateIds.push(JSON.parse(response.getContentText()).id)
});
return copiedTemplateIds;
}
function replaceText(copiedTemplateIds) {
let fillRequests = []
copiedTemplateIds.forEach(id => {
for (let [field, data] of Object.entries(enteredInfo)) {
let fill = "<<" + field + ">>"
fillRequests.push({
"url": `https://docs.googleapis.com/v1/documents/${id}:batchUpdate`,
"method": "POST",
"headers": {
"Authorization": "Bearer " + token
},
"payload": `{"requests":[{"replaceAllText":{"replaceText":"${data}","containsText":{"text":"${fill}","matchCase":false}}}]}`,
"contentType": "application/json",
})
}
})
let responses = UrlFetchApp.fetchAll(fillRequests);
console.log(responses.toString())
}
Here is the code for building and sending the requests. I thought this would result in 2 total requests. It seems as though, if I'm timing out, each request in the fetchAll counts as an individual request? If that's the case, what's the advantage of fetchAll vs fetch? I feel like I'm missing something here and would appreciate any insight.