Last active
March 7, 2024 21:05
-
-
Save moayadhani/fcb75d0bf5be50b83e36e05746e2722d to your computer and use it in GitHub Desktop.
Get Gmail Emails By Assigned Label
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ui = SpreadsheetApp.getUi(); | |
function onOpen(e){ | |
ui.createMenu("Gmail Manager").addItem("Get Emails by Label", "getGmailEmails").addToUi(); | |
} | |
function getGmailEmails(){ | |
var input = ui.prompt('Label Name', 'Enter the label name that is assigned to your emails:', Browser.Buttons.OK_CANCEL); | |
if (input.getSelectedButton() == ui.Button.CANCEL){ | |
return; | |
} | |
var label = GmailApp.getUserLabelByName(input.getResponseText()); | |
var threads = label.getThreads(); | |
for(var i = threads.length - 1; i >=0; i--){ | |
var messages = threads[i].getMessages(); | |
for (var j = 0; j <messages.length; j++){ | |
var message = messages[j]; | |
if (message.isUnread()){ | |
extractDetails(message); | |
GmailApp.markMessageRead(message); | |
} | |
} | |
threads[i].removeLabel(label); | |
} | |
} | |
function extractDetails(message){ | |
var dateTime = message.getDate(); | |
var subjectText = message.getSubject(); | |
var senderDetails = message.getFrom(); | |
var bodyContents = message.getPlainBody(); | |
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); | |
activeSheet.appendRow([dateTime, senderDetails, subjectText, bodyContents]); | |
} |
@mtram8
Hi! I am pleased you found this code helpful to you.
Regarding the issue you are facing, I guess the previous execution was not successful, and you ran the secod label extraction before the previous is done. That being said, it could be caused by other factors. Can you share with me a short screen recording of the problem and send over email at [email protected] and cc [email protected]
I will help you out with your issue
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @moayadhani! Love your tutorial and appreciate your help in advance!
When I tried running the above (pasted below too) source code, I saw a box appear in the UI requesting for Label Name. When I provided the Label Name again, I see error messages that say Error resuming script execution and Script getGmailEmails experienced an error.
Can you please lmk what the source code should be so I can retrieve only email messages labeled General User Support?
Source Code
var ui = SpreadsheetApp.getUi();
function onOpen(e){
ui.createMenu("Gmail Manager").addItem("Get Emails by Label", "getGmailEmails").addToUi();
}
function getGmailEmails(){
var input = ui.prompt('Label Name', 'General User Support', Browser.Buttons.OK_CANCEL);
if (input.getSelectedButton() == ui.Button.CANCEL){
return;
}
var label = GmailApp.getUserLabelByName(input.getResponseText());
var threads = label.getThreads();
for(var i = threads.length - 1; i >=0; i--){
var messages = threads[i].getMessages();
}
}
function extractDetails(message){
var dateTime = message.getDate();
var subjectText = message.getSubject();
var senderDetails = message.getFrom();
var bodyContents = message.getPlainBody();
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
activeSheet.appendRow([dateTime, senderDetails, subjectText, bodyContents]);
}