简体   繁体   English

使用 Google Apps 脚本将 Gmail 解析为 Google 表格

[英]Parsing Gmail into Google Sheets using Google Apps Script

I am trying to parse gmail with a specific subject and have the data output to a google sheet in specific columns.我正在尝试使用特定主题解析 gmail 并将数据 output 到特定列中的谷歌表。 I had the script working using just the getPlainBody() option, but then I tried to add getDate() and now I am getting an error.我只使用 getPlainBody() 选项让脚本工作,但后来我尝试添加 getDate() ,现在出现错误。 I am sure there is something wrong in my syntax but have racked my brain for a couple weeks now trying various things in the getGmail and gather functions.我确信我的语法有问题,但我已经绞尽脑汁了几个星期,现在尝试在 getGmail 和收集功能中进行各种操作。 My current code is below and when ran, the date of the email lands in column A, the entire body of the email lands in column B and I get the error "TypeError: message.replace is not a function".我当前的代码如下,运行时,email 的日期位于 A 列,email 的整个主体位于 B 列,我收到错误“TypeError:message.replace 不是函数”。 Can someone please help guide me??有人可以帮助指导我吗?

    function onOpen() {
    const spreadsheet = SpreadsheetApp.getActive();
    let menuItems = [
        {name: 'Gather emails', functionName: 'gather'},
    ];
    spreadsheet.addMenu('New Employees', menuItems);
}
function gather() {
  let messages = getGmail();
  if (messages.length > 0) {
    let curSheet = SpreadsheetApp.getActiveSheet();
    curSheet.getRange(curSheet.getLastRow() + 1, 1, messages.length, messages[0].length).setValues(messages);
     messages.forEach(message => {curSheet.appendRow(parseEmail(message))});
  }
}

function getGmail() {
  const query = "from:(ApplicationServices@help.net) subject:New Account Newer_than:18d NOT label:done";
  let threads = GmailApp.search(query,0,10);

  let label = GmailApp.getUserLabelByName("done");
  if (!label) {label = GmailApp.createLabel("done")}

  let messages = [];
  threads.forEach(thread => {
    const m = thread.getMessages()[0];
    messages.push([m.getDate(), m.getPlainBody()]);
    label.addToThread(thread);
  });
  return messages;
}

function parseEmail(message){
    let parsed = message.replace(/,/g,'')
        .replace(/\n\s*.+: /g,',')
        .replace(/^,/,'')
        .replace(/\n/g,'')
        .replace(/^[\s]+|[\s]+$/g,'')
        .replace(/\r/g,'')
        .split(',');
    let result = [0,1,2,3,4,5,6,7].map(index => parsed[index]);

    return result;
}

Here is what the emails look like:以下是电子邮件的样子:

An account has been created:已创建帐户:

Employee ID: 987333 First: Bill Last: Walsh Username: bwalsh Pwd: 67&&8^.!员工 ID:987333 第一:Bill 最后:Walsh 用户名:bwalsh 密码:67&&8^.!

Employees must change their email password as soon as possible.员工必须尽快更改其 email 密码。 Passwords expire every 90 days and must be 8 characters or more (with at least one uppercase, one lowercase and a number or symbol).密码每 90 天过期一次,并且必须包含 8 个或更多字符(至少一个大写字母、一个小写字母和一个数字或符号)。 Note: This password is also the password to access Google Email.注意:此密码也是访问谷歌Email的密码。

If you require assistance please contact your district technical support team or the HELP IT Help Desk at 998-9999 or via e-mail at helpdesk@help.net.如果您需要帮助,请致电 998-9999 或发送电子邮件至 helpdesk@help.net 联系您的学区技术支持团队或 HELP IT 帮助台。

Any help you can provide would be greatly appreciated.您能提供的任何帮助将不胜感激。

Your getGmail() function returns a 2D array where the first value in each subarray is a Date and the second value is a text string.您的getGmail() function 返回一个二维数组,其中每个子数组中的第一个值是日期,第二个值是文本字符串。 To access the text string, you need to refer to it through index 1 , like this:要访问文本字符串,您需要通过索引1引用它,如下所示:

     messages.forEach(message => { curSheet.appendRow(parseEmail(message[1])) });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM