Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pulling numbers between white space from email body into google sheets

I am trying to pull a 6 digit number(authentication code)between white space from the email body into a google sheet. "date, sender, and subject" are all populating in the google sheet correctly except "body", which is populating " [Ljava.lang.Object;@6f90342a ". Here is my full code and a screenshot of the email body.

 function getGmailEmails(){
      var input = ui.prompt('Label Name', 'Enter the label name that is assigned to your emails:', Browser.Buttons.OK_CANCEL);
      
      if (input.getSelectedButton() == ui.Button.CANCEL){
        return;
      }
      
      var label = GmailApp.getUserLabelByName(input.getResponseText().trim());
      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];
          if (message.isUnread()){
            extractDetails(message);
            GmailApp.markMessageRead(message);
            }
        }
        threads[i].removeLabel(label); //delete the label after getting the message    
      }  
    }
          function extractDetails(message){
          
          var emailData = {
            date: "Null",
            sender: "Null",
            subject: "Null",
            body: "Null",
          }
           regExp = /(\b(\d+)\b)/;
     
          emailData.date = message.getDate();
          emailData.subject = message.getSubject();
          emailData.sender = message.getFrom();
          main = message.getPlainBody();
          emailData.body = main.match(regExp);
  var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  
  var emailDataArr = [];
  
  for(var propName in emailData){
    emailDataArr.push(emailData[propName]);
  }
  activeSheet.appendRow(emailDataArr);

email body example and regexp

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In your situation, I thought that the reason for your issue is due to that the return value from main.match(regExp) is an array. So, how about the following modification?

From:

emailData.body = main.match(regExp);

To:

var temp = main.match(regExp);
emailData.body = temp ? temp[0] : ""; // I thought that temp[1] can be also used.

Reference:

match()

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading