在Google云端硬盘中保存Gmail附件

时间:2013-04-21 22:00:30

标签: google-apps-script gmail google-drive-api email-attachments

我有一个小问题。我找到了一个用于在Drive-Cloud中保存附件的脚本。该功能成功但脚本保存图片不正确,如果我想在我的云端硬盘中打开已保存的图片,所以我只能用GoogleDocs打开它,我没有预览。 脚本:

function sendToGDrive() {    

  var sheet   = SpreadsheetApp.getActiveSheet();
  var gLabel  = sheet.getRange("D4:D4").getValues();
  var gFolder = sheet.getRange("D5:D5").getValues();

  var threads = GmailApp.search("label:" + gLabel, 0, 5);  
  var folder  = DocsList.getFolder(gFolder);

  for (var x=0; x<threads.length; x++) {

    var messages = threads[x].getMessages();

    for (var y=0; y<messages.length; y++) {

      var att = messages[y].getAttachments();

      for (var z=0; z<att.length; z++) {
        try {
          var file = folder.createFile(att[z]);
          Utilities.sleep(1000);
        }
        catch (e) {
          GmailApp.sendEmail(
            Session.getActiveUser().getUserLoginId(), 
            "Error: " + e.message
          );
        }
      }       
    }    
    GmailApp.getUserLabelByName(gLabel)
      .removeFromThread(threads[x]);
  }
}

function configure() {
  var sheet   = SpreadsheetApp.getActiveSheet();
  var gLabel  = sheet.getRange("D4:D4").getValues();

  if (!GmailApp.getUserLabelByName(gLabel)) {
    GmailApp.createLabel(gLabel);
  }

  createFolder();

  if (ScriptApp.getScriptTriggers().length == 0) {
    ScriptApp.newTrigger("sendToGDrive").timeBased().everyMinutes(5).create();
  }

  SpreadsheetApp.getActiveSpreadsheet()
    .toast("You can now close this Google Docs sheet and it will run in the background.",
           "Success", -1);
}

function onOpen() {  
  var menu = [    
    {name: "Initialize", functionName: "configure"},
    {name: "Run", functionName: "configure"}
  ];  
  SpreadsheetApp.getActiveSpreadsheet()
    .addMenu("Gmail (Click here)", menu);
}

function createFolder() {
  var sheet   = SpreadsheetApp.getActiveSheet();
  var gFolder = sheet.getRange("D5:D5").getValues();

  var folders = DocsList.getAllFolders();
  var found = false;

  for (var i=0; i<folders.length; i++) {
    if (folders[i].getName() == gFolder) {
      found = true;
      break; 
    }
  }

  if (!found) {
    DocsList.createFolder(gFolder);
  }
}

1 个答案:

答案 0 :(得分:1)

不幸的是,这是known issue (1239)你可以通过明星告知它何时/如何修复以及对它进行投票。