Google Apps脚本 - 重新发送电子邮件

时间:2016-09-26 20:33:09

标签: email google-apps-script

过去几周我一直在玩这个剧本。该脚本的目标是通过报告收件箱,从电子邮件附件中提取报告数据,复制到谷歌电子表格,然后重新标记电子邮件以将其从收件箱中删除,以防止意外双重复制报告。

脚本按此顺序运行:

  1. 在包含附件的收件箱中查找新电子邮件
  2. 复制附件数据
  3. 在下一个打开的行中粘贴到电子表格中
  4. 使用“报告”而非“收件箱”重新标记电子邮件,以将所有报告移至报告文件夹
  5. 我已经成功地完成了步骤1 - 3,但对于我的生活,我无法重新开始工作。当我在Google Apps控制台中运行调试时,它不会返回任何错误。下面粘贴的是脚本执行重新贴标签的摘录:

      for (var i = 0; i < myLabel.length; i++) {
        labels = myLabel[i].getLabels();
        for (var j = 0; j < labels.length; j++) {
          labels[j].addLabel("test_2");
          labels[j].removeLabel("Test");
        }
      }
    

    以下是我正在运行的完整脚本。

    function getCSV() {
    
    
      // Create variable that looks for Gmails in the main inbox
      var myLabel = GmailApp.getUserLabelByName("test");
      Logger.log("myLabel:",myLabel);
    
      // Create variable that is filled with all threads within Inbox    label
      var threads = myLabel.getThreads();
      Logger.log("threads:",threads);
    
      // Retrieves all messages in the specified thread
      var msgs = GmailApp.getMessagesForThreads(threads);
      Logger.log("msgs:",msgs);
    
      // Uses active sheet the script is implemented on
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheetByName("test");
    
      // Grabs CSV data from attachments and pastes into next available row in Spreadsheet
      for (var i = 0; i < threads.length; i++) {
        var messages = threads[i].getMessages();
        for (var j = 0; j < messages.length; j++) {
          var attachments = messages[j].getAttachments();
          Logger.log("attachments:",attachments);
          var csvData = Utilities.parseCsv(attachments[j].getDataAsString(), ",");
          Logger.log(csvData);
          for (var k = 1; k < csvData.length; k++) {
            var dataPaste = sheet.appendRow(csvData[k]);
            Logger.dataPaste;
          }
        }
      }
      // Removes Inbox Label and Adds Report Label
      for (var i = 0; i < myLabel.length; i++) {
        labels = myLabel[i].getLabels();
        for (var j = 0; j < labels.length; j++) {
          labels[j].addLabel("test_2");
          labels[j].removeLabel("Test");
        }
      }
    }
    

1 个答案:

答案 0 :(得分:0)

我最终搞清楚了。另外,我添加了一个部分,如果压缩了CSV,它可以提取数据。

function getCSV() {


  // Associated Inbox label and Report Label with variables
  var myInboxLabel = GmailApp.getUserLabelByName("Test");
  var myReportLabel = GmailApp.getUserLabelByName("test_2");

  // Create variable that is filled with all threads within Inbox label
  var threads = myInboxLabel.getThreads();
  Logger.log("threads:" + threads);

  // Retrieves all messages in the specified thread
  var msgs = GmailApp.getMessagesForThreads(threads);
  Logger.log("msgs:" + msgs);

  // Uses active sheet the script is implemented on
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("test");
/*   Script to pull data from CSV that is NOT zipped
  // Grabs CSV data from attachments and pastes into next available row in Spreadsheet
  for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    for (var j = 0; j < messages.length; j++) {
      var attachments = messages[j].getAttachments();
      Logger.log("attachments:" + attachments);
      var csvData = Utilities.parseCsv(attachments[j].getDataAsString(), ",");
      Logger.log("csvData:" + csvData);
      for (var k = 1; k < csvData.length; k++) {
        var dataPaste = sheet.appendRow(csvData[k]);
        Logger.dataPaste;
      }
    }
  }
*/

// Grabs CSV within a zip folder and pastes into next available row in Spreadsheet
 for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    for (var j = 0; j < messages.length; j++) {
      var attachments = messages[j].getAttachments();
      var extracted = Utilities.unzip(attachments[j]);
      var csvData = Utilities.parseCsv(extracted[j].getDataAsString(), ",");
      Logger.log(csvData);
      for (var k = 1; k < csvData.length; k++) {
        var dataPaste = sheet.appendRow(csvData[k]);
        Logger.dataPaste;
      }
    }
  }
  // Removes Inbox Label and Adds Report Label
  for (var x in threads) {
    var thread = threads[x];
    thread.removeLabel(myInboxLabel);
    thread.addLabel(myReportLabel);
  }
}