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
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.
All functions are metged as one function.
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.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".
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.
From above situation, I would like to propose the pattern 3 in my answer.
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 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.