Curl 如何知道Google应用程序脚本的进度状态

Curl 如何知道Google应用程序脚本的进度状态,curl,google-apps-script,httprequest,google-apps-script-api,Curl,Google Apps Script,Httprequest,Google Apps Script Api,我创建了一个应用程序脚本,可自动执行多个任务: 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

我创建了一个应用程序脚本,可自动执行多个任务:

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.
这5个函数在同一个应用程序脚本中使用“全局”函数顺序执行,该函数允许我检索文件夹的ID和创建的文档我使用cURL运行此应用程序脚本(从终端)

脚本的持续时间约为10秒,我想知道是否有可能在每个函数完成时得到更新的消息(或文本值)作为响应

我在互联网上和Stackoverflow上查找过,但我发现的所有内容都涉及到在电子表格中使用“flush”命令来更新单元格的值。我需要的是更新终端中的响应消息

谁能帮帮我吗

多谢各位


典狱长

从您的以下职能中

职能1。-创建新文件夹并检索ID。 职能2。-在文件夹中创建文档文件的副本。 职能3。-替换文档中{{text}}类型的几个标记的文本。 职能4在文档中插入图像。 职能5检索文档的公共链接

我认为在您的情况下,可以使用以下3种模式

  • 所有功能都作为一个功能来实现

    • 在这种情况下,您的
      目标我想知道是否有可能在响应中获得消息(或文本值),并在每个功能完成时更新。
      无法实现
    • 为了在每个函数完成时检索响应,每次都使用curl命令执行每个函数。这一点我已经提过了
  • 在“功能1”中创建新文件夹id时,将从该功能返回文件夹id,并在“功能2”中使用文件夹id。并且,当复制文档时,将从函数返回文档ID,并将其用于“函数3”、“函数4”和“函数5”

    • 在这种情况下,不需要使用全局变量。为了检索响应,每次都使用curl命令执行每个函数。但是,当执行每个函数时,需要将每个响应值提供给下一个函数。我想这可能有点复杂
  • 当在“功能1”中创建新文件夹id时,文件夹id将放入PropertiesService,而当运行“功能2”时,将使用从PropertiesService检索到的文件夹id。并且,当复制文档时,文档ID被放入PropertiesService,当运行“功能3”、“功能4”和“功能5”时,使用从PropertiesService检索到的文档ID

    • 在这种情况下,不需要使用全局变量。并且,文件夹ID和文档ID在每个函数运行期间放置和获取。这样,可以使用curl命令按顺序运行每个函数。因此,我认为使用curl命令的脚本可能会变得有点简单
  • 根据以上情况,我想在我的回答中提出模式3

    用法: 示例脚本: 作为测试此模式的示例脚本,将使用以下脚本

    // 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.";
    }
    
    • 在这个示例脚本中,在每个函数运行期间放置和获取文件夹ID和文档ID
    • 在此示例脚本中,不包括错误处理。所以当你使用这个时,请添加它
    测试。 为了从每个函数中检索每个响应,在本例中,脚本运行5个curl命令。示例脚本如下所示

    #!/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}
    
    当运行上述脚本时,将获得以下结果

    {
      "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."
      }
    }
    
    当然,作为测试,您还可以手动运行每个curl命令

    注:
    • 在这个答案中,假设您已经能够使用Google Apps脚本API执行Google Apps脚本项目的功能。请小心这个
    参考:

    我认为http请求在执行期间无法返回消息。我认为您可能需要改变方法并调用每个函数,这可能是自动化的,即通过创建windows批处理文件、Power Shell脚本等。感谢@Rubén,调用每个函数的选项是我的第二选择,但我希望有一种方法可以在“全局”上实现函数。我建议您研究http请求和REST API的工作原理。非常感谢@Tanaike的详细回复。我将按照您的建议使用模式3。你的剧本非常清晰,效果非常好。我还可以选择将其单独用于特定功能。非常感谢@典狱长:谢谢你的回复。我很高兴你的问题解决了。也谢谢你。