Google 已经获取电子邮件信息但需要附件名称的脚本

Google script that already fetches email infos but needs attachment name

上下文

我测试了什么

我想要什么

修改:

对您在问题中提到的 的答案进行以下修改:

  • 添加这些行:

    var attachments = messages[maxIndex].getAttachments();
    var attNames = attachments.map(att=>att.getName());
    
  • 并修改这个:

    ss.appendRow([from, cc, time, sub,...attNames ,'https://mail.google.com/mail/u/0/#inbox/'+mId])
    

解决方案:

function myFunction() {
  // Use sheet
  var ss = SpreadsheetApp.getActiveSheet();
  // Gmail query
  var query = "label:support -label:trash -label:support-done -from:me";
  // Search in Gmail, bind to array
  var threads = GmailApp.search(query);
  // Loop through query results
  for (var i = 0; i < threads.length; i++)
  {
    // Get messages in thread, add to array
    var messages = threads[i].getMessages();

    // Used to find max index in array
    var max = messages[0];
    var maxIndex = 0;

    // Loop through array to find maxIndexD = most recent mail
    for (var j = 0; j < messages.length; j++) {
      if (messages[j] > max) {
        maxIndex = j;
        max = messages[j];
      }
    } 
    // Find data
    var mId = messages[maxIndex].getId() // ID used to create mail link
    var from = messages[maxIndex].getFrom();
    var cc = messages[maxIndex].getCc();
    var time = threads[i].getLastMessageDate()
    var sub = messages[maxIndex].getSubject();
    var attachments = messages[maxIndex].getAttachments();
    var attNames = attachments.map(att=>att.getName());

    // Write data to sheet
ss.appendRow([from, cc, time, sub,...attNames ,'https://mail.google.com/mail/u/0/#inbox/'+mId])
  }
}

不要忘记根据您的需要更改 query 的值。

参考文献:

您必须 enable V8 运行时才能使用代码段。