Google sheet 用于搜索和检索 Gmail 数据的脚本

Google sheet script to search and retrieve Gmail data

我已经启动了一个脚本,该脚本将 Col D 中的电子邮件地址与 Gmail 相匹配,并在 Col E 中写入最后一封邮件的日期。我希望该脚本逐行重复 - 现在它只写入第 2 行(新手问题 - 抱歉)。

除了“来自”之外,我还想搜索数据。我还想搜索“to”和其他变量。

最后,我希望它有一个基于时间(每天或每周)的触发器,但我仍然在思考触发器,所以非常感谢您的帮助。

function myFunction() {
// Get the value in the cell D2 (which will have the email to check the latest interaction)
var ss = 
SpreadsheetApp.getActive().getSheetByName('Sheet1').getRange('D2').getValue();

// Search the date of the last message of the search from the email in cell D1 and log it
var latestEmail = GmailApp.search('from:"'+ss+'"')[0].getLastMessageDate();
Logger.log(latestEmail);

// Print the date on the cell to the right
SpreadsheetApp.getActive().getSheetByName('Sheet1').getRange('E2').setValue(latestEmail);
}  

I'd like that script to repeat row by row - now it only writes to Row 2

您需要遍历(“循环”)sheet 的行。

试试这个:

function myFunction() {

  const sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1')
  const addresses = sheet.getRange(2, 4, sheet.getLastRow()-1).getValues().flat().filter(Boolean)

  for (let [index, address] of addresses.entries()) {
    const latestEmail = GmailApp.search(`from: ${address}`)[0].getLastMessageDate()
    sheet.getRange(index+2, 5).setValue(latestEmail);
  }

}

评论:

function myFunction() {

  // Your sheet.
  const sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1')
  // All cells in Column D (Column 4).
  const addresses = sheet.getRange(2, 4, sheet.getLastRow()-1).getValues().flat().filter(Boolean)

  // For each address in Column D... (While keeping track of the index)
  for (let [index, address] of addresses.entries()) {
    // Get the last message date from this address...
    const latestEmail = GmailApp.search(`from: ${address}`)[0].getLastMessageDate()
    // And set the adjacent cell to that date.
    sheet.getRange(index+2, 5).setValue(latestEmail);
    // "Index+2" because index starts at 0 and we started our range at row 2.
  }

}

I'd also like to search for data in addition to "from". I'd like to also search "to" and other variables.

这一切都可以使用 GmailApp.search()

来完成

Lastly, I'd like this to have a trigger that is time-based (daily or weekly) and I'm still getting my head wrapped around triggers, so any help is really appreciated.

再简单不过了,请看这里:

在您的脚本编辑器中,点击触发器图标,然后点击添加触发器按钮。

您将希望从事件源“Time-driven”和 select 中选择此函数,当您希望它时 运行。