Extract the exact subjects and update the google sheets using apps script - google-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++;
}
}
};

Related

Apps script - google sheet ,need a help in user form

I'm working on an apps script for my google spreadsheet. In that, I want a search feature that will search in data and brings back it to the user form for updating. I did code with no error but still, the output is not completed. Please help me with this guys!!
this is the code-
var SEARCH_COL_IDX = 0;
function search() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var e = ss.getSheetByName("ENTRY"); //Form Sheet
var str = e.getRange("G7").getValue();
var values = ss.getSheetByName("DATA").getDataRange().getValues();
for (var i = 2; i < values.length; i++) {
var row = values[i];
if (row[SEARCH_COL_IDX] == str) {
e.getRange("D6").setValue(row[0]);
e.getRange("H4").setValue(row[1]);
e.getRange("D4").setValue(row[2]);
e.getRange("D7").setValue(row[3]);
e.getRange("D8").setValue(row[4]);
e.getRange("D12").setValue(row[5]);
e.getRange("D13").setValue(row[6]);
e.getRange("D14").setValue(row[7]);
e.getRange("D15").setValue(row[8]);
e.getRange("D16").setValue(row[9]);
e.getRange("H12").setValue(row[10]);
e.getRange("H13").setValue(row[11]);
e.getRange("H14").setValue(row[12]);
e.getRange("H15").setValue(row[13]);
e.getRange("H16").setValue(row[14]);
e.getRange("H17").setValue(row[15]);
} }}

Get email size using google apps script

I have written this to extract last 500+ message from gmail into spreadsheets.
function myFunction() {
var ss = SpreadsheetApp.getActiveSheet();
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++)
{
var frm = messages[j].getFrom();
var to = messages[j].getTo();
var dat = messages[j].getDate();
ss.appendRow([frm, to, dat])
}
}
}
Is it possible to get the size of each email (with attachment and without attachment)? When I look into documentation of Gmail message method, there is no any function I could use.
Thanks to #s1c0j1 and #TheMaster. I could get the size of an email by using this function.
var size = Utilities.newBlob(messages[j].getRawContent()).getBytes().length;
the user.messages.get returns a user.messages.response which contains a parameter called "sizeEstimate"
sizeEstimate integer Estimated size in bytes of the message.
You will need to remember that this is an estimate.

Get url of the google form every time after submission is done

I am using a google script to get the url of the Google form successfully to Google spreadsheets. However, I must manually click run in google script in order to get the url.
How to get the url automatically when someone click the submit button in Google form?
Below is the code of my script
function assignEditUrls() {
var form = FormApp.openById('1F4YdKMGlJXn9w8aiq0UXzegWEfzjxUMd2CV2vewmoLQ');
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1');
var data = sheet.getDataRange().getValues();
var urlCol = 37;
var responses = form.getResponses();
var timestamps = [], urls = [], resultUrls = [];
for (var i = 0; i < responses.length; i++) {
timestamps.push(responses[i].getTimestamp().setMilliseconds(0));
urls.push(responses[i].getEditResponseUrl());
}
for (var j = 1; j < data.length; j++) {
resultUrls.push([data[j][0]?urls[timestamps.indexOf(data[j][0].setMilliseconds(0))]:'']);
}
sheet.getRange(2, urlCol, resultUrls.length).setValues(resultUrls);
}
I also have set the project triggers to on form submit
However, I still can't automatically get the url of the google form even submitted. I have to manually click run the script in order to get the url in spreadsheet
To use the "on form submit" spreadsheet trigger, you need to first make sure that the form is saving responses to that spreadsheet. You can then use the event object to replace some of the literal values you've declared.
Unfortunately, the trigger doesn't provide the response ID, so you'll need to manually search for it using the provided timestamp.
function assignEditUrls(e) {
var sheet = SpreadsheetApp.getActive().getSheetByName("Form Responses 1");
var ts = new Date(e.namedValues.Timestamp[0]);
var form = FormApp.openById("1F4YdKMGlJXn9w8aiq0UXzegWEfzjxUMd2CV2vewmoLQ");
var formResponses = form.getResponses(ts);
var editURLColumn = 37;
var row = e.range.getRow();
for (var i = 0; i < formResponses.length; i++) {
var formResponseTs = (new Date(formResponses[i].getTimestamp())).getTime();
if (formResponseTs === ts.getTime()) {
var responseId = formResponses[i].getId();
var editURL = formResponses[i].getEditResponseUrl();
sheet.getRange(row, editURLColumn).setValue(editURL);
break;
}
}
}

How to parse gmail with specific subject line and get populated in the google sheets

The Code which i have tried is not able to get the email with specific subject line please let me know what is the mistake in the script mentioned below
Tried the Below mentioned Script but still not working:
function amy() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var threads = GmailApp.search('subject: "New submission from Free Diagnosis*"');
var a=[];
for (var i = 0; i < threads.length; i++) {
var messages = GmailApp.getMessagesForThread(threads[i]);
for (var j = 0; j < messages.length; j++) {
a[j]=parseMail(messages[j].getPlainBody());
}
}
var nextRow=s.getDataRange().getLastRow()+1;
var numRows=a.length;
var numCols=a[0].length;
s.getRange(nextRow,1,numRows,numCols).setValues(a);
}

GoogleScript Mail Parser - Only retrieve emails for past X days

Built a basic email parser so that I can get in gSheet my emails, however though, I have > 50k emails and I only need to retrieve them for the past 7 days each time I'll run the parser. Looked around but weren't able to find any similar issue
This is the script I have
var sheet = SpreadsheetApp.getActiveSheet();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
function getEmails() {
var label = GmailApp.getUserLabelByName("HOUSE_PHONELEAD");
var inc = 100;
var start = 0;
var row = 2;
var threads = label.getThreads();
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].getPlainBody());
row++;
Utilities.sleep(1000);
}
}
}
function onOpen() {
var menuEntries = [ {name: "Load Emails", functionName: "getEmails"} ];
spreadsheet.addMenu("Email", menuEntries);
}
Try to use
var threads = GmailApp.search('newer_than:7d');
instead of
var threads = label.getThreads();
search method allow to pass a string query :
https://developers.google.com/apps-script/reference/gmail/gmail-app#searchquery
the query string must be a gmail specific operator :
https://support.google.com/vault/answer/2474474?hl=en