- You want to know whether the script is running by the time-driven trigger now.
- You want to achieve this using Google Apps Script.
If my understanding is correct, how about this answer? Please think of this as just one of several answers.
Solution:
I think that your goal can be achieved using the method of processes.list in Google Apps Script API. When the method of processes.list in Google Apps Script API is request during the script is run by the time-driven trigger, the following value can be retrieved.
{
"processes": [
{
"projectName": "sampleProject",
"functionName": "myFunction",
"processType": "TIME_DRIVEN",
"processStatus": "RUNNING", // <--- here
"userAccessLevel": "OWNER",
"startTime": "2019-10-07T00:00:00.000Z",
"duration": "36.145s"
}
]
}
In this case, processStatus is RUNNING. When the script is finished, processStatus is changed to COMPLETED.
The sample script for retrieving the information when processStatus is RUNNING is as follows.
Usage:
1. Linking Cloud Platform Project to Google Apps Script Project
In this case, it is required to link Cloud Platform Project to Google Apps Script Project. About how to link it, you can see it at here.
2. Enable Google Apps Script API
As the second step, please enable Google Apps Script API at API console. About how to enable it, you can see it at this thread.
3. Run sample script:
function sample() {
var scriptId = "###"; // Please set the script ID here.
var functionName = "###"; // Please set the function name here.
var url = "https://script.googleapis.com/v1/processes?userProcessFilter.functionName=" + functionName + "&userProcessFilter.scriptId=" + scriptId;
var res = UrlFetchApp.fetch(url, {headers: {Authorization: 'Bearer ' + ScriptApp.getOAuthToken()}});
var obj = JSON.parse(res);
var runningScript = obj.processes.filter(function(p) {return p.processType == "TIME_DRIVEN" && p.processStatus == "RUNNING"});
Logger.log(runningScript)
}
- When you use this script, please add the scope of
https://www.googleapis.com/auth/script.external_request and https://www.googleapis.com/auth/script.processes. So please add them to the manifest file (appsscript.json).
- When you use this script, please set the variables of
scriptId and functionName which are run when the time-driven trigger is fired.
- In this script, when there are no running scrit,
[] is returned.
- If you want to know other process types, please modify
p.processType == "TIME_DRIVEN".
Note:
- I think that in your case, you can also use Method: processes.listScriptProcesses.
- In my experience, when the script is finished, there was the case that
processStatus sometimes became UNKNOWN. But in this case, it indicates that the script is finished.
References:
If I misunderstood your question and this was not the direction you want, I apologize.
Added:
I think that the method using Apps Script API is one solution. In order to achieve your goal without linking Cloud Platform Project to GAS Project, here, I would like to propose a workaround.
In this workaround, I used the property of Drive API. When the property of Drive API is used, the property can be retrieved other project. So here, I used the property of Drive API. Please think of this as just one of several workarounds.
Usage:
1. Enable Drive API at Advanced Google services
At first, please enable Drive API at Advanced Google services.
2. Sample scripts:
Sample script 1:
Please copy and paste the following script. It supposes that the function of runByTrigger is run by the time-driven trigger.
function runByTrigger(e) {
var fileId = "###"; // Please set the file ID.
if (e && "triggerUid" in e && e.triggerUid != "") {
Drive.Files.update({properties: [
{key: "processStatus", value: "RUNNING", visibility: "PUBLIC"},
{key: "processStartTime", value: new Date().toISOString(), visibility: "PUBLIC"},
]}, fileId);
}
// do something
// Here, please put your script.
if (e && "triggerUid" in e && e.triggerUid != "") {
Drive.Files.update({properties: [
{key: "processStatus", value: "COMPLETED", visibility: "PUBLIC"},
{key: "processStartTime", value: "", visibility: "PUBLIC"},
]}, fileId);
}
}
Sample script 2:
This script is used for checking whether the script is run by the time-driven trigger. Please run this script for checking it. You can also use this script which is not the same project including runByTrigger.
function myFunction() {
var fileId = "###"; // Please set the file ID.
var runningScript = Drive.Properties.get(fileId, "processStatus", {visibility: "PUBLIC"}).value;
if (runningScript == "RUNNING") {
var startTime = Drive.Properties.get(fileId, "processStartTime", {visibility: "PUBLIC"}).value;
Logger.log("Script is currently running. Start time is %s", startTime)
} else {
Logger.log("Script was finished.")
}
}
- Both
fileId of script 1 and 2 is the same ID. As a test case, if the script is the standalone script, please set the script ID of the standalone script. If the script is the container-bound script, please set the file ID of Google Docs.
- When
runByTrigger is run by the time-driven trigger, processStatus and processStartTime are put to the property of the file.
- If the script is currently running, when the function of
myFunction is run, Script is currently running. Start time is {startTime} is shown at the log.
- If the script is finished and not run,
Script was finished. is shown at the log.
Note:
- In this workaround, the property of file is used. But I think that for example, the process status can be also saved to Spreadsheet and so on.
References: