Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Google Script for emailing Google Drive folder-updates to a group of recipients.
// Copyright 2016 Wojciech Dziemianczyk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
function notify() {
// statics
var folderName = 'kittens';
var dataStorefileName = 'last_updated_kittens_store';
var emails = ['firstEmail123@example.com', 'otherEmail124@example.com'];
// Check for store file, get the last updated time
var currentTime = new Date().getTime();
var lastCheckedDate = new Date(0);
var storeFiles = DriveApp.getFilesByName(dataStorefileName);
if (storeFiles.hasNext()){
var storeFile = storeFiles.next();
var content = Number(storeFile.getBlob().getDataAsString());
lastCheckedDate = new Date(content);
storeFile.setContent(currentTime);
} else {
DriveApp.createFile(dataStorefolderName, currentTime);
return;
}
// Check for new files
var emailBody = 'New files have been added to ' + folderName + '. Files added: \n\n';
var folder = DriveApp.getFoldersByName(folderName);
while (folder.hasNext()) {
var kittensFolder = folder.next();
var files = kittensFolder.getFiles();
var updatedFilesFormatted = '';
// If there are files in the folder
if (files.hasNext()){
while (files.hasNext()){
var file = files.next();
// And if the lastUpdated date is greater than the lastCheckedDate
if (file.getLastUpdated() >= lastCheckedDate){
// Format & appened to a String
updatedFilesFormatted = updatedFilesFormatted.concat(file.getName() + '\n');
}
}
// If our formatted string is not empty, email the list to emails
if (updatedFilesFormatted.length > 0){
for (var i = 0; i < emails.length; i++){
GmailApp.sendEmail(emails[i], folderName + ' was updated', emailBody.concat(updatedFilesFormatted));
}
}
}
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment