0

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@g.com

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.

  • Do more and better debugging, using `console` and editing your code to perform one operation per line. – tehhowch May 11 '18 at 07:53
  • 1
    As @tehhowch mentioned, try adding logger to debug the issue. Looks like you are receiving some unexpected input which does not contain 'Name:' part. – Darpan Sanghavi May 11 '18 at 08:46

1 Answers1

1

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.

Suyash Gandhi
  • 926
  • 6
  • 24
  • Error has nothing to do with the end of line characters. Still get the same error when I do `var fullname = messages[j].getPlainBody().split('Name: ')[1]` – user8642569 May 11 '18 at 12:34