Reputation: 157
I am new to Google Scripting.
I created a script where Google Spreadsheet should connect to GMail. Everything is working well.
My problem is, I need to check if an email content has the word "ORDER". If yes, it should get the word "ORDER" and the numbers after it.
For example, an email contains "ORDER 90104" in the message body, then the Google script should insert ORDER 90104 to a column in the sheet. An example email could be:
Hi, the customer is requesting for ORDER 90104. Thanks.
Is this even possible? As of now, I used getPlainBody() and I can get all the contents of the email and put it in a column. My plan was to check that column and look for the word ORDER. Though I can't find/end with a solution for it.
Here's my working code getting the email content of the 1st three emails:
function getGMail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var threads = GmailApp.getInboxThreads(0,3);
cell = sheet.getRange("A1");
for (var i = 0; i < threads.length; i++) {
threadMessage = threads[i].getMessages();
var emailContent = threadMessage[0].getPlainBody();
cell.offset(i,0).setValue(emailContent);
}
}
Any advice or comments will be greatly appreciated.
Thanks!
Upvotes: 1
Views: 1872
Reputation: 3337
Hello theladydevbugger,
As you didn't gave a lot of code my answer will be quite short: yes you can do that!
How: use a regexp: /ORDER [0-9]+/g.
How to use the regexp: bellow a sample code
var emailBody = yourMail.getPlainBody(); // supposing "yourMail" is the mail you retrieved
var isThereAnyOrder = false;
if(mailBody.search(/ORDER [0-9]+/g)>-1){
isThereAnyOrder=true;
var order=mailBody.match(/ORDER [0-9]+/g)>-1)[0];
Logger.log(order);
// do something with the order
}
else{
// there is no order do nothing
}
Upvotes: 1