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.
Related
`
`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.
How to get gmail messages received after a particular date using Google Apps Script?
Here is my current code.
function getGmailEmails(){
var label = GmailApp.getUserLabelByName("TestLabel");
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];
extractDetails(message);
}
}
}
I know we can set limits with getThread(0,50) for example but how can I do it so I can retrieve all my gmails from a specific period of time while also getting it within a certain label from my inbox?
Messages After a Date:
function getGmailEmailsAfter(date){
const label = GmailApp.getUserLabelByName("TestLabel");
const threads = label.getThreads();
const dtv = new Date(date.getFullYear(),data.getMonth(),date.getDate()).valueOf();
for (let i = threads.length - 1; i >=0; i--){
let messages = threads[i].getMessages();
for (let j = 0; j <messages.length; j++){
let message = messages[j];
let dt = new Date(message.getDate())
if(new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf() > dtv) {
extractDetails(message);
}
}
}
}
I made a script that forwards emails to different people when clicking on a card button. This is the main part of the code:
function forwardtest() {
var threadsa = GmailApp.search('label: Inbox label: product-a');
for (var i = 0; i < threadsa.length; i++) {
var recipient = 'producta#gmail.com';
var messages = threadsa[i].getMessages();
for (var j = 0; j < messages.length; j++) {
var body = messages[j].getBody();
messages[j].forward(recipient,{htmlBody: body});
}
GmailApp.moveThreadToArchive(threadsa[i]);
}
var threadsb = GmailApp.search('label: Inbox label: product-b');
for (var i = 0; i < threadsb.length; i++) {
var recipient = 'productb#gmail.com';
var messages = threadsb[i].getMessages();
for (var j = 0; j < messages.length; j++) {
var body = messages[j].getBody();
messages[j].forward(recipient,{htmlBody: body});
}
GmailApp.moveThreadToArchive(threadsb[i]);
}
}
Sometimes, we'll need to change the forwarding addresses. Since I don't want people to change it directly in the code, I made a Google Sheets in which the forwarding addresses are listed depending on the email labels:
Product label
Forward address
product-a
producta#gmail.com
product-b
productb#gmail.com
Is there a way to replace the var recipient = 'producta#gmail.com'; with "Look for the cell containing "product-a" content and define recipient as the content in the cell which is next to it"?
Thank you very much in advance!
Solution:
Since you will be referencing a Google Sheet to fetch the data, you would need to use Sheets API starting from the SpreadsheetApp class. Also, since this script is not attached to the sheet from your use case, you would need to open the sheet by it's ID, which is in the URL link:
https://docs.google.com/spreadsheets/d/<SHEET-ID>/edit#gid=0
Take note of the SHEET-ID since we will use it in the code.
For the code itself, I made a separate functions for finding the recipient and sending the emails for modularity:
EDIT: Rearranged the code, moved the function into a for loop instead.
function forwardtest() {
var sheet = SpreadsheetApp.openById("SHEET-ID").getSheetByName("Sheet1");
var prodList = sheet.getRange(2,1,sheet.getLastRow()-1,2).getValues();
for (var k = 0; k < prodList.length; k++) {
var prod = prodList[k][0];
var recipient = prodList[k][1];
var threads = GmailApp.search('label: Inbox label: '+prod);
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var j = 0; j < messages.length; j++) {
var body = messages[j].getBody();
messages[j].forward(recipient,{htmlBody: body});
}
GmailApp.moveThreadToArchive(threads[i]);
}
}
}
Note: Replace the SHEET-ID with the sheet ID from the URL.
Sample Output:
Using a sample table with a variable number of products:
References:
Class SpreadsheetApp
Class Sheet
Class Range
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.
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;
}