I wrote a small script in Google Apps to process some 200+ files stored in my Google Drive. The processing consists basically in copying them all, one by one, to a master file, stored also in my Google Drive, and perform some basic formatting.
I notice that when the script is executed on demand (I set it to run manually) it takes less than around 10 seconds to process batches of 20 files each, and thus finishes successfully largely below the limit of 360 seconds per execution set by Google.
However, when it is executed from a time trigger, set for one hour intervals, it always exceeds the limit of 360 seconds to process just around 100 to 180 files (it depends on the time of day?) and therefore does not finish successfully.
The script should take the same time to finish in both cases. Google is clearly assigning different priorities to the script execution, depending on whether it is run "on demand" or based on a time trigger.
I read that there are some workarounds to solve the 360 seconds time limit but, as I see it, my script is very much able to run in less time than that. Can anyone help me on this?
Edit 2023-08-02 with additional information
I wrote the script as a standalone one, using samples of code I found through searches in the internet, the relevant part of which is shown below. The makeDoc
function takes two arguments, one Google Drive file ID and an object containing the list of files to be processed (among other things), opens the main file where the other ones are to be copied into and then processes all of them one by one:
function makeDoc(mainDocID, rowsIDs) {
var mainDoc = DocumentApp.openById(mainDocID);
var body = mainDoc.getBody();
// This sequence of clear, save and close, and reopen is required to avoid 'ScriptError: Too many changes applied before saving document'.
// https://stackoverflow.com/questions/50875601/how-do-you-re-open-the-active-document-after-calling-saveandclose
body.clear(); // clear the whole document
mainDoc.saveAndClose(); // save and close the document
mainDoc = DocumentApp.openById(mainDocID); // reopen the document
body = mainDoc.getBody();
// extract data from object
var header = rowsIDs.header;
var values = rowsIDs.rows; // TOC list
var docIDs = rowsIDs.ids; // atoms list
// start writing the file
body.appendParagraph(header).setBold(true);
body.appendParagraph('').setBold(false);
// body.appendTable(values);
// append atoms from database
for (var i = 0; i < values.length; i++) {
// body.appendPageBreak();
var otherBody = DocumentApp.openById(docIDs[i]).getActiveSection();
var totalElements = otherBody.getNumChildren();
var isHeader = true;
if (i%20 == 19) { // progress indicator
Logger.log('Processados '+Utilities.formatString('%03d', i+1)+' cânticos');
}
for (var j = 0; j < totalElements; ++j) {
var element = otherBody.getChild(j).copy();
switch (element.getType()) {
case DocumentApp.ElementType.PARAGRAPH :
if (isHeader) {
element.insertText(0, Utilities.formatString('%03d', i+1)+'. ').setBold(true);
isHeader = false;
}
body.appendParagraph(element);
break;
case DocumentApp.ElementType.TABLE :
body.appendTable(element);
break;
case DocumentApp.ElementType.LIST_ITEM :
body.appendListItem(element).setGlyphType(DocumentApp.GlyphType.NUMBER);
break;
default:
throw new Error('Unknown element type: ' + element.getType())
}
}
}
mainDoc.saveAndClose(); // close the file
}
Here are two examples of the execution log when the script is started by a time trigger:
Here is an example of a successful execution when the script is started manually (I erased the second to last line from the log for privacy reasons):