需要根据错误和警告拆分行并删除特定内容

Need to split the row based on Error and Warning and remove particular content

虽然运行提供的脚本运行完美,但需要修改。这是截图告诉我需要什么,我正在尝试更改脚本,如果可以的话请帮助我

function autocopy() {
  var label = GmailApp.getUserLabelByName("Sanmina EDI Failed Concurrent Jobs Alert");
  var threads = label.getThreads();
  var read = threads.getMessages();
  var uread = threads.isUnread();

  for(var i = 0; i <= uread.length; i++) { 
    var message=uread(i);
  }

  var message1 = new message.Date();
  var day = message1.getUTCDate();
  var bodycontent = message.getbody();
  var action = bodyContents.search("Invoice")
  var action1 = bodyContents.search("Error")
  var action2 = bodyContents.search("Terminated")

  if (action > 0) {
    var out ="Need to create SR"
  } else if (action1 > 0 || action2 > 2) {
    var out ="Need to create SR"
  } else {
    var out ="Printing output file"
  }

  var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  activeSheet.appendRow([day, bodycontent, out]);
}

我想从电子邮件中提取数据到电子表格,方法是读取未读线程 ID 并使用 for 循环从已读线程 ID 到未读线程 ID 的范围,并打印未读电子邮件中的邮件正文内容和日期。

我稍微修改了你的代码。在其中添加一些注释,以便您可以清楚地看到每一行中发生了什么。您的问题是您试图在错误的对象上使用方法(例如,线程数组没有 getMessages 方法,因此您必须遍历每个线程并获取每个特定线程的消息) .

function autocopy() {
  var label = GmailApp.getUserLabelByName("Sanmina EDI Failed Concurrent Jobs Alert");
  // Get all threads belonging to this label
  var threads = label.getThreads();
  // Loop through each thread
  for (var i = 0; i < threads.length; i++) {
    // Check whether thread is unread
    if (threads[i].isUnread()) {
      // Get all messages for each unread thread
      var messages = threads[i].getMessages();
      // Loop through all messages for each unread thread
      for (var j = 0; j < messages.length; j++) {
        // Check whether message is unread 
        // (delete this condition if you want all messages in an unread 
        // thread to be printed in your spreadsheet, read or unread)
        if (messages[j].isUnread()) {
          var day = messages[j].getDate();
          var bodyContent = messages[j].getBody(); // Use getPlainBody() instead if you don't want html tags;
          var action = bodyContent.search("Invoice");
          var action1 = bodyContent.search("Error");
          var action2 = bodyContent.search("Terminated");

          if (action > 0) {
            var out = "Need to create SR"
          } else if (action1 > 0 || action2 > 2) {
            var out = "Need to create SR"
          } else {
            var out = "Printing output file"
          }
          var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
          activeSheet.appendRow([day, bodyContent, out])
        }
      }
    }
  }
}

希望这对你有用!