reply time between threads [google apps script] - google-apps-script

I want to develop a script in GAS to get the reply time of all of my threads in Gmail in a specific period. The example script below seems to be a way, but im not sure on how to proceed.
function processInbox() {
// get all threads in inbox
var threads = GmailApp.getInboxThreads();
for (var i = 0; i < threads.length; i++) {
// get all messages in a given thread
var messages = threads[i].getMessages();
// iterate over each message
for (var j = 0; j < messages.length; j++) {
// log message subject
Logger.log(messages[j].getSubject());
}
}
};

Maybe use:
Google Documentation - getDate() GmailMessage Class
When you say you want to "get" the information, how do you want to get it? Be sent an email? Store it in a spreadsheet? Record it in a .csv file?

Related

Getting the body of individual emails from gmail to google sheets

I'm really new at using Google Apps Script, so if what I'm trying doesn't make sense, or just isn't possible please let me know.
Everyday I get several emails that look like the following:
Your Name: FirstName LastName
Phone Number: 555 867 5309
Email Address: FakeEmail#email.com
What do you need help with? Request someone makes.
I'm attempting to automatically send the body of these emails to a new line in a Google Sheet when they come in.
As of right now I have every email get the label "myLabel" when it comes in. I then run the following script, which is a slightly modified version of something I found here:
function myFunction() {
var ss = SpreadsheetApp.getActiveSheet();
var label = GmailApp.getUserLabelByName("MyLabel");
var threads = label.getThreads();
for (var i=0; i<threads.length; i++)
{
var messages = threads[i].getMessages();
for (var j=0; j<messages.length; j++)
{
var msg = messages[j].getBody();
ss.appendRow([msg])
}
threads[i].removeLabel(label);
}
}
I'm attempting to run this code with a timer trigger every 15 minutes. The issue I've run into is that every time the code runs it pulls from every email in the thread. I would like it to just pull from the emails that are new since the last time it ran. Any advice would be greatly appreciated.
Why not mark the messages as read when you finish processing them? Here is a sample from one of my scripts.
var pendingEmailLabel = "MyLabel";
var threads = GmailApp.getUserLabelByName(pendingEmailLabel).getThreads();
for (var t = 0; t < threads.length; ++t) {
var thread = threads[t];
var messages = thread.getMessages();
for (var m = 0; m < messages.length; ++m) {
var message = messages[m];
if (message.isUnread()) {
// INSERT YOUR CODE HERE THAT TAKES ACTION ON THE MESSAGE
message.markRead();
}
}
}
}

Issue with GetUserLabelByName Google App Script

I'm trying to use "getUserLabelByName" to only retrieve email threads labeled with "MyLabel", but the script is also pulling emails from the "Sales", "Marketing" labels. These emails do not have the "MyLabel" label and I can't figure out why they would be included in the array when I've specifically told App Script to only pull emails with the label "MyLabel".
I added the Logger.log(To); so that I could see which email addresses were being logged. For background, all emails coming to a certain address, for instance info#company.com should be marked with the label "MyLabel", while other emails sent to other email addresses are assigned different labels. So if were are only collecting emails from "MyLabel", there should be no reason to see any other email addresses in the log other than "info#company.com", but there are other email addresses being captured and I can't figure out how to fix the issue.
function getEmail() {
var label = GmailApp.getUserLabelByName("MyLabel");
var threads = label.getThreads();
Logger.log(label);
//get first message in first thread
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 To=message.getTo();
Logger.log(To);
Logger.log(message.getFrom());
Yes, there is more to this script but this section is the part with the issue.

permanently delete only one gmail message from a thread using a google script

I want to permanently delete a Gmail message inside a thread already in the trash.
I merged a few scripts around there, so I can delay and track emails. It works by saving a draft, then the script copy the draft into a new email, send it at the specified time and send the original draft to trash. The problem is that once in a while, the drafts that are in the trash are sent again (i haven't been able to figure out why yet)...
As a workaround, I was using the following code that that was originally posted here: delete forever emails 1:
function cleanUp() {
var threads = GmailApp.search("in:trash is:draft");
Logger.log(threads.length);
for (var i = 0; i < threads.length; i++) {
Logger.log(threads[i].getId());
Gmail.Users.Message.remove('me',threads[i].getId());
}
}
This was working fine, until a while ago. If the draft was inside a thread with more than 1 message, only the draft was deleted... I got now an error on line 6 that says: "Cannot call method "remove" of undefined".
In this post: delete forever emails 2, it is suggested to replace line 6 by
Gmail.Users.Threads.remove('me',threads[i].getId());
This dosn't get any errors, but if the draft is in a thread with more than one message, the whole thread is deleted instead of only the draft...
So, is there a way to get only the draft erased?
I tried calling the message id of the draft inside the thread and use the original line 6:
function cleanUp2() {
var threads = GmailApp.search("in:trash is:draft");
Logger.log(threads.length);
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
Logger.log(messages.length);
for (var j = 0; j < messages.length; j++){
if (messages[j].isDraft()){
Logger.log('id msg: ' + messages[j].getId());
Gmail.Users.Message.remove('me',messages[j].getId());
}
}
}
}
But I got the same error, now on line 10...
I also tried using this function:
function deleteMessage(userId, messageId) {
var request = gapi.client.gmail.users.messages.delete({
'userId': userId,
'id': messageId
});
request.execute(
function(resp) { });
}
That you can find in the developers page of google: here. In the "try this API" section it works, but in my implementation i got an error on line 2 that says (translated from Spanish so i don't know if it will be exact): "a name (?) is missing behind (after?) operator "."" And if i copy the function in a separated tab, i can save it and the same error is showed...
Any help will be appreciated...
Regards,
i finally made it trough an http request:
function cleanUp2() {
var threads = GmailApp.search("in:trash is:draft");
Logger.log(threads.length);
var userId = 'xxxxx#gmail.com';
var options = {
'method' : 'delete',
'muteHttpExceptions': true
};
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
Logger.log(messages.length);
for (var j = 0; j < messages.length; j++){
if (messages[j].isDraft()){
Logger.log('id msg: ' + messages[j].getId());
var url = 'https://www.googleapis.com/gmail/v1/users/' + userId + '/messages/' + messages[j].getId();
var response = UrlFetchApp.fetch(url,options);
Logger.log(response);
}
}
}
}

Removing label from Gmail email after X days using Google Apps Script

I created a Google Apps Script Code.gs as follows to remove the Gmail label from every thread that is older than X days and labeled Y.
function archiveYThreads() {
// Every thread, older than two days, and labeled "Unread Feeds".
var threads = GmailApp.search('label:"Unread Feeds" older_than:2d');
for (var i = 0; i < threads.length; i++) {
threads[i].removeLabel("Unread Feeds");
}
}
According to the documentation, the function removeLabel exists. Alternatively, I found some sources that use deleteLabel. However, with both I get the error that both functions do not exist, after having set a time-based trigger:
Can anybody please help me detecting why the function does not work?
You have to supply an object of type GmailLabel as the argument to removeLabel() method. Try this snippet.
function archiveYThreads() {
var label = GmailApp.getUserLabelByName("Unread Feeds");
var threads = GmailApp.search('label:"Unread Feeds" older_than:2d');
for (var i = 0; i < threads.length; i++) {
threads[i].removeLabel(label);
}
}

google apps script and GmailApp: get just new messages

I'm trying to implement a simple google script that processes each message that is received by a Gmail user.
I've found an example that does something like this:
var threads = GmailApp.getInboxThreads();
for (var i=0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var j=0; j < messages.length; j++) {
if (!messages[j].isUnread()) {
continue;
}
//process message
}
}
That is: I iterate through all messages in the inbox and search for the unread ones. This is very slow on just 1800 messages.
Ideally, I'm looking for a trigger that gets fired once each new message is received.
If there is no such thing, I would try to make use of this that I saw:
GmailApp.getMessageById(id)
Sorry for the late response but I just had the same type of problem and I ended up using GmailApp.search() ... hope this helps.
// find unread messages
var threads = GmailApp.search('is:unread');
....
WARNING
This call will fail when the size of all threads is too large for the system to handle. Where the thread size is unknown, and potentially very large, please use the 'paged' call, and specify ranges of the threads to retrieve in each call.
Take a look at GmailApp.search(query) and
GmailApp.search(query, start, max)
Unfortunately there in no trigger that fires for each recieved message. There is however a good workaround:
Set up a filter rule that assigns a special label, "ToBeProcessedByScript" as example, to all incoming messages. Since wildcards don't really work in Gmail filters use the to: field.
Run a time-triggered script that collects all new message threads with GmailApp.getUserLabelByName("ToBeProcessedByScript").getThreads(). Remove the special label just before processing the new messages.
you can use
GmailApp.getInboxThreads(0, 50);
to initialize the variable with first fifty mail.
I have extended the code with checking if the first message is really the one which is unread. If it is not it will check the next message and will continue untill it finds the unread message:
function getUnreadMails() {
var ureadMsgsCount = GmailApp.getInboxUnreadCount();
var threads;
var messages;
var k=1;
if(ureadMsgsCount>0)
{
threads = GmailApp.getInboxThreads(0, ureadMsgsCount);
for(var i=0; i<threads.length; i++)
{
if(threads[i].isInInbox())
{
messages = threads[i].getMessages();
for(var j=0; j<messages.length; j++)
{
while (messages[j].isUnread() === false)
{
threads=GmailApp.getInboxThreads(k, ureadMsgsCount);
messages = threads[i].getMessages();
k++;
}
Logger.log(messages[j].getSubject());
// process unread message
}
}
}
}
}
You can create a time trigger as djtek mentioned but instead of labeling all messages and then retrieve labeled messages, you can just get the number of the unread messages, and retrieve threads from 0 to the number of the unread messages, following a code that works for me:
function getUnreadMails() {
var ureadMsgsCount = GmailApp.getInboxUnreadCount()
if(ureadMsgsCount>0)
{
var threads = GmailApp.getInboxThreads(0, ureadMsgsCount);
for(var i=0; i<threads.length; i++)
{
if(threads[i].isInInbox())
{
var messages = threads[i].getMessages();
for(var j=0; j<messages.length; j++)
{
Logger.log(messages[j].getSubject());
// process unread message
}
}
}
}
}
function getUnreadMessages(threadLimit) {
function flatten(arr) { return [].concat.apply([], arr) }
var threadsWithUnreadMessages = GmailApp.search('is:unread', 0, threadLimit)
var messageCollection = threadsWithUnreadMessages.map(function(thread) {
return thread.getMessages()
})
var unreadMessages = flatten(messageCollection).filter(function(message) {
return message.isUnread()
})
return unreadMessages
}
getUnreadMessages(100)