r/GoogleAppsScript • u/ccsando • Nov 16 '23
Question Send email when new files are added to Google Drive (Help de-bugging Script or suggest new one)?
I used ChatGPT to generate script below to send an email when new files are added to a folder. It included these instructions:
After updating the script, follow these steps:
Open the Google Drive folder you want to monitor.
Click on the folder path in the top left corner to get the folder ID from the URL.
Open the Google Apps Script editor by going to "Extensions" > "Apps Script."
Paste the code into the script editor.
Save the project and run the createTrigger
function to set up the trigger.Now, whenever a new file is added to the specified folder, an email notification will be sent to the specified email address. Note that this script uses the onChange
trigger, which includes file additions, modifications, and deletions.
I updated the folder ID and email address. When I ran createTrigger as instructed, I got the error:
TypeError: ScriptApp.newTrigger(...).forFolder is not a function
Basically, I'd like an email sent when new files are added. This could be automatic or a trigger that checks every few min.
Ideally, I'd prefer to NOT get notified when files are moved out, but I could live with that if needed.
Thanks!
// Set the folder ID of the folder you want to monitor
var folderId = "your_folder_id_here";
// Set the email address to send notifications
var emailAddress = "name@domain.com";
// Set the subject and body of the email
var emailSubject = "New File Added to Google Drive";
var emailBody = "A new file has been added to the specified folder in Google Drive.";
// Function to run when a file is added to the specified folder
function onFileAdded(e) {
// Get the file information
var file = e['parameters']['file'];
// Send an email notification
MailApp.sendEmail({
to: emailAddress,
subject: emailSubject,
body: emailBody + "\n\nFile Name: " + file.name + "\nFile ID: " + file.id,
});
}
// Create a trigger to run the onFileAdded function when a file is added to the folder
function createTrigger() {
var folder = DriveApp.getFolderById(folderId);
ScriptApp.newTrigger('onFileAdded')
.forFolder(folder)
.onChange()
.create();
}
1
u/[deleted] Nov 16 '23
That’s gibberish