本文介绍了使用Google App Scrip过滤邮件正文放入电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
根据电子邮件正文中的交易数据,我在每笔交易中都会收到来自银行的电子邮件,请参阅下面的示例。
现在,我想要分隔要在电子表格中传输的数据,比如分支机构代码旁边的值应该在A列的最后一行,分支机构代码在B列,编号在C列,依此类推。
下面是我尝试的代码:
数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">//function to seprate and filter data
function parseEmail(message){
let parsed = message.replace(/Branch Code /g,'')
.replace(/Branch Name /g,'')
.replace(/Slip No /g,'')
.replace(/
*.+:/g,',')
.replace(/^,/,'')
.replace(/
/g,'')
.split(',');
let result = [0,1,2,3,4,6].map(index => parsed[index]);
Logger.log(result[0]);
return result;
}
// main function to search email
function testforemails0003(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Emails");
var Gmail = GmailApp;
var lasttime = sheet.getRange("Z1").getValue();
Logger.log(lasttime);
var cdate = new Date();
var ctime = cdate.getTime();
var qDate = sheet.getRange("Z3").getValue();
Logger.log("QDATE IS " + qDate);
// SEARCH EMAIL
var query = 'subject: subject, after:' + Math.floor((qDate.getTime()) /1000);
var threadsNew = Gmail.search(query);
Logger.log(threadsNew.length);
//loop all emails
for(var n in threadsNew){
var thdNew = threadsNew[n];
var msgsNew = thdNew.getMessages();
var msgNew = msgsNew[msgsNew.length-1];
// GET ATTACHMENT
var bodyNew = msgNew.getBody();
var plainbody = msgNew.getPlainBody();
var subject = msgNew.getSubject();
var Etime = msgNew.getDate();
var attachments = msgNew.getAttachments();
var attachment = attachments[0];
Logger.log(Etime);
Logger.log(subject);
parseEmail(plainbody);
}
Logger.log(threadsNew.length);
var lastscantime = threadsNew[0].getLastMessageDate();
var lastsubject = threadsNew[0].getFirstMessageSubject();
Logger.log(lastscantime);
Logger.log(lastsubject);
sheet.getRange("z3").setValue(lastscantime);
}