我在Google Apps脚本中使用以下代码,我想在收件箱中获取所有消息。此代码给出“无法将数组转换为数字[] []。(第19行,文件“代码”)”错误。我该如何解决此代码?

    var myspreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    var mysheet = myspreadsheet.getSheets()[0];
    var start = 0;
    var max = 19;
    var count = 0;
    while (count < 7) {
        var threads = GmailApp.getInboxThreads(start, max);
        var messages = GmailApp.getMessagesForThreads(threads);
        //var froms = [];
        messages.get
        for (var i = 0; i < threads.length; i++) {
            var thisThread = threads[i];
            var messages = thisThread.getMessages();
            var messageCount = thisThread.getMessageCount();
            for ( var m = 0; m<=messageCount; m++) {
              var lastMessage = messages[m];
              froms = ([lastMessage.getId(), lastMessage.getSubject(), lastMessage.getTo(), lastMessage.getFrom(), lastMessage.getCc(), JSON.stringify(lastMessage.getDate()), lastMessage.getReplyTo()]);
              mysheet.getRange(1, 1, froms.length, 7).setValues(froms);
              froms = [];
            }
        }
        start = start + 100;
        count++;
        }
}

最佳答案

尝试更改此内容:froms = ([lastMessage.getId(), lastMessage.getSubject(), lastMessage.getTo(), lastMessage.getFrom(), lastMessage.getCc(), JSON.stringify(lastMessage.getDate()), lastMessage.getReplyTo()]);

对此:froms = ([[lastMessage.getId(), lastMessage.getSubject(), lastMessage.getTo(), lastMessage.getFrom(), lastMessage.getCc(), JSON.stringify(lastMessage.getDate()), lastMessage.getReplyTo()]]);

但实际上我认为您的脚本有更大的问题。

我会用这样的东西。但是我无法很好地对其进行测试,因为我的收件箱中没有太多垃圾。

function emailsStuff() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheets()[0];
  sh.clearContents();
  var threads=GmailApp.getInboxThreads()
  for(var i=0;i<threads.length;i++) {
    var messages=GmailApp.getMessagesForThread(threads[i]);
    for(var j=0;j<messages.length;j++) {
      var msg=messages[j];
      sh.appendRow([msg.getId(), msg.getSubject(), msg.getTo(), msg.getFrom(), msg.getCc(), JSON.stringify(msg.getDate()), msg.getReplyTo()]);
    }
  }
}

09-16 23:45