TypeError:无法读取属性" length"来自undefined

时间:2015-06-09 03:23:54

标签: google-apps-script

我正在尝试使用下面的代码来自此链接:

Import emails that fit criteria to Google Spreadsheet using apps script

它给了我错误

" TypeError:无法读取属性"长度"来自undefined。"

有人可以帮忙吗?

   function getMessagesWithLabel() {
     var destArray = new Array();
      var threads = GmailApp.getUserLabelByName('Facebook').getThreads(1,10);

      for(var n in threads){
            var msg = threads[n].getMessages();
            var destArrayRow = new Array();
            destArrayRow.push('thread has '+threads[n].getMessageCount()+' messages');
              for(var m in msg){
                         destArrayRow.push(msg[m].getSubject());
               }
      destArray.push(destArrayRow);           
            }
    Logger.log(destArray);
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sh = ss.getActiveSheet();
    if(ss.getLastRow()==0){sh.getRange(1,1).setValue('getMessagesWithLabel() RESULTS')};
    sh.getRange(ss.getLastRow()+1,1,destArray.length,destArray[0].length).setValues(destArray)
    }

1 个答案:

答案 0 :(得分:0)

问题是,如果GmailApp.getUserLabelByName('Facebook').getThreads(1,10);中没有元素,则threads将为空,并且循环for(var n in threads)将不起作用

这意味着您需要输入

var threads = GmailApp.getUserLabelByName('Facebook').getThreads(1,10);

if (threads != null) {
  for(var n in threads) {