将Google Doc脚本更新为Google Drive(将带有标签的电子邮件归档) [英] Updating Google Doc script to Google Drive (Archive emails with tags to drive)

查看:623
本文介绍了将Google Doc脚本更新为Google Drive(将带有标签的电子邮件归档)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本将具有特定标签的电子邮件的pdf版本保存到Google驱动器。我有三个几乎相同的三个不同标签的脚本版本,我定期运行它们。



最近他们因DocsList贬值而停止工作。我将DocsList的所有实例都更改为DriveApp,但现在出现错误TypeError:无法在对象FolderIterator中查找函数createFolder。



问题出现在最后几个行,脚本应该创建一个文件夹来保存电子邮件的pdf。

有人可以帮我修复createFolder函数并获取脚本,运行?

< pre-class =snippet-code-js lang-js prettyprint-override> / ** *主函数运行在电子表格打开* / function onOpen(){var ss = SpreadsheetApp.getActiveSpreadsheet(); var menuEntries = [{name:Initialize,functionName:init},{name:存档Gmail邮件,函数名称:ScanGmail}]; ss.addMenu(Gmail Archiver,menuEntries);} / ** *初始化系统* / function init(){//创建所需的Gmail标签GmailApp.createLabel(Archive to Drive); //创建Google Drive文件夹如果不存在try {var folder = DriveApp.getFolder(Email Archive); } catch(e){//文件夹不存在DriveApp.createFolder(Email Archive); } Browser.msgBox(创建Gmail标签:存档到云端硬盘和Google云端硬盘文件夹:电子邮件存档);} / ** *扫描邮件存档请求的Gmail帐户* / function ScanGmail(){//默认驱动器文件夹存档位置消息var baseFolder =Sparkfly Receipts 2015; //获取标签var label = GmailApp.getUserLabelByName(Sparkfly receipt); var threadsArr = getThreadsForLabel(label); for(var j = 0; j

解决方案

getFoldersByName()方法返回一个 FolderIterator (文件夹对象的集合),并且该集合不包含createFolder方法。因此,如果集合至少有一个Folder对象,那么应该将该对象从集合中取出,然后调用createFolder。



您可以在这里找到相关文档: https ://developers.google.com/apps-script/reference/drive/folder#getFoldersByName(String)


My script saves pdf versions of emails with a specific label to Google drive. I have three nearly identical versions of the script for three different labels and I run them periodically.

Recently they stopped working because of depreciation of DocsList. I changed all the instances of DocsList to DriveApp, but now am getting the error "TypeError: Cannot find function createFolder in object FolderIterator."

The issue is in the last few lines, where the script should be creating a folder in which to save the pdf of the emails.

Could someone help me fix the createFolder function and get the script back up and running?

/**
 * Main function run at spreadsheet opening
 */
function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [ 
    {name: "Initialize", functionName: "init"},
    {name: "Archive Gmail Messages", functionName: "ScanGmail"}
  ];
  ss.addMenu("Gmail Archiver", menuEntries);
}
    
/**
 * Initialize the system
 */
function init() {
    // Create the needed Gmail label
    GmailApp.createLabel("Archive to Drive");
    
    // Create Google Drive folder if doesn't exists
    try {
       var folder = DriveApp.getFolder("Email Archive");
    } catch(e) {
       // Folder doesn't exists
       DriveApp.createFolder("Email Archive");
    }
    
    Browser.msgBox("Created Gmail label: Archive to Drive and Google Drive folder: Email Archive");
}

/**
 * Scan Gmail account for message archive requests
 */
function ScanGmail() {
  // Default Drive folder where to archive messages
  var baseFolder = "Sparkfly Receipts 2015";
  
  // Get the label
  var label = GmailApp.getUserLabelByName("Sparkfly receipt");
    var threadsArr = getThreadsForLabel(label);
    for(var j=0; j<threadsArr.length; j++) {
      var messagesArr = getMessagesforThread(threadsArr[j]);
      for(var k=0; k<messagesArr.length; k++) {
        var messageId = messagesArr[k].getId();
        var messageDate = Utilities.formatDate(messagesArr[k].getDate(), Session.getTimeZone(), "MM.dd.yyyy");
        var messageFrom = messagesArr[k].getFrom();
        var messageSubject = messagesArr[k].getSubject();
        var messageBody = messagesArr[k].getBody();
        var messageAttachments = messagesArr[k].getAttachments();
        
        // Create the new folder to contain the message
        var newFolderName = messageDate + " - " + messageSubject;
        var newFolder = createDriveFolder(baseFolder, newFolderName);
        
        // Create the message PDF inside the new folder
        var htmlBodyFile = newFolder.createFile('body.html', messageBody, "text/html");
        var pdfBlob = htmlBodyFile.getAs('application/pdf');
        pdfBlob.setName(newFolderName + ".pdf");
        newFolder.createFile(pdfBlob);
        htmlBodyFile.setTrashed(true);
        

        // Save attachments
        for(var i = 0; i < messageAttachments.length; i++) {
            var attachmentName = messageAttachments[i].getName();
            var attachmentContentType = messageAttachments[i].getContentType();
            var attachmentBlob = messageAttachments[i].copyBlob();
            newFolder.createFile(attachmentBlob);
        }

      }
      // Remove Gmail label from archived thread
      label.removeFromThread(threadsArr[j]);
    }
    Browser.msgBox("Gmail messages successfully archived to Google Drive");
}


/**
 * Find all user's Gmail labels that represent mail message
 * movement requests es: moveto->xx@yyyy.com
 *
 * @return {GmailLabel[]} Array of GmailLabel objects
 */
function scanLabels() {
  // logs all of the names of your labels
  var labels = GmailApp.getUserLabels();
  var results = new Array();
  for (var i = 0; i < labels.length; i++) {
    if(labels[i].getName() == "Sparkfly receipt") {
      results.push(labels[i]);
    }
  }
  return results;
}

/**
 * Get all Gmail threads for the specified label
 *
 * @param {GmailLabel} label GmailLabel object to get threads for
 * @return {GmailThread[]} an array of threads marked with this label
 */
function getThreadsForLabel(label) {
  var threads = label.getThreads();
  return threads;
}

/**
 * Get all Gmail messages for the specified Gmail thread
 *
 * @param {GmailThread} thread object to get messages for
 * @return {GmailMessage[]} an array of messages contained in the specified thread
 */
function getMessagesforThread(thread) {
  var messages = thread.getMessages();
  return messages;
}


/**
 * Get methods of an object
 * @param {Object} object to scan
 * @return {Array} object's methods
 */
function getMethods(obj) {
  var result = [];
  for (var id in obj) {
    try {
      if (typeof(obj[id]) == "function") {
        result.push(id + ": " + obj[id].toString());
      }
    } catch (err) {
      result.push(id + ": inaccessible");
    }
  }
  return result;
}

/**
 * Create a Google Drive Folder
 *
 * @param {String} baseFolder name of the base folder
 * @param {String} folderName name of the folder
 * @return {Folder} the folder object created representing the new folder 
 */
function createDriveFolder(baseFolder, folderName) {
  var baseFolderObject = DriveApp.getFoldersByName(baseFolder);
  return baseFolderObject.createFolder(folderName);
}

解决方案

The method getFoldersByName() returns a FolderIterator (collection of folder objects), and that collections does not contain a "createFolder" method.

So if the collection has at least one Folder object you should take that object out of the collection and then call "createFolder".

Here you can find the documentation: https://developers.google.com/apps-script/reference/drive/folder#getFoldersByName(String)

这篇关于将Google Doc脚本更新为Google Drive(将带有标签的电子邮件归档)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆