I used this script from Medium. I wanted to download all Excel attachments from my may under the label excelHere. The script works without specifics in the attachment and labels. How do I improve the script to fit my need?
function saveNewAttachmentsToDrive() {
var folderId = "ABCDE"; // Replace with the ID of the destination folder in Google Drive
var emailAddress = "[email protected]"; // Email address
var searchQuery = "to:" + emailAddress + " has:attachment"; // Replace with the search query to find emails with attachments
var lastExecutionTime = getLastExecutionDate();
var threads = GmailApp.search(searchQuery + " after:" + lastExecutionTime);
var driveFolder = DriveApp.getFolderById(folderId);
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
var attachments = message.getAttachments();
for (var k = 0; k < attachments.length; k++) {
var attachment = attachments[k];
var attachmentBlob = attachment.copyBlob();
var fileName = attachment.getName();
driveFolder.createFile(attachmentBlob).setName(fileName);
}
}
}
updateLastExecutionDate();
}
function getLastExecutionDate() {
var properties = PropertiesService.getUserProperties();
return properties.getProperty("lastExecutionDate") || "2023-09-01";
}
function resetLastExecutionDate() {
PropertiesService.getUserProperties().deleteProperty("lastExecutionDate");
}
function updateLastExecutionDate() {
var now = new Date();
var dateString = now.toISOString().split("T")[0];
var properties = PropertiesService.getUserProperties();
properties.setProperty("lastExecutionDate", dateString);
}
The script above works fine.But I want to download Excel attachments from a specific label.
I want to get all Excel attachments from a specific label