0

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:

Unsuccessful execution when started by time trigger

Another example of an unsuccessful execution when started by 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):

Sucessful execution when started manually

pxc
  • 141
  • 1
  • 2
  • 6
  • 1
    Please provide [mcve] I'd prefer to see the problem for myself rather than take your word for it. – Cooper Aug 01 '23 at 21:33
  • 1
    I have to apologize for my poor English skill. Unfortunately, I cannot imagine your situation of `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.`. Can I ask you about the detail of it? First, I would like to correctly understand your question. – Tanaike Aug 01 '23 at 23:18
  • Upon checking on [**Google's Quotas docs**](https://developers.google.com/apps-script/guides/services/quotas) it says [*"...It most commonly occurs for scripts that run on a trigger, which have a lower daily limit than scripts executed manually."*](https://imgur.com/a/I0zMp3I) which maybe related to execution time error. On your script, you've got multiple nested **`for`** loops with multiple API calls such as `.getNumChildren()`, `.copy()` & more. You may probably need to refactor your script & use a more efficient logic via [*Batching*](https://stackoverflow.com/a/29800759/15384825) for e.g. – SputnikDrunk2 Aug 07 '23 at 16:36

0 Answers0