【问题标题】:Google Sheets Script for Notification用于通知的 Google 表格脚本
【发布时间】:2022-12-20 09:48:33
【问题描述】:

我有一张铅纸。此铅表中有多个表。这些工作表会定期自动输入数据。我想要的是,如果在某个时间段内没有数据输入,我希望将通知发送到指定的电子邮件地址。我可以使用特定的脚本或不同的设置来实现吗?

如果在脚本指定的超时后没有数据输入,则自动发送电子邮件

【问题讨论】:

  • 你忘了包括你解决这个问题的尝试。

标签: javascript


【解决方案1】:

是的,如果在特定时间段后 Google 表格文档中没有数据条目,您可以使用脚本自动发送电子邮件。这对于跟踪何时将数据输入工作表以及在数据输入过程出现任何延迟或问题时提醒您很有用。

要创建一个在特定时间段后没有数据输入时发送电子邮件的脚本,您可以使用 JavaScript 中的 setInterval 方法。此方法允许您定期运行一个函数,因此您可以使用它来检查工作表中的新数据,如果在特定时间段后没有新数据,则发送电子邮件。

下面是一个示例,说明如果一小时后您的 Google 表格文档中没有新数据,您可以如何使用 setInterval 发送电子邮件:

function checkForData() {
  // Get the active sheet in the document
  var sheet = SpreadsheetApp.getActiveSheet();

  // Check the last row and column in the sheet
  var lastRow = sheet.getLastRow();
  var lastColumn = sheet.getLastColumn();

  // Get the current time
  var currentTime = new Date();

  // If there are no rows or columns in the sheet, or if the last update
  // was more than one hour ago, send an email
  if (lastRow == 0 || lastColumn == 0 ||
      currentTime.getTime() - sheet.getLastUpdated().getTime() > 3600000) {
    // Set the email address to send the notification to
    var emailAddress = "you@example.com";

    // Set the subject and body of the email
    var subject = "No data in Google Sheets document";
    var body = "There has been no data entered in the Google Sheets document " +
               "in the last hour. Please check the data entry process and " +
               "ensure that everything is working as expected.";

    // Send the email
    MailApp.sendEmail(emailAddress, subject, body);
  }
}

// Run the checkForData function every hour
setInterval(checkForData, 3600000);

在此示例中,checkForData 函数使用 setInterval 方法每小时调用一次。该函数检查活动工作表中的最后一行和最后一列,以及工作表的最后一次更新时间,如果工作表中没有行或列或者上次更新是在一个多小时前,则发送一封电子邮件。

您可以调整此脚本中的时间段和其他设置以满足您的特定需求。例如,您可以更改将通知发送到的电子邮件地址,或者您可以更改时间段以增加或减少检查数据输入的频率。

编写脚本后,您可以将其部署为 Google 表格插件,使其在您的表格中可用。如果工作表中没有数据条目,这将允许您使用脚本自动发送电子邮件通知。

【讨论】:

    猜你喜欢
    • 2019-08-27
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    相关资源
    最近更新 更多