Star a Gmail message with google script - google-apps-script

I'm totally new to Google Script and I'm trying to find out how to star a message that I have labeled as "R/R(short)/R(ASAP)" and has held that label for at least a day. Any suggestions? Here's what I've got so far:
function star2do() {
var delayDays = 1 // Enter # of days before messages are starred
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays);
var label = GmailApp.getUserLabelByName("R/R(short)/R(ASAP)");
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
if (threads[i].getLastMessageDate()<maxDate)
{
threads[i].markUnread();
threads[i].addLabel(star) //this is where I'm having issues
}
}
}

threads[i].getMessages()[0].markUnread().star();
Hope this helps :)

Related

Google Apps Script Gets All Emails In Thread Not Only Labeled

I have the following code in my script:
function getEmail() {
var label = GmailApp.getUserLabelByName("parse");
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 msg = messages[j].getBody();
Logger.log(msg);
}
threads[i].removeLabel(label);
}
}
When no emails with label, everything is fine.
But if new email arriving and gets label "parse", there are all emails inside messages array, not just the one with label.
I have the same code in another account and everything works perfect.
Please help me figure it out.

Count all eMails from a Label

i have create this script:
function myTest() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var label = GmailApp.getUserLabelByName("Label1");
var threads = label.getThreads();
var countMail = 0;
//get first message in first thread
for (var i = 0; i < threads.length; i++) {
var messages=threads[i].getMessages();
countMail = countMail + messages.length;
}
ss.getRange(1,1).setValue(countMail);
}
it runs nearly perfect. Here I get all eMails back which are connected with this treads (marked with Label1 or not).
Does anyone can share a simply script how I can count all eMails which are "realy" marked with the Label.
Thanks
Try this:
function labelMsgs(){
var labels = Gmail.Users.Labels.list("me").labels;
var lblId;
for (var i = 0; i < labels.length; i ++){
if (labels[i].name == "Label1"){
lblId = labels[i].id;
break;
}
}
var optionalArgs = {
"labelIds" : lblId,
"maxResults" : 200
}
var messages = Gmail.Users.Messages.list("me", optionalArgs).messages;
}
This method uses the Gmail API directly, for this you'll need to enable Google Advanced Services, it will return a list of all messages tagged with the "Label1" label. You can play around with maxResults and with the pageToken to see more results, but this is the general approach.

Extract the exact subjects and update the google sheets using apps script

I have found a below mentioned apps script to get the subject name of the emails from the particular label and update the google sheets.
function getSubjects() {
var sheet = SpreadsheetApp.getActiveSheet();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var label = GmailApp.getUserLabelByName("Test");
var threads = label.getThreads();
var row = 2;
for (var i = 0; i < threads.length; i++) {
var messages=threads[i].getMessages();
for (var m=0; m < messages.length; m++) {
sheet.getRange(row,1).setValue(messages[m].getSubject());
row++;
}
}
};
On daily basis, I will receive more than 100 emails with the same subject lines with small modifications. So, I need to get in the google sheets only the emails that have a subject line which includes the word "Pending".
I am hoping someone can help me make this example work.
Use the Gmail API for Apps Script
It features the method Users.messages.list which allows you to
triage the messages by both labels and query simultaneously.
Sample:
function myFunction() {
Logger.log(Gmail.Users.Messages.list('me', {'labelIds': 'Label_1003148783259556236','q': 'subject:Pending' }));
}
Make sure you enable Gmail API from the Apps Script UI Resources-> Advanced Google Services-> Gmail API
Never-mind. I have figured it out. Below is the updated code which is working as expected.
function getSubject() {
var sheet = SpreadsheetApp.getActiveSheet();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var threads = GmailApp.search('subject: Successfully Completed is:Test/2019_08');
var row = 2;
for (var i = 0; i < threads.length; i++) {
var messages=threads[i].getMessages();
for (var m=0; m < messages.length; m++) {
sheet.getRange(row,1).setValue(messages[m].getSubject());
row++;
}
}
};

How to move gmail messages with label to a folder after 7 days

I have added a label to messages as noted in the lifehacker link below.
I would like to have client emails labeled "clients" moved to the INBOX/Clients folder after 7 days.
Thanks in advance!
I know that there is a code for cleaning the mail-box
function cleanUp() {
var delayDays = 2 // Enter # of days before messages are moved to trash
var maxDate = new Date();
maxDate.setDate(maxDate.getDate() - delayDays);
var label = GmailApp.getUserLabelByName("delete me");
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
if (threads[i].getLastMessageDate() < maxDate) {
threads[i].moveToTrash();
}
}
}
But I need to move emails. How to do this?
Try something like this
function moveUp() {
var delayDays = 7 // Enter # of days before messages are moved to trash
var maxDate = new Date();
maxDate.setDate(maxDate.getDate() - delayDays);
var label1 = GmailApp.getUserLabelByName("clients1"); // FROM
var label2 = GmailApp.getUserLabelByName("clients2"); // TO
var threads = label1.getThreads();
for (var i = 0; i < threads.length; i++) {
if (threads[i].getLastMessageDate() < maxDate) {
threads[i].addLabel(label2).removeLabel(label1).moveToArchive();
}
}
}
label1 is from-label
label1 is to-label
You could erase .moveToArchive() if you don't need to archive the thread.

Get message sender address from Gmail API

Let me preface my question by saying that I am not a JS dev, or even a web dev, so bear with me! I'm trying to create a gs script that deletes messages that are older than n months that have a certain label and are from a given sender.
I think I have it down, but the Gmail API getFrom() method seems to return the sender's address in the format "First Last" <address#mail.tld> rather than just address#mail.tld. Currently I can work around this by the fact that I know this information, but is there a better way of doing this so that the code operates solely on the actual email address?
My current code:
function auto_delete_emails() {
var label = GmailApp.getUserLabelByName("foo");
var sender = "\"Foo Bar\" <info#foo.bar>";
if (label != null) {
var delayDays = 30
var maxDate = new Date();
maxDate.setDate(maxDate.getDate() - delayDays);
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
var messages = GmailApp.getMessagesForThread(threads[i]);
var from = messages[0].getFrom();
if (from == sender) {
if (threads[i].getLastMessageDate < maxDate) {
threads[i].moveToTrash();
}
}
}
}
}
I have seen a question that used a regex to fix this, but I barely understand regex, never mind how to apply in this context or how the example worked (or didn't!).
Suggestions for other improvements on this code also welcome.
I think extracting the email address from the From-header is your safest bet. You can use e.g. this great RegEx:
function extractEmailAddress(header) {
return header.match(/[^#<\s]+#[^#\s>]+/)[0];
}
function auto_delete_emails() {
var label = GmailApp.getUserLabelByName("foo");
var sender = extractEmailAddress("\"Foo Bar\" <info#foo.bar>");
if (label != null) {
var delayDays = 30;
var maxDate = new Date();
maxDate.setDate(maxDate.getDate() - delayDays);
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
var messages = GmailApp.getMessagesForThread(threads[i]);
var from = extractEmailAddress(messages[0].getFrom());
if (from === sender) {
if (threads[i].getLastMessageDate < maxDate) {
threads[i].moveToTrash();
}
}
}
}
}