Google Drive app on Make doesn’t offer an instant trigger on file uploads – so you either has to schedule your scenario and waste a lot of check runs or you do some other sourcery to trigger it on demand.
Well… I’ve searched and found out a way to use Google Apps Script to do this recurrent check on a Google Drive folder – so it fires a Make webhook that you can then use to start your scenario.
In order for this to work, go to script.google.com and create a new project.
Paste in this code:
// Replace 'myFolderID' with the actual ID of your folder and 'https://mysample.webhook.com' with your webhook URL
const folderId = 'myFolderID';
const webhookUrl = 'https://mysample.webhook.com';
const cache = CacheService.getScriptCache();
function isRecentlyCreated(file) {
const now = new Date();
const threshold = 60 * 1000; // Check for files created in the last minute (adjust as needed)
return now - file.getLastUpdated() < threshold;
}
function checkForNewFiles() {
const folder = DriveApp.getFolderById(folderId);
const files = folder.getFiles();
let newFilesFound = false;
while (files.hasNext()) {
const file = files.next();
const fileId = file.getId();
// Check cache for processed fileId
if (!cache.get(fileId)) {
if (isRecentlyCreated(file)) {
const payload = { fileId };
UrlFetchApp.fetch(webhookUrl, {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify(payload)
});
cache.put(fileId, 'processed', 60 * 60); // Cache processed fileId for 1 hour
console.log(`New file found: ${fileId}`);
newFilesFound = true;
}
} else {
console.log(`File ${fileId} already processed`);
}
}
if (!newFilesFound) {
console.log('No new files found');
}
}
function startMonitoring() {
// Schedule a function to check for new files periodically (e.g., every minute)
ScriptApp.newTrigger('checkForNewFiles')
.timeBased()
.everyMinutes(1)
.create();
}
function main() {
startMonitoring();
}
Also, click on the “Resources” section, on the left-side panel and add the Drive API.
Then, select the function “main” on the menu at the top and click “Execute” to run it. You’ll first will be prompted to authorize the script with your credentials, then click “Execute” again to create the trigger.
In order to see if this is working or not, go to the “Triggers” section (on the left-side panel) and check if there is a trigger created.
You can also see all the events executions in the “Executions” section.
Note: this will check for the file modification date – so it works for files recently created or modified – so take that in mind.
Hope it helps!