Google Script从电子邮件中提取某些信息并放入工作表

时间:2018-02-20 20:15:32

标签: javascript google-apps-script google-sheets

希望能够自动化我一天中烦人的一部分...... 电子邮件的格式总是相同的...这是我想要从中提取数据的电子邮件的一部分。

项目编号:SS10MM
产品描述:10mm SOCKET
供应商:商店
供应商商品代码:10MSS
投诉:失去了另一个,真的希望你能把GPS芯片放进去!

是否可以让脚本提取该信息并将其中的每一个放入相应的列中?

我尝试使用我在网上搜索“解析电子邮件到表格”时发现的一些脚本,但那些似乎试图将整个电子邮件翻过来,这显然不是我想要的。

帮助??

编辑:我猜当前的代码可能会有所帮助,是吗?

function getGmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var threads = GmailApp.search('in:inbox is:unread from:
(you@aol.com) "TEST EMAIL"');

for (var i = 0; i < threads.length; i++) {
var messages = GmailApp.getMessagesForThread(threads[i]);
for (var j = 0; j < messages.length; j++) {
var msg=messages[j].getPlainBody(); 
var msg=msg.trim() 
}
}
result1(msg)
}
function result1(range) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("Sheet1")
var lr=sheet1.getLastRow()
var output=[]
var line=range.split("\n")
for(j=0;j<line.length;j++){
output.push(line[j].split(" "))
}
var output1=[]
for(k=0;k<output.length;k++){
if(output[k]!=""){
output1.push(output[k])
}}
sheet1.getRange(lr+1, 1, output1.length, 
output1[0].length).setValues(output1)
}

1 个答案:

答案 0 :(得分:0)

我使用this gist并添加了一点来帮助您入门。您必须设置Time-driven Trigger才能运行main_emailDataToSpreadsheet()功能,并将电子邮件标记为“待处理”。

更新:

 //Modified from https://gist.github.com/richard-to/8797504 < http://pipetree.com/qmacro/blog/2011/10/04/automated-email-to-task-mechanism-with-google-apps-script/

var LABEL_PENDING = "pending";
var LABEL_DONE = "done";

// processPending(sheet)
// Process any pending emails and then move them to done
function processPending_(sheet) {

  // Get out labels by name
  var label_pending = GmailApp.getUserLabelByName(LABEL_PENDING);
  var label_done = GmailApp.getUserLabelByName(LABEL_DONE);

  // The threads currently assigned to the 'pending' label
  var threads = label_pending.getThreads();

  // Process each one in turn, assuming there's only a single
  // message in each thread
  for (var t in threads) {
    var thread = threads[t];

    // Gets the message body
    var message = thread.getMessages()[0].getPlainBody();
    Logger.log(message);
    // Process the messages here
    message = message.substr(message.search("Item#:")); //Get the beginning of the important part + cut off the beginning
    Logger.log(message);
    message = message.split("\n");
    Logger.log(message[0].split(": ")[1]);

    var data = [message[0].split(": ")[1], //Item#
                message[1].split(": ")[1], //Prod. Desc.
                message[2].split(": ")[1], //Vendor
                message[3].split(": ")[1], //Ven. Item Code
                message[4].split(": ")[1] //Complaint
               ];
    // Add message to sheet
    sheet.appendRow(data);

    // Set to 'done' by exchanging labels
    thread.removeLabel(label_pending);
    thread.addLabel(label_done);
  }
}