Creating unique IDs in Google Sheets using Google Apps Script is an invaluable skill for automating...
Automate Your Google Drive: Email Alerts for New Files with Apps Script!
This script is designed to monitor specified Google Drive folders for new file additions and send email notifications upon detecting any changes. The complete code can be found in my GitHub repository at Net-RVA's GitHub, which provides a detailed script for automating your Google Drive to send email alerts for new files.
Full Code Explanation
1. Specifying Folders to Monitor
javascript
Copy code
const watchedFolders = [
{
resourceType: 'folder',
resourceName: 'Demo 1',
resourceId: 'YOUR FOLDER ID' // Replace with your actual folder ID
}
];
This array, watchedFolders
, contains objects defining the Google Drive folders you want to keep an eye on. Each object includes the type of resource (resourceType
), a friendly name for identification (resourceName
), and the unique ID of the folder (resourceId
).
2. Initializing the Persistent Storage
javascript
Copy code
const persistentStore = ObjectStore.create('script', {manual: true});
The persistentStore
variable initializes the Object Store library for data storage, ensuring that file counts within each folder are tracked over time. The manual: true
option indicates that the data will be saved explicitly using the persist()
method.
3. Main Function for Monitoring
javascript
Copy code
function monitorFolders() {
let folderResource, currentFiles, previousFileCount;
let newFileCount = 0;
for(let folder of watchedFolders) {
folderResource = fetchResource(folder);
previousFileCount = persistentStore.get(folder.resourceId);
if(previousFileCount === null) {
previousFileCount = initializeFolderInStore(folder);
}
currentFiles = folderResource.getFiles();
while(currentFiles.hasNext()) {
currentFiles.next();
newFileCount++;
}
if(newFileCount !== previousFileCount) {
MailApp.sendEmail({
to: 'YOUR EMAIL', // Specify your email address
subject: `New file in "${folder.resourceName}"`,
htmlBody: `A new file has been added to "${folderResource.getName()}". Access it here: ${folderResource.getUrl()}`
});
persistentStore.set(folder.resourceId, newFileCount);
persistentStore.persist();
} else {
console.log('No new files added.');
}
}
}
monitorFolders
is the core function that iterates through each folder defined in watchedFolders
, checks for new files, and sends an email alert if any are found. It uses fetchResource
to access the folder and compares the current file count with the stored count. If there's a difference, an email is sent, and the new count is updated in the store.
4. Initializing Folders in the Store
javascript
Copy code
function initializeFolderInStore(folderInfo) {
persistentStore.set(folderInfo.resourceId, 0);
persistentStore.persist();
return persistentStore.get(folderInfo.resourceId);
}
The initializeFolderInStore
function sets up a new folder in the persistentStore
with an initial file count of 0, preparing it for monitoring.
5. Fetching Resources
javascript
Copy code
function fetchResource(resourceDetails) {
switch(resourceDetails.resourceType) {
case "folder":
return DriveApp.getFolderById(resourceDetails.resourceId);
case "file":
return DriveApp.getFileById(resourceDetails.resourceId);
}
}
fetchResource
retrieves the Google Drive resource (folder or file) based on its ID and type, allowing the script to access and monitor these resources.
Conclusion
This script automates the monitoring of Google Drive folders for new files, sending email notifications when changes are detected. For the full code and more details, visit the GitHub repository provided above. This script offers a practical solution for staying updated on file additions to your Google Drive folders, enhancing productivity and collaboration.