Recommand · May 24, 2021 0

How to get last execution time of a google app script?

Does Google App Scripts provide an API to determine when a script was executed the last time?

I’m executing a script periodically and I only like to process data which is new since the last execution, thus I’d like to know when my script was executed the last time. But since I execute it not every x min its hard to hardcode this value.

Thanks in advance, I really appreciate your help and expertise.

I believe your goal as follows.

  • You want to retrieve the last executed time of the functions of Google Apps Script.
  • You want to achieve this using Google Apps Script.

In this case, I thought that the method of "Method: processes.list" of Google Apps Script API might be able to be used. In order to use this API, please do the following flow.

Usage:

1. Linking Google Cloud Platform Project to Google Apps Script Project.

In this case, at first, it is required to link Google Cloud Platform Project to Google Apps Script Project. For this, you can see the detail flow at this repository

2. Prepare sample script.

Please copy and paste the following script to the script editor of the Google Apps Script project linking to Google Cloud Platform Project.

function myFunction() {
  const scriptId = "###"; // Please set the Google Apps Script project.
  const url = "https://script.googleapis.com/v1/processes?pageSize=1&userProcessFilter.scriptId=" + scriptId;
  const res = UrlFetchApp.fetch(url, {headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()}});
  const obj = JSON.parse(res.getContentText());
  const lastExecutedProcess = obj.processes[0];
  console.log(lastExecutedProcess)
}
  • In this sample script, the last executed process of the specific Google Apps Script project can be retrieved. For example, you can also retrieve the last executed process of the specific function of the the specific Google Apps Script project.
  • When an error occurs, please check https://github.com/tanaikech/Linking-Google-Cloud-Platform-Project-to-Google-Apps-Script-Project-for-New-IDE#sample-1. You can see the method for avoiding the error using Apps Script API.

Note:

  • For example, if you want to retrieve the last executed process of all functions of all Google Apps Script project in your Google Drive, you can also use the following script.

      function myFunction2() {
        const url = "https://script.googleapis.com/v1/processes?pageSize=1";
        const res = UrlFetchApp.fetch(url, {headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()}});
        const obj = JSON.parse(res.getContentText());
        const lastExecutedProcess = obj.processes[0];
        console.log(lastExecutedProcess)
      }
    
  • In this API, you can also use from outside of Google. So, you can also achieve above using other languages except for Google Apps Script.

References:

  • Method: processes.list
  • Linking Google Cloud Platform Project to Google Apps Script Project for New IDE