Search code examples
curlgoogle-apps-scripthttprequestgoogle-apps-script-api

How to know the progress status of a Google Apps script


I have created an Apps Script to perform several tasks automatically:

Function 1. - Create a new folder and retrieve the ID.
Function 2. - Create a copy of a doc file in the folder.
Function 3. - Replace the text of several tags of type {{TEXT}} in the document.
Function 4. - Insert an image in the document.
Function 5. - Retrieve the public link of the doc document.

These 5 functions are executed sequentially in the same Apps Script using a "global" function that allows me to retrieve the IDs of the folder and the Doc document created. I run this Apps Script using cURL (from the terminal).

The duration of the script is approximately 10 seconds and I would like to know if it is possible to get a message (or a text value) in response that is updated as each function is completed.

I have looked on the internet and on Stackoverflow but everything I have found refers to using the 'flush' command in a spreadsheet to update the value of a cell. What I need is that the response message in the terminal is updated.

Could someone help me, please?.

Thank you very much.

Wardiam


Solution

  • From your following functions,

    Function 1. - Create a new folder and retrieve the ID. Function 2. - Create a copy of a doc file in the folder. Function 3. - Replace the text of several tags of type {{TEXT}} in the document. Function 4. - Insert an image in the document. Function 5. - Retrieve the public link of the doc document.

    I think that in your situation, the following 3 patterns can be used.

    1. All functions are metged as one function.

      • In this case, your goal of I would like to know if it is possible to get a message (or a text value) in response that is updated as each function is completed. cannot be achieved.
      • In order to retrieve the response when each function is finished, each function is executed every time with curl command. This has already been mentioned by Rubén's comment.
    2. When new folder id created at "Function 1", the folder ID is returned from the function and the folder ID is used in "Function 2". And, when the Document is copied, the document ID is returned from the function and use it for "Function 3", "Function 4" and "Function 5".

      • In this case, the global variables are not required to be used. In order to retrieve the response, each function is executed every time with curl command. But, when each function is executed, each response value is required to be give to the next function. I thought that this might be a bit complecate.
    3. When new folder id created at "Function 1", the folder ID is put to PropertiesService, and when "Function 2" is run, the folder ID retrieved from PropertiesService is used. And, when the Document is copied, the document ID is put to PropertiesService, and when "Function 3", "Function 4" and "Function 5" are run, the document ID retrieved from PropertiesService is used.

      • In this case, the global variables are not required to be used. And, the folder ID and document ID are put and get during each function is run. By this, each function can be run in order using the curl commands. So, I thought that the script for using curl commands might become a bit simple.

    From above situation, I would like to propose the pattern 3 in my answer.

    Usage:

    Sample script:

    As the sample script for testing this pattern, the following script is used.

    // Create a new folder and retrieve the ID.
    function function1() {
      const folderId = DriveApp.createFolder("sampleFolder").getId();
      PropertiesService.getScriptProperties().setProperty("folderId", folderId);
      return "Function1: Done.";
    }
    
    // Create a copy of a doc file in the folder.
    function function2() {
      const folderId = PropertiesService.getScriptProperties().getProperty("folderId");
      const folder = DriveApp.getFolderById(folderId);
      const documentId = DocumentApp.create("sampleDocument").getId();
      DriveApp.getFileById(documentId).moveTo(folder);
      PropertiesService.getScriptProperties().setProperty("documentId", documentId);
      return "Function2: Done.";
    }
    
    function function3() {
      const documentId = PropertiesService.getScriptProperties().getProperty("documentId");
    
      // do something
    
      return "Function3: Done.";
    }
    
    function function4() {
      const documentId = PropertiesService.getScriptProperties().getProperty("documentId");
    
      // do something
    
      return "Function4: Done.";
    }
    
    function function5() {
      const documentId = PropertiesService.getScriptProperties().getProperty("documentId");
    
      // do something
    
      return "Function5: Done.";
    }
    
    • In this sample script, the folder ID and document ID are put and get during each function is run.
    • In this sample script, no error processing is included. So when you use this, please add it.

    Testing.

    In order to retrieve each response from each function, in this case, 5 curl commands are run by a script. The sample script is as follows.

    #!/bin/sh
    accessToken="###your access token###"
    url="https://script.googleapis.com/v1/scripts/###your script ID###:run"
    
    curl -X POST -H "Authorization: Bearer ${accessToken}" -H "Content-Type: application/json" -d "{\"function\": \"function1\", devMode: true}" ${url}
    curl -X POST -H "Authorization: Bearer ${accessToken}" -H "Content-Type: application/json" -d "{\"function\": \"function2\", devMode: true}" ${url}
    curl -X POST -H "Authorization: Bearer ${accessToken}" -H "Content-Type: application/json" -d "{\"function\": \"function3\", devMode: true}" ${url}
    curl -X POST -H "Authorization: Bearer ${accessToken}" -H "Content-Type: application/json" -d "{\"function\": \"function4\", devMode: true}" ${url}
    curl -X POST -H "Authorization: Bearer ${accessToken}" -H "Content-Type: application/json" -d "{\"function\": \"function5\", devMode: true}" ${url}
    

    When above script is run, the following result is obtained.

    {
      "done": true,
      "response": {
        "@type": "type.googleapis.com/google.apps.script.v1.ExecutionResponse",
        "result": "Function1: Done."
      }
    }
    {
      "done": true,
      "response": {
        "@type": "type.googleapis.com/google.apps.script.v1.ExecutionResponse",
        "result": "Function2: Done."
      }
    }
    {
      "done": true,
      "response": {
        "@type": "type.googleapis.com/google.apps.script.v1.ExecutionResponse",
        "result": "Function3: Done."
      }
    }
    {
      "done": true,
      "response": {
        "@type": "type.googleapis.com/google.apps.script.v1.ExecutionResponse",
        "result": "Function4: Done."
      }
    }
    {
      "done": true,
      "response": {
        "@type": "type.googleapis.com/google.apps.script.v1.ExecutionResponse",
        "result": "Function5: Done."
      }
    }
    

    Of course, as a test, you can also manually run each curl command.

    Note:

    • In this answer, it supposes that you have already been able to execute the function of Google Apps Script project using Google Apps Script API. Please be careful this.

    Reference: