Tuesday 26 April 2022

Email notification of Drive file changes within last x hours

The following Google Apps Script is designed to periodically check for any changes that have occurred to a given Google Drive file (ie edits to it) and send an email to notify of that. It goes through the Revision (Version History) of the file and looks at the modification dates for those that match with your given timeframe, to then collect the Username & Email address of the person that made those edits for inclusion in the email.

So as an example you may have a Google Sheet where you want to setup a check every 4 hours, to then be emailed if there have been any edits to it, along with who made those edits.

Email notification of Drive file changes
Email notification of Drive file changes

Instructions

  1. There are 3 pieces of information to complete in the file: firstly enter the ID of the Google Drive file that you wish to monitor and receive notifications for. A file ID is typically the long string of random letters/numbers of the Url eg for a Google Doc: https://docs.google.com/document/d/this_bit_is_the_ID/edit.
  2. Provide the number of hours you would like to use as the frequency to check for changes: 1, 2, 4, 6, 8, 12.
  3. Provide the email address (can be a Google Group email) for where you would like the email to be sent.
  4. Using the menu along the top ensure that 'createTrigger' is selected and then press the 'Run' button to set it up automatically.
  5. If you wish to undo/cancel the scheduled check then select 'deleteTriggers' from the dropdown in Step 4 and press 'Run'.
  6. At the moment this script is desiged to work with just one file and one email address so you will need to make another copy if you wish to reuse it.

 

Limitations

This was initially developed for use with Google Sheets, so when I was exploring Drive Revisions to see if I could get the exact details of what had been edited, the data was a lot more jumbled than the examples I was seeing online for Google Docs. Therefore I could not effectively get the exact details of the edits (such as new rows added, cell values changed, etc) to be included within the email.


The Code

A few bits of the code have been highlighted below.

There is a Function to create the Trigger that runs every x hours. So this takes the input of the number of hours specified and will then schedule to run automatically - so this is the piece of code that ultimately lets you walk away and let it do its thing:

function createTrigger() {

  ScriptApp.newTrigger('emailOnFileRevisions')
    .timeBased()
    .everyHours(hours)  // run every x hours
    .create();
  Logger.log('New Trigger successfully created');

}

We need to use the Drive API in order to get the all of the revisions for the given file along with some extra details that will be useful for us:

// get all revisions for Drive file
  revisions = Drive.Revisions.list(fileID);

// get all revision items (array returned)
  var revisionItems = revisions.items;

// date/time last modification was made
  var modifiedDate = new Date(revision.modifiedDate);

// get Username of last modifier
  var lastModifierUsername = revision.lastModifierUsername;

// get User Email address of last modifier
  var lastModifierEmail = revision.lastModifyingUser.emailAddress;

Another tricky part is working out how to navigate the dates/times so we only account for changes within our given time period. For that we just need to take the current date/time, subtract the modified date/time from the file revision and if this total is less than x hours (the interval we specified at the very beginning) then the change is one we want to log for our email:

if ((currentDateTime - modifiedDate) < xHours) {

With regards to the email, it is possible there are multiple file revisions that have been made during our time period. During the creation of the email body therefore we we need a small loop that will simply append a new line for each change:

for (var j = 0; j < editList.length; j++) {
      body += "> " + editList[j] + "\n\n";
}


Download

Email notification of Drive file changes within last x hours download (please use 'Overview' > 'Make a copy' for your own version).

No comments:

Post a Comment