user8642569
user8642569

Reputation: 11

apps script getPlainBody gets undefined

I don't know what happened. I thought this script was working at one point. Now I just keep getting "cannot call method 'split' of undefined".

function getVulture(){
  var newSub = SpreadsheetApp.openById('xxx').getSheetByName('newS');
  var label = GmailApp.getUserLabelByName('mail');
  var threads = label.getThreads();  
  for (var i=0; i<threads.length; i++) 
  {
    var messages = threads[i].getMessages();
    for (var j=0; j<messages.length; j++) 
    { 
      var date = messages[j].getDate();
      var fullname = messages[j].getPlainBody().split('Name: ')[1].split('\n')[0];
    }
    threads[i].removeLabel(label).moveToArchive();
  }

The plainbody text is:

Subscription date: May 10, 2018 01:36 pm

Name: Adam West

E-mail Address*: [email protected]

How is this not defined? It's strange because it actually does what I want ... except since there is an error, it never moves on to the threads[i].removeLabel(label).moveToArchive(); line of code.

Upvotes: 0

Views: 415

Answers (1)

Suyash Gandhi
Suyash Gandhi

Reputation: 946

Looks like you are having trouble with the split("\n") method as it is not always necessary that a new line can be inserted by using "\n" only.

Please refer to what the difference is between "\n" and "\r" and how it used on different platforms.

Difference between \n and \r?

\r\n , \r , \n what is the difference between them?

You'll have to use the trail and error method of finding the solution to your problem.

Hope it helps.

Upvotes: 1

Related Questions