如何循环遍历电子邮件地址并记录电子邮件回复的时间戳?

huangapple go评论61阅读模式
英文:

How to loop through email adresses and timestamp email replies?

问题

我正在尝试创建一个脚本,用于搜索我的收件箱以查找电子邮件回复,并在找到回复时在我的表格上加上时间戳。

我在A列中有电子邮件地址,我已经发送了电子邮件,当其中任何一个回复/给我发送电子邮件时,我希望记录回复,将时间戳放在已回复的电子邮件所在行的C列中。

到目前为止我已经成功完成的部分:

我已经多次重写了脚本,并且能够在只有一个电子邮件地址时找到回复并加上时间戳,但是当涉及到多个电子邮件时,我遇到了困难。

我正在努力弄清楚如何编写循环和条件语句,以使脚本能够循环遍历每一行电子邮件并在正确的行中加上时间戳。

我不是专家,但我现在对这些事情有一定的理解,因此一直在进行搜索、谷歌搜索和观看YouTube视频来自学。我已经尝试了几个星期,但没有成功,真的很感激在这里得到一些帮助和意见。

以下是我试图实现的步骤:

1. 搜索

  • 检查是否在A列中有电子邮件地址
  • 如果有电子邮件,则检查是否在C列中已经记录了回复(时间戳)

2. 记录回复

  • 如果在C列中有时间戳,则跳过该行,进入下一行并重复步骤1

3. 无回复

  • 如果没有时间戳,则在我的收件箱中使用该行中的电子邮件进行搜索回复。
  • 如果没有找到回复,则跳过该行并重复步骤1

4. 找到回复

  • 如果找到回复,则在相同行的C列上加上时间戳,然后进入下一行

5. 遍历所有包含数据的行

  • 当脚本遍历了所有包含数据的行后,将停止并退出脚本。

以下是表格和下面的脚本的链接:

电子表格链接:https://docs.google.com/spreadsheets/d/1HzLRtSFULijIVbtW8xcIPDOgQKCYdQ4TLN2FgEtSqIg/edit?usp=sharing

脚本:

function myFunction() {
  const sheet = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1HzLRtSFULijIVbtW8xcIPDOgQKCYdQ4TLN2FgEtSqIg/edit#gid=1870420095').getSheetByName('Sheet1');
  const email = sheet.getRange('A:A').getValues().flat();
  const responseColumn = sheet.getRange('C:C').getValues().flat();
  const q = GmailApp.search(email);

  var messages = q[0].threads.getMessages();
  for (var i = 0; i < email.length; i++) {
    if (email[i]) { // Make sure the cell is not empty
      if (responseColumn[i]) {
        continue; // Skip if response already recorded
      }

      if (q.length > 1) {
        for (var i = 0; i < messages.length; i++) {
          sheet.getRange(i + 2, 3).setValue(new Date());
        }
        Logger.log(email);
      }
    }
  }
}

希望这能帮助你进一步解决问题。

英文:

I'm trying to make a script to search my inbox for email replies and timestamp my sheet when I reply is found.

I have email adresses in Col A that I have emailed and when any of them reply/email me back I then want to record the reply by putting a timestamp in Col C on the same row of the email that have replied.

What I have managed to so far:

I have rewritten the script many times and been able to timestamp when a reply is found on only 1 email adress but when there is more emails I get stuck.

I'm trying to figure out how to write the loop and if statements to make the script loop through every row of emails and timestamp the correct rows.

I'm not an expert at this but I do now have a decent understanding around things hence to all the searching, googling and youtubeing to edjucate myself. I have been trying to make this work for weeks and without success, would really appreciate some help and input on what is wrong here.

Here is the sequence that I'm trying to achieve explained:

1. The search

  • Check if there is email adresses in a Col A
  • If there is emails then check if a response is already recorded
    (timestamp) in Col C

2. Recorded reply

  • If there is a timestamp in Col C then skip that row and go to the
    next one and repeat step 1

3. No reply

  • If there is no timestamp then do a search with the email on that row in my inbox for replies.

  • If no reply is found then skip the row and repeat step 1

4. Reply found

  • If a reply is found then timestamp Col C in the same row and go to the next one

5. Searched through all rows with data

  • When the script has gone through every row with data in it will stop
    and exit the script.

Here is a link to the sheet and the script down below.

Spreadsheet: https://docs.google.com/spreadsheets/d/1HzLRtSFULijIVbtW8xcIPDOgQKCYdQ4TLN2FgEtSqIg/edit?usp=sharing

Script:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function myFunction() {

  
  const sheet = SpreadsheetApp.openByUrl(&#39;https://docs.google.com/spreadsheets/d/1HzLRtSFULijIVbtW8xcIPDOgQKCYdQ4TLN2FgEtSqIg/edit#gid=1870420095&#39;).getSheetByName(&#39;Sheet1&#39;);
  const email = sheet.getRange(&#39;A:A&#39;).getValues().flat();
  const responseColumn = sheet.getRange(&#39;C:C&#39;).getValues().flat();
  const q = GmailApp.search(email); 


  
    var messages = q[0].threads.getMessages();
  for (var i = 0; i &lt; email.length; i++) {
    if (email[i]) { // Make sure the cell is not empty
     if (responseColumn[i]) {
        continue; // Skip if response already recorded
      }
   
      
      

      if (q.length &gt; 1){
    
        

        for (var i = 0; i &lt; messages.length; i++)
  {sheet.getRange(i + 2, 3).setValue(new Date());
        } 

Logger.log(email);
            
  
} 
} 
} 
}

<!-- end snippet -->

答案1

得分: 0

你有一列电子邮件地址(在电子表格的A列中),你已经发送了一封电子邮件给这些地址。在电子表格的C列中,有一个日期(与相关的电子邮件地址相邻),显示你是否收到了回复。

涉及的过程如下:

  • 获取地址和日期的值(如果有的话)
  • 循环遍历电子邮件地址
    • 对于每个电子邮件地址,测试列C中的值是否包含日期
    • 如果是,则移动到下一个电子邮件地址
    • 如果不是,则测试是否有回复
      • 搜索电子邮件以查找回复(from:(&lt;&lt;插入电子邮件&gt;&gt;)
      • 如果找到线程,则在列C中插入一个日期

将这些脚本插入包含电子邮件和响应数据的电子表格的项目编辑器中。
假设在第1行有一个单独的标题行。
执行findEmail

function findEmail() {
  const ss = SpreadsheetApp.getActiveSpreadsheet()
  const sheet = ss.getSheetByName('Sheet1')
  const numRows = sheet.getLastRow()-1 //(减去标题行)
  const emailData = sheet.getRange(2,1,numRows,3).getValues()
  Logger.log("DEBUG: email range = "+sheet.getRange(2,1,numRows,3).getA1Notation())
  const emailSent = emailData.map(function(e){return e[0]})
  Logger.log("DEBUG: 电子邮件地址数量 = "+emailSent.length)
 
  for (var i=0;i<emailSent.length;i++)
  {
    var result = isValidDate(emailData[i][2])
    if (result == true){
      // 列C中有日期 - 下一行
      Logger.log("DEBUG: i="+i+", 电子邮件地址 = "+emailData[i][0]+", 列C = "+emailData[i][2]+" , 列C的值是日期")
    }else
    {
      // 列C中没有日期,检查是否有响应
      Logger.log("DEBUG: i="+i+", 电子邮件地址 = "+emailData[i][0]+" , 列C的值不是日期")
      const searchTerm = "from:("+emailData[i][0]+")";
      const threads = GmailApp.search(searchTerm); 
      // 如果有线程,则在列C中插入日期
      if (threads.length >= 1){
        var tz = ss.getSpreadsheetTimeZone()
        const date = new Date();
        sheet.getRange(i+2,3,1,1).setValue(Utilities.formatDate(date, tz, 'd/MM/yyyy'))
      }
    }
  }
}

//so_1353684
// JavaScript中检测"无效日期"的Date实例
function isValidDate(d) {
  return d instanceof Date && !isNaN(d);
}

示例

如何循环遍历电子邮件地址并记录电子邮件回复的时间戳?

英文:

You have a list of email addresses (in Column A of your spreadsheet) to whom you have sent an email. In Column C of the spreadsheet you have a date (adjacent to the relevant email address) which shows whether or not you have received a reply.

The process involved is as follows:

  • get the values for address and date (if any)
  • loop through the email addresses
    • for each email address, test if the value in Column C contains a date
    • if yes, then move to the next email address
    • if no, then test for a reply
      • search emails for a reply (from:(&lt;&lt;insert email&gt;&gt;)
      • if threads are found, then insert a date in Column C

  • Insert these scripts in the Project editor of the spreadsheet containing the Email and Response data.
  • Assume a single header row in row#1
  • Execute findEmail

function findEmail() {
const ss = SpreadsheetApp.getActiveSpreadsheet()
const sheet = ss.getSheetByName(&#39;Sheet1&#39;)
const numRows = sheet.getLastRow()-1 //(subtract header row)
const emailData = sheet.getRange(2,1,numRows,3).getValues()
Logger.log(&quot;DEBUG: email range = &quot;+sheet.getRange(2,1,numRows,3).getA1Notation())
const emailSent = emailData.map(function(e){return e[0]})
Logger.log(&quot;DEBUG: Number of email addresses = &quot;+emailSent.length)
for (var i=0;i&lt;emailSent.length;i++)
{
var result = isValidDate(emailData[i][2])
if (result == true){
// there&#39;s a date in Column C - next row
Logger.log(&quot;DEBUG: i=&quot;+i+&quot;, email address = &quot;+emailData[i][0]+&quot;, ColC = &quot;+emailData[i][2]+&quot; , the ColC value is a date&quot;)
}else
{
// there&#39;s no date in column C, check for response
Logger.log(&quot;DEBUG: i=&quot;+i+&quot;, email address = &quot;+emailData[i][0]+&quot; , the ColC value is NOT a date&quot;)
const searchTerm = &quot;from:(&quot;+emailData[i][0]+&quot;)&quot;
const threads = GmailApp.search(searchTerm); 
// if there are threads, then insert date in Column C
if (threads.length &gt;= 1){
var tz = ss.getSpreadsheetTimeZone()
const date = new Date();
sheet.getRange(i+2,3,1,1).setValue(Utilities.formatDate(date, tz, &#39;d/MM/yyyy&#39;))
}
}
}
}
//so_1353684
// Detecting an &quot;invalid date&quot; Date instance in JavaScript
function isValidDate(d) {
return d instanceof Date &amp;&amp; !isNaN(d);
}

SAMPLE

如何循环遍历电子邮件地址并记录电子邮件回复的时间戳?

huangapple
  • 本文由 发表于 2023年6月1日 17:23:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定