简体   繁体   English

Google Apps 脚本 - 将 gmail 中的数据提取到电子表格中

[英]Google Apps Scripts - Extract data from gmail into a spreadsheet

this is the first script i try to write from scratch.这是我尝试从头开始编写的第一个脚本。 It's been no good up to now so i'm going to ask for some help.到现在为止都不好,所以我要寻求一些帮助。

Case: I recieve e-commerce confirmation emails from e-commerce sites no reply email address.案例:我收到来自电子商务网站的电子商务确认电子邮件,没有回复电子邮件地址。 In the email's body they send email address from buyers.在电子邮件的正文中,他们发送买家的电子邮件地址。 I want to send an automated mail to the body's email address.我想向正文的电子邮件地址发送一封自动邮件。

How i plan to do this (any suggetions to eliminate steps will be thanked).我打算如何做到这一点(将感谢任何消除步骤的建议)。

  1. Use a rule to tag incoming emails with a unique tag.使用规则为传入的电子邮件添加唯一标签。

  2. Use that tag to identify emails in gmail with a script, go one by one and extract the info i need.使用该标签通过脚本识别 gmail 中的电子邮件,逐一提取我需要的信息。 Use regex with the emails body content to extract the email address i need to send the automated emails.使用带有电子邮件正文内容的正则表达式来提取我需要发送自动电子邮件的电子邮件地址。 Plan is to get: subject, date, email from body.计划是从正文获取:主题、日期、电子邮件。

  3. Write all that info to a spreadsheet.将所有这些信息写入电子表格。

  4. Get rid of unique tag info to prevent duplicate runs.摆脱独特的标签信息以防止重复运行。

  5. Then use form mule addon to send emails from the spreadsheet.然后使用表单 mule 插件从电子表格发送电子邮件。

So far, i've dealt with steps 1 (easy), and been stuggling with steps 2 and 3 (im not a coder, i can read, undestrand and hack. writing from scratch is a completely different thing).到目前为止,我已经处理了第 1 步(简单),并且一直在第 2 步和第 3 步中挣扎(我不是编码员,我可以阅读、理解和破解。从头开始写作是完全不同的事情)。 Ive dealt with 4 before i think this is the best way to deal with it.在我认为这是处理它的最佳方法之前,我已经处理了 4 个。

With the script i extract info to the spreadsheet, with the addon i use the info from the spreadsheet to send emails.使用脚本我将信息提取到电子表格中,使用插件我使用电子表格中的信息发送电子邮件。

This is the code ive written so far.这是我到目前为止编写的代码。 I've left the regex part for later cause i cant even write anything into the spreadsheet yet.我把正则表达式部分留到以后,因为我什至还不能在电子表格中写任何东西。 once i get that working, ill start working in the regex and "remove the label" aspects of the script.一旦我开始工作,我就开始使用正则表达式和脚本的“删除标签”方面。

function myFunction() {
  function getemails() {
    var label = GmailApp.getUserLabelByName("Main tag/subtag");
    var threads = label.getThreads();
    for (var i = 0; i < threads.length; i++) { 
    var messages=threads[i].getMessages();  
      for (var j = 0; j < messages.length; j++) {
    var message=messages[j];
    var subject=message.getSubject();
    tosp(message);
      }
     }
  }

  function tosp(message){
    var body=message.getBody()
    var date=message.getDate();
    var subject=message.getSubject(); 
    var id= "my spreasheet id";
    var ss = SpreadsheetApp.openById(id);
    var sheet = ss.getActiveSheet();
    sheet.appendRow(subject,date,body);    

}
} 

Any help would be appreciated.任何帮助,将不胜感激。

Thanks Sebastian谢谢塞巴斯蒂安

Following is the code I wrote and tested that performs the steps 2, 3 and 4 mentioned by you perfectly well.以下是我编写和测试的代码,可以很好地执行您提到的步骤 2、3 和 4。

function myFunction() {

  var ss = SpreadsheetApp.getActiveSheet();

  var label = GmailApp.getUserLabelByName("MyLabel");
  var threads = label.getThreads();

  for (var i=0; i<threads.length; i++)
  {
    var messages = threads[i].getMessages();

    for (var j=0; j<messages.length; j++)
    {
      var msg = messages[j].getBody();
      var sub = messages[j].getSubject();
      var dat = messages[j].getDate();

      ss.appendRow([msg, sub, dat])
    }
      threads[i].removeLabel(label);
  }
}

One of the faults in your code was that the appendRow function accepts an array of elements specified within [ ] brackets.您的代码中的一个错误是appendRow函数接受在[ ]括号内指定的元素数组。

Depending on where you're attaching this script, your line of code:根据您附加此脚本的位置,您的代码行:

var ss = SpreadsheetApp.openById(id);

is not necessary if the script is being written in the script editor of the Spreadsheet where you want these emails to be logged.如果脚本是在您希望记录这些电子邮件的电子表格的脚本编辑器中编写的,则不需要。 However, if there are multiple sheets in that spreadsheet, you can replace my line但是,如果该电子表格中有多个工作表,您可以替换我的行

var ss = SpreadsheetApp.getActiveSheet();

by经过

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");

Another suggestion is that the current code will give you messages in HTML format.另一个建议是,当前代码将为您提供 HTML 格式的消息。 Hence, if you want to get the message in plain text as you see it, use:因此,如果您想以纯文本形式获取消息,请使用:

var msg = messages[i].getPlainBody();

Now you can write another function for regex and pass the message msg to that.现在您可以为正则表达式编写另一个函数并将消息msg传递给它。 Hope this helps!希望这可以帮助!

I made a ready-to-use script, explaining how to use it (from the start) as well, for those who need more assistance.我为需要更多帮助的人制作了一个即用型脚本,并解释了如何使用它(从一开始)。

It's on gmail-to-google-sheets-script repository.它位于gmail-to-google-sheets-script存储库中。 Just read the content and follow the instructions.只需阅读内容并按照说明进行操作。

How to use如何使用

  • Create a new Google Sheet创建新的 Google 表格
  • Access menu Tools > Script Editor访问菜单工具 > 脚本编辑器
  • Copy the content from gmailt-to-sheets.gs to editor, replacing the sample code there将内容从gmailt-to-sheets.gs复制到编辑器,替换那里的示例代码
  • Replace the value on SEARCH_QUERY to your real query (Do your search on gmail first, copy and paste the search terms there)SEARCH_QUERY上的值替换为您的实际查询(首先在 gmail 上进行搜索,然后将搜索词复制并粘贴到那里)
  • Select saveEmails on menu (near "run" and "debug" buttons)在菜单上选择saveEmails (靠近“运行”和“调试”按钮)
  • Click on "Run" button点击“运行”按钮
  • It will ask for authorization at first run, proceed accepting it (it's your Gmail account authorizing your Google Script account)它会在第一次运行时请求授权,继续接受它(这是您的 Gmail 帐户授权您的 Google Script 帐户)
  • After run, the results will be applied to you sheet运行后,结果将应用于您的工作表

Changing fields更改字段

If you want to save different message attributes, take a look at gmail-message class and change your script file the code below comments with a ✏️ (pencil).如果您想保存不同的消息属性,请查看gmail-message 类并使用✏️(铅笔)将下面的代码更改为您的脚本文件。

The solution by @pointNclick works on one-time execution. @pointNclick 的解决方案适用于一次性执行。 What if you received a new email on a Gmail thread that has already been 'processed' by the script?如果您在 Gmail 线程中收到一封已被脚本“处理”的新电子邮件怎么办? The script ends up reprocessing old email(s) in the thread.该脚本最终重新处理线程中的旧电子邮件。 How would you prevent duplicate reads from happening?您将如何防止发生重复读取?

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

相关问题 使用 Google Apps 脚本将 gmail 中的数据提取到电子表格中 - Extracting data from gmail into a spreadsheet using Google Apps scripts Google Apps脚本-上传文件并将数据发布到电子表格 - Google Apps Scripts- Upload File And Post Data To Spreadsheet 通过电子表格中的数据创建帐户-Google Apps - creating account by data from spreadsheet - google apps Google Apps电子表格脚本会随机停止? - Google Apps Spreadsheet Scripts Randomly Stopping? 从gmail检索csv附件文件并将数据放在谷歌电子表格中 - Retrieve csv attachment file from gmail and place the data in a google spreadsheet 使用 googe 应用程序脚本从电子表格中的所有工作表中提取数据 - extract data from all sheets in a spreadsheet with googe apps script 将线程中的所有消息从 gmail 提取到电子表格中 - Extract all messages in thread from gmail into a spreadsheet 谷歌应用程序脚本html选择从电子表格数据加载的选项 - google apps script html select options loaded from spreadsheet data 使用Google Apps脚本作为服务从电子表格中检索数据 - Using Google Apps Script as service to retrieve data from spreadsheet 如何使用 Google Apps 脚本访问外部电子表格中的数据 - How to access data from an external spreadsheet with Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM