Gmail App: Find threads which have a calendar entry - google-apps-script

Some messages have calendar entries at the top of them like this:
In GScript, is there any way to find messages which have these calendar entries? If so, how does one extract the information from it?
Update
I took a lucky shot and found that it's classed as an attachment (of type ics), but there still remains the issue of extracting the data from this file...

Try this:
function getIcs(){
var threads = GmailApp.getInboxThreads();
var thread, messages, message, attachments, attachment;
var result = [];
for (var i = 0; i < threads.length; i++){
messages = threads[i].getMessages();
for (var j = 0; j < messages.length; j++){
message = messages[j];
attachments = message.getAttachments();
for (var k = 0; k < attachments.length; k++){
attachment = attachments[k];
if(attachment){
if (attachment.getContentType() == "application/ics"){
Logger.log("found ics");//continue;
result.push(message);
}
}
}
}
}
return result;
}
This method will view every thread, every message in it and every attachment in each message, and returns a list of the message objects that have an attachment of type application/ics.
UPDATE
In addition to my previous code, the following function will return an array of event objects based on the attachments found in the other method:
function getSutff(){
var msgs = getIcs();
var ics;
var position;
var eventId;
var event = [];
for (var i = 0; i < msgs.length; i++){
ics = msgs[i].getAttachments()[0].getDataAsString();
position = (ics.search("UID:")) + 4;
eventId = ics.substr(position,26);
event.push(CalendarApp.getEventById(eventId));
}
Logger.log(event)
return event;
}

Related

I want to make an script to move messages to a label

`
`function moveEmailsToLabel() {
var labelName = "My Label"; // Replace with the name of the label you want to move the messages to
var label = GmailApp.createLabel(labelName);
var threads = GmailApp.search("subject:My Subject"); // Replace with the search query that returns the threads you want to move to the label
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
var messages = thread.getMessages();
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
GmailApp.moveMessageToLabel(message, label); // it seems like this does not work
}
}
}``
Hello,
With app script I want to move all open threads to a label ( folder) not just mark them.
but I am not getting it right.

Google Apps Script Why it only searches for one (first) attachment

I have two codes, the first of which works and shows all attachments and the second shows only the first. "getAttachmentAndMessage" is used by several of my scripts so I need to have it in a separate function in order not to duplicate my configuration. Please help.
//this function get me all attachments
//this function is just an example of how I built my code below;
function searchEmailsData(){
var search = GmailApp.search('in:inbox newer_than:3d');
var threads = GmailApp.getMessagesForThreads(search);
for (var i = 0 ; i < threads.length; i++) {
var thread = threads[i];
for (var j = 0; j < thread.length; j++) {
var message = thread[j];
var attachments = message.getAttachments();
for (var k = 0; k < attachments.length; k++) {
var attachment = attachments[k];
Logger.log(attachment.getName());
}
}
}
}
//this functions get me only first attachment but I need all of them;
function searchEmailsData2(){
var search = GmailApp.search('in:inbox newer_than:3d');
var threads = GmailApp.getMessagesForThreads(search);
for (var i = 0 ; i < threads.length; i++) {
var thread = threads[i];
var obj = getAttachmentAndMessage(thread);
Logger.log(obj[1].getName())
}
}
function getAttachmentAndMessage(thread){
for (var j = 0; j < thread.length; j++) {
var message = thread[j];
var attachments = message.getAttachments();
for (var k = 0; k < attachments.length; k++) {
var attachment = attachments[k];
var obj = [message, attachment];
return obj;
}
}
}
In order to return not the first attachment but rather all of them, you can modify the getAttachmentAndMessage() function as follows:
function getAttachmentAndMessage(thread) {
var results = [];
for (var j = 0; j < thread.length; j++) {
var message = thread[j];
var attachments = message.getAttachments();
var obj = {'message': message, 'attachments': attachments};
results.push(obj);
}
return results;
}
The above will return a list of objects, in which each objects contains a property message, and a property attachments (which is a list of attachments).
In order to return a list containing only the attachments in the thread, you can use the following variation of the code above:
function getAttachmentAndMessage(thread) {
var results = [];
for (var j = 0; j < thread.length; j++) {
var message = thread[j];
var attachments = message.getAttachments();
results = results.concat(attachments);
}
return results;
}
Your original code
In your original getAttachmentAndMessage() code, you were using a return statement inside of the for loop. That means that upon finding the first message (first iteration of the outer loop) and upon finding its first attachment (first iteration of the inner loop) the function would simply return one value, which is a list containing the first message and its first attachment.

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.

Exact each gmailapp threads subject line one at a time in google apps script

I want to set the variable "subject" to be the subject line of each email that matches my GmailApp.search criteria. Right now when it searches, it stores every subject line into the "subject" variable and I am unsure how to make it so I can perform subject[0] for the first subject, subject[1] for the second subject, etc
var threads = GmailApp.search("label:bills")
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var m = 0; m < messages.length; m++) {
var subject = messages[m].getSubject()
}
}
If I have 2 emails that match the "bills" label, it will store both subject lines under "var = subject".
Instead of assigning the subject to a variable in you for loop, you need to push them into an array then you can access them like you wanted to with subject[1].
var subject = new Array();
var threads = GmailApp.search("label:bills")
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var m = 0; m < messages.length; m++) {
subject.push(messages[m].getSubject());
}
}

Count number of email sent to a person

I am trying to get the number of times that a person has been sent an email, so I can keep track of leads, for example
user#domain.com was emailed 5 times
user2#domain.com was emailed 3 times
From reading the docs I cannot figure this one out
Is this possible?
EDIT:
function myFunction(e) {
e = "someemail#gmail.com";
var threads = GmailApp.search("to:" + e + "");
var count = 0;
for (var i = 0; i < threads.length; i++) {
count++;
}
Logger.log(count) //1
}
This gives me threads but no number of messages
I think you are on the right lines using search() to find the sent items. This is a snippet I use to scan through all the unread emails in the inbox, and the processing that email. You'll want something similar:
var totalThreads = GmailApp.getInboxThreads().length
// Read in READ_AT_ONCE pages of email at a time
for (var pageindex = 0; pageindex < totalThreads; pageindex += READ_AT_ONCE) {
var threads = GmailApp.getInboxThreads(pageindex, READ_AT_ONCE)
var msgs = GmailApp.getMessagesForThreads(threads)
var numThreads = threads.length
// Read in all the threads in the batch
for (var threadIndex = 0; threadIndex < numThreads; threadIndex++) {
var numMsgs = msgs[threadIndex].length
// Process each msg in the thread
for (var msgIndex = 0; msgIndex < numMsgs; msgIndex++) {
var msg = msgs[threadIndex][msgIndex]
if (!msg.isUnread() || msg.isInTrash()) {
continue
}
Email_.processMsg(msg)
} // for each msg
} // for each thread
} // for each batch