I want to know how to extract specific parts from Gmail using GAS.

Asked 1 years ago, Updated 1 years ago, 61 views

I would like to extract the following OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO

Products:
------------------------------------------------------------------
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO

In the e-mail, you will receive the name of the item with the dotted line after the item:I can extract other parts using the match method, but I don't know how to extract them because the product name doesn't come after the product.There are many more types of products, and even if you use the match method, the spreadsheet will only spread sideways.So I would like to know how to extract only certain lines of OOOOOOOO.

I would like to specify the line (actual product name) that comes after the dotted line and have it written on the extraction spreadsheet.

The scripts I use are as follows:

function fetchContactMail(){

    var strTerms='(is:unread "OOOOOOOOOOOOOOOOOOO);
    varmyThreads=GmailApp.search(strTerms,0,10); 
    var myMsgs=GmailApp.getMessagesForThreads(myThreads); 

    varvalMsgs=[];

    for (vari=0;i<myMsgs.length;i++){

        valMsgs[i]=[];

        valMsgs[i][0] = myMsgs[i][0].getDate();
        valMsgs[i][1] = myMsgs[i][0].getFrom();
        valMsgs[i][2] = myMsgs[i][0].getSubject();
        valMsgs[i][3] = myMsgs[i][0].getPlainBody().match(/OOOOO(.+)/); 
        valMsgs[i][4] = myMsgs[i][0].getPlainBody().match(/OOOOOOO(.+)/); 
        valMsgs[i][5] = myMsgs[i][0].getPlainBody().match(/OOO(.+)/);  
        valMsgs[i][6] = myMsgs[i][0].getPlainBody().match(/OOO(.+)/); 
        valMsgs[i][7] = myThreads[i].getPermalink();

        myMsgs[i][0].markRead(); 

    }

    if(myMsgs.length>0){

        varmySheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('OOO'); 
        varmaxRow=mySheet.getDataRange().getLastRow();
        mySheet.getRange(maxRow+1,1,i,9).setValues(valMsgs); 

    }  
}

google-apps-script

2022-09-30 21:41

1 Answers

The 0000... portion of the above format can be retrieved using the following regular expressions:

/Product:\n-+\n(.*)/

This takes advantage of the fact that \n means a new line.


2022-09-30 21:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.