I get every day multiple automatic emails. One has the subject 'Daily Report' and one has the subject 'Daily Report details'. This is the script I was using, but once the emails started coming without chronological order, I sometimes catch the wrong one.
var threads = GmailApp.search('Daily Report') //search gmail with the given query(partial name using * as a wildcard to find anything in the current subject name).
var msgs = GmailApp.getMessagesForThreads(threads);
Is there a way to tell the search that I want the mail with exact subject 'Daily Report' and not the one with 'Daily Report Details'?
Thanks
Like #Pierre-Marie Richard and #Cooper mentioned, you can use the built-in Gmail search functions. Here are a list of the Gmail Search Functions, like #Cooper mentioned you may be best using something like:
var threads = GmailApp.search('subject:"Daily Report"-Details')
var msgs = GmailApp.getMessagesForThreads(threads);
The quotes ("") work as an exact match and the minus ('-') is to exclude a word.
If that still doesn't work let me know and I will look further into it.
Good Luck :)
Related
I’m writing to you because I’m facing a problem with an automatic email script based on Google Form submission (or new GSheet entry when GForm is sent). Process in short:
I have a Google Form (here's similar one) that is linked to Data Studio and Power BI Dashboards. Based on data that goes into spreadsheets, the forms are automatically pre-filled using calculated fields in dashes (Date, ActionID, Code, ID, Location etc.) and data goes to Dash-linked spreadsheet [Example of what it might look like]. The thing is that in case of errors found during inspections, the copy of the form/email should be also sent to Employee (optionally, the person that is superior to them, so that both know the details, not only the auditor who fills-in the form – just option). I can’t use any available Add-ons for GForms due to sensitive nature of data. Auditors don’t have edit access to Forms/Dashes, and sending all information via email would take too long. I also created a test Google Form to demonstrate what it looks like.
All in all, What I’m looking for is a dynamic script that will also grab company email of the employee (supervisor is optional), and when auditor submits the form, the email/copy of the form also goes to those mentioned aboce. I couldn't find anything GForms related.
I used a similar script before, but don’t think it was based on new rows added to spreadsheets [Example]. If it’s not possible to achieve something like this, even spreadsheet entry form/email would be really greatly appreciated (email based on "EmployeeEmail" from form + selected columns included, as marked in sheet), although I’m afraid of things going wrong in a file with lots of rows. I tried to find something similar and discovered 2 scripts, but I don’t know how to make them dynamic: The first one below just sends messages to emails from a given range and it’s static (range).
thanks! It looks good, but I have 2 issues.
Edit:, it always needs that dummy email ('xxx#xxx.xxx'”, which means I’ll (or sb else) be getting hundreds of emails. Can we somehow get rid of this line or replace it with form sender email (I can replace existing column with one with form sender email (google form add emails automatically)/those that are already in columns 2/3?
2nd thing, how can I concatenate dummy message lines to variables:
var message = row.join('\n'); (currently adds values from row n)
e.g. can we somehow replace text join with e.g.
// Info from specific columns
var Date = lastRowValues[n][5]; //I don’t know how to grab last value from columns, it’s probably something like variable.getLastRow() + variable.getLastColumn() + Get values
var ID = lastRowValues[n][6];
var Code= lastRowValues[n][8];
var Action = lastRowValues[n][9];
var Location = lastRowValues[n][10];
var ErrorType = lastRowValues[n][11];
and then in message
// var = message = “You’ve made an error. Please find the details below.” +
“\n “ +
“\n Date of Action: “ + Date +
“\n ID: “ + ID +
“\n Action Code: “ Code +
“\n Type of Action: “ + Action +
“\n Location: “ + Location +
“\n Error Type: “ + ErrorType +
The rest, such as the title, may stay the same. I realised that numbers only are hard to read. Is it possible to achieve this output? This is what it looks like now.
You can add this code in your linked spreadsheet:
// this function will fires automatically
// it grabs all data from the last row and send it via email
function send_email() {
var row = SpreadsheetApp.getActive().getDataRange().getValues().pop();
var message = row.join('\n');
var subject = 'test subject';
var address = 'xxx#xxx.xxx';
GmailApp.sendEmail(address, subject, message);
// additional emails
var address2 = row[2];
GmailApp.sendEmail(address2, subject, message);
var address3 = row[3];
GmailApp.sendEmail(address3, subject, message);
}
// this function should be run just once to install the trigger
function install_onFormSubmitTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger('send_email').forSpreadsheet(ss).onFormSubmit().create();
}
After you install the trigger every time your form was submitted all data from the form (= all cells from the last row on the sheet) will be sent to xxx#xxx.xxx. And to two additional addresses from cells 'C' and 'D'.
Feel free to change the code as you need.
I want to return a list of all email subjects received at a specified day, at my Gmail account. If there is more than one message with the same subject, I want to have a number of how many there are. This is what I did:
var emailThreads = GmailApp.search("subject:<partial_subject> after:2020/8/30 before:2020/8/31");
var messages = GmailApp.getMessagesForThreads(emailThreads);
This returns "an array of arrays of messages, where each item in the outer array corresponds to a thread and the inner array contains the messages in that thread".
I try to access data I need like this:
for (i = 0; i < messages.length; i++) {
messages[i][0].getSubject() // a subject
messages[i].length // number of messages with the same subject
}
However, this returns more messages than is displayed in Gmail UI.
Here's what I've noticed:
it only happens to threads with more than one message
it doesn't happen to all threads with more than one message
every time a run my script for the same day, the result is the same
when I run my script for different days, the result is different even though the same emails were received
Considering that this code is very simple and I'm only using methods delivered by google, I assume I must have misunderstood something or Gmail is doing something weird with messages in threads.
Any ideas what could this be and how to work around it?
Edit:
I've noticed that there is a "Don't include chats" checkbox in gmail filter, which adds "-in:chats" into search query, but it didn't help either.
I also tried adding "in:inbox" to the query, but with no change in the result.
Explanation:
The reason you are getting too many messages is because your current
filter criteria does not query only emails but also hangout
messages or other threads rather than emails.
Use this to get the subjects of all emails:
var emailThreads = GmailApp.search("is:inbox after:2020/8/30 before:2020/8/31");
Solution:
This is how the full code would look like:
function myFunction() {
var emailThreads = GmailApp.search("is:inbox after:2020/8/30 before:2020/8/31");
var messages = GmailApp.getMessagesForThreads(emailThreads);
var messageArr = [];
for (i = 0; i < messages.length; i++) {
Logger.log(messages[i][0].getSubject()) // a subject
Logger.log(messages[i].length) // number of messages with the same subject
}
messages.forEach(m=>{
messageArr.push({
subject: m[0].getSubject(),
num_messages:m.length
})});
}
I also created for you a json array messageArr which contains all the
subject names and the number of message for each subject.
From the question
However, this returns more messages than is displayed in Gmail UI.
There might be some reasons for that
Messages in Trash and Spam are not displayend on the search results on the Gmail UI (unless you include in:anywhere, textually)
The UI is not showing all messages because the results might be paged and you missed that
After the receive/import/deletion of a large number of messages there is a noticible delay on getting them displayed on the UI, so even if it looks that you reached the last search results page, wait some minutes and regresh the Gmail webpage.
getMessagesForThreads return messages that might not be shown on the Gmail UI search results because they were sent to the Spam or Trash.
To correctly compare the results from the Gmail UI with the results of this method, on the Gmail UI include in:anywhere as part of your search. The complete search statement will look something like this:
in:anywhere subject:<partial_subject> after:2020/8/30 before:2020/8/31
As mentionend on the first part of this answer, you might have to refresh the Gmail webpage several times to be sure that there aren't messages recently made available through the web UI.
Related
How to keep gmail threads from coming back
Resources
Search operators you can use with Gmail
I have looked on SO and researched my many questions on Google, but did not find anything that provided a direct answer. I am setting up code to email a sheet one (or many) times. I am having trouble setting the email address of the recipient(s). I need the script to send out one email for each address in the data range (P122:P126).
I regret that, for privacy reasons, I am unable to share the entire script. (On a positive note, everything except this works.)
The active sheet is "var sourceSheet"
The number of email addresses is a number in cell S121 on the active sheet
All of the email addresses needed are in a vertical column on the active sheet ("P122:P126")
Here is the relevant section of code:
// Sets the recipient of the email message
var numRows = sourceSheet.getRange("S121:S121").getValues(); // Number of rows to process
// Fetch the range of cells P122:P126 that contain email addresses
var dataRange = sourceSheet.getRange(122,16,numRows,1);
// Fetch values for each row in the Range.
What needs to be done to obtain one (or more) email addresses usable by the "GmailApp.sendEmail" statement? No matter how I analyze the problem, it seems to me (and I may be wrong) that a loop is necessary, and I don't know how to use the information I have to create the actions needed.
Try this:
var numRows=sourceSheet.getRange("S121").getValue();
var dataRange=sourceSheet.getRange(122,16,numRows,1);
I am using Google App Scripts to search and reply to a going emails.
Let's say I can use GmailApp.search to search a thread by subject already and I want to reply to the first search result
I want to reply to: and cc: to specific people every time search and reply this going email. Like: to: a#gmail.com + cc: b#gmail.com, c#gmail.com.
Now I am using thread[0].replyAll because I don't know how to do as above. But always it reply to all the people of the last message of the going email ( even including me in to the cc or me in the to: if I am the last one doing message of the total email)
var subjectsearch =brandname+"/ SAMBU"+"/ MATERIAL REQUEST/ ROUND "+round+'/ '+subjectname;
var threads = GmailApp.search('subject:('+subjectsearch+')');
if (threads.length>0){
threads[0].replyAll(messagePrefix,{from:"kevin#sambu.sewing.co.kr",htmlBody: message, })
first message
If I replyall it will look like this, I dont want to send to me but always a specific to: and cc: if I want
I'm making a website for school to connect students and teachers. Teachers fill out a Google Form asking for service (grading papers...) and it is connected to Google Spreadsheets. Then a student can see that Spreadsheet and sign up on a different Google Form to help the teacher, and that response is recorded in the same Google Spreadsheet. When the student submits his/her Google Form, I want the teacher to be notified by email that a student signed up to help them. How can I send that notification? And I don't want the notification to come from my own school address. Is that possible?
Here is the Google website: https://sites.google.com/fcpsschools.net/jmhsservicesignup/subjects/math
This is the code I have so far:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Tutors
Signed Up");
var startRow = sheet.getLastRow(); // First row of data to process
var dataRange = sheet.getRange(startRow, 1, 1, 5)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[3]; // Second column
var tutor = row[0];
MailApp.sendEmail(emailAddress,"Tutor Request Filled",""+tutor+" has accepted your request to be tutored! Please notify"+tutor+" if anything changes.");
}
}
The getLastRow doesn't seem to work, and the email sends from my own address, not a Google forms notification if that is even possible.
If this is impossible I might just make the students send the email to the teacher, but I really want to automate the process. If you can think of a workaround, that would also be awesome.
Adding to what Sandy Good and Guilherme mentioned.
Here are a few more tips:
Not clear what data you're trying to get in your function. Are you trying to grab the data from the form submission? In that case you need to set up a trigger from the scripts menu, and connect it to the function (function needs to have an 'event' argument, usually called e).
function processFromSubmission(e) {//get data from e}
Apologies if you're not trying to do that. What happens when you Log sheet.getLastRow()?
Ask the school if they can get you another email address to use for this form, something like notifications#yourschool.edu. Follow the steps here to set up your alias. Do it for the gmail account that "owns" the spreadsheet these forms are sending data to.
Make sure your alias is set up properly. You should be able to see it in the "from" dropdown menu when you compose an email. Also try running the code Logger.log(GmailApp.getAliases()) and check the log to see that it's available.
Use GmailApp.sendEmail to send email, not MailApp.sendEmail. They're almost the same, but it looks like the MailApp version doesn't let you send from an alias. You can specify the 'replyTo' email as well if you want to set it to something else, like the student filling out the form or whatever (still not clear exactly what you're trying to set up!).
Send email like:
GmailApp.sendEmail(emailAddress, subject, body,
{replyTo: replyToEmailAddress_can_be_any_email,
from: 'notifications#yourschool.edu'
});
Note: your link didn't open.