简体   繁体   English

Google Apps 脚本:将 Gmail 附件保存到 Google Drive

[英]Google Apps Script: Saving Gmail Attachment to Google Drive

I'm fairly close to completing my Google Apps script project, which is parsing a simple 2 line Email and adding the attachment link (Attachment saved to Google Drive) to Google Sheets.我非常接近完成我的 Google Apps 脚本项目,该项目正在解析一个简单的 2 行 Email 并将附件链接(附件保存到 Google Drive)添加到 Google 表格。

I'm stuck on saving the Email attachment to Google Drive.我坚持将 Email 附件保存到 Google Drive。 The code that is commented out is the block that's not working correctly.被注释掉的代码是不能正常工作的块。 I took an example from: Save Gmail attachments on Google Drive -- But it doesn't seem to work for me.我举了一个例子: 在 Google Drive 上保存 Gmail 附件——但这似乎对我不起作用。

Attached is my full code.附上我的完整代码。

function parseEmailMessages() {

  var threads = GmailApp.search('label:test',0,100);
  var sheet = SpreadsheetApp.openById('<<SHEET ID>>');
  var folder = DriveApp.getFolderById('<<FOLDER ID>>');
  var testLabel = GmailApp.getUserLabelByName('test');
  var doneLabel = GmailApp.getUserLabelByName('finished');

  for (var i = 0; i < threads.length; i++) {
    var message = threads[i].getMessages();
    for (var j in message) {
      var content = message[j].getPlainBody();

      /*
      var attachments = threads[j].getAttachments();
      for (var k in attachments) {
      var attachment = attachments[k];
        var isDefinedType = checkIfDefinedType_(attachment);
        if (!isDefinedType) continue;
        var attachmentBlob = attachment.copyBlob();
        var file = DriveApp.createFile(attachmentBlob);
      }
      */

    }

    var [name, date] = content.split("\n");    

    function latestFile() {
      var gDriveFolder = DriveApp.getFolderById('<<FOLDER ID>>');
      var gDriveFile = gDriveFolder.getFiles();
      var lastFileId = gDriveFile.next().getId(); 
      return lastFileId.toString();
    }

    var urlStart = 'https://drive.google.com/a/<<DOMAIN>>/file/d/';
    var urlString = latestFile();
    var urlEnd = '/view?usp=drivesdk';    
    var pic = urlStart + urlString + urlEnd;

    sheet.appendRow([name,date,pic]);

    threads[i].removeLabel(testLabel);
    threads[i].addLabel(doneLabel);

  }
}

The error I receive is: "TypeError: Cannot find function getAttachments in object GmailThread."我收到的错误是:“TypeError:在 object GmailThread 中找不到 function getAttachments。”

It's true that getAttachments does not seem to be part of GmailThread in the documentation, but I'm not sure how to achieve my goal at the moment.确实 getAttachments 在文档中似乎不是 GmailThread 的一部分,但我目前不确定如何实现我的目标。

getAttachments() is not the method of Class GmailThread. getAttachments()不是 Class GmailThread 的方法。 So how about the following modification?那么下面的修改呢?

From:从:

var attachments = threads[j].getAttachments();

To:至:

var attachments = message[j].getAttachments();

Reference:参考:

If this was not the direct solution, I apologize.如果这不是直接的解决方案,我深表歉意。

var results = Gmail.Users.Messages.list(userId, {q: query});
results.messages.forEach(function (m) {
    var msg = GmailApp.getMessageById(m.id);
    msg.getAttachments().forEach(function (a) {
        var fileName = a.getName();
        fileName = saveAttachmentToFolder(folder, a, fileName, msg.getDate(), input.tz);
        return fileName;
    });
});
function saveAttachmentToFolder(folder, attachment, fileName, date, timezone) {
    if (timezone) {
        fileName = standardizeName(attachment, date, timezone);
    }
    Logger.log(fileName);
    folder.createFile(attachment.copyBlob()).setName(fileName);
    return fileName;
}

The code snippet above is from a Gmail add-on that I created, specifically for saving attachments to Drive: https://github.com/ellaqezi/archiveByLabel/blob/main/Code.gs#L24上面的代码片段来自我创建的 Gmail 插件,专门用于将附件保存到 Drive: https://github.com/ellaqezi/archiveByLabel/blob/main/Code.gs#L24

In the label field, you can define nested directories to create in Drive eg foo/bar.label字段中,您可以定义要在 Drive 中创建的嵌套目录,例如 foo/bar。

In the query field, you can copy the parameters as you would use them in Gmail's search bar.query字段中,您可以复制参数,就像在 Gmail 的搜索栏中使用它们一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM