如何根据Google表格中的单元格日期发送提醒日期?

时间:2017-03-27 16:17:20

标签: google-apps-script google-sheets google-sheets-api

我一直在研究如何根据Google表格中的单元格中的特定日期发送电子邮件。我找到了这个article,它几​​乎完全符合我想做的事情,因为时间对我来说无关紧要。

我的问题是,当我使用代码时,我得到错误TypeError:无法在对象中找到函数toLocaleDateString。 (第19行,文件"代码")。

以下是我拥有的工作表设置的副本:https://docs.google.com/spreadsheets/d/1FlrpvLMRMuq8t6pI4inlKI2acrZJ68pyGHQ7Xtqi5AU/edit?usp=sharing

这是我的代码,格式化我的列等

Index: lib/wiking_wiki_helper_patch.rb
===================================================================
--- lib/wiking_wiki_helper_patch.rb (revision 78)
+++ lib/wiking_wiki_helper_patch.rb (working copy)
@@ -49,10 +49,10 @@

             if defined? ChiliProject
                 url = url_for(:controller => 'help', :action => 'wiki_syntax')
-            elsif File.exists?(File.join(Rails.root, 'public/help', current_language.to_s.downcase, 'wiki_syntax.html'))
-                url = "#{Redmine::Utils.relative_url_root}/help/#{current_language.to_s.downcase}/wiki_syntax.html"
+            elsif File.exists?(File.join(Rails.root, 'public/help', current_language.to_s.downcase, 'wiki_syntax_textile.html'))
+                url = "#{Redmine::Utils.relative_url_root}/help/#{current_language.to_s.downcase}/wiki_syntax_textile.html"
             else
-                url = "#{Redmine::Utils.relative_url_root}/help/wiki_syntax.html"
+                url = "#{Redmine::Utils.relative_url_root}/help/wiki_syntax_textile.html"
             end

             if File.exists?(File.join(Rails.root, 'plugins/wiking/assets/help/', current_language.to_s.downcase, 'wiki_syntax.html'))

如何修复此错误?除了错误之外,还有一种方法可以通过电子邮件发送我工作表中的两个联系点?

感谢您提供的所有帮助。

2 个答案:

答案 0 :(得分:0)

您收到错误的最可能原因是您尝试访问空单元格并尝试将其转换为日期字符串。

修改此行:

var numRows = 999;

以下

var numRows = sheet.getLastRow()-1 // Get last row, -1 because your startrow is 2

因此您只能访问包含数据的行。 另外,要向多个人发送电子邮件,只需将所有电子邮件包含在逗号分隔的字符串中,如下所示:

var emailAddress = row[3] + "," + row[4];  // POC Email column D and E (Just create a comma seprated email list to send email to multiple people)

查找以下修改后的代码:

function sendEmails3() {
  var today = new Date().toLocaleDateString();  // Today's date, without time

  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = sheet.getLastRow() - 1;   // Number of rows to process, -1 since you are starting from row 2
  // Fetch the range of cells A2:G(LastRow -1) 
  var dataRange = sheet.getRange(startRow, 1, numRows, 8)
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[3] + "," + row[4];  // POC Email column D and E (Just create a comma seprated email list to send email to multiple people)
    var subject = row[5];     // Subject column F
    var message = row[6];    // Message column G
    var emailSent = row[7];   // Output the message is sent in column H
    var reminderDate = row[2].toLocaleDateString();  // date specified in cell C

    if (reminderDate != today)      // Skip this reminder if not for today
      continue;

    if (emailSent != EMAIL_SENT) {  // Prevents sending duplicates

      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 8).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}

希望有所帮助!

答案 1 :(得分:0)

这一行:

var reminderDate = row[2].toLocaleDateString();  // date specified in cell C

你应该分解为多个操作:

var cellValue,reminderDate;//Define variables at top of function

cellValue = row[2];// date specified in cell C

if (!cellValue) {continue;}//If there is no cell value continue looping

if (typeof cellValue === 'object') {
  reminderDate = cellValue.toLocaleDateString();
} else {
  cellValue = new Date(cellValue);//Convert date as string to object
  reminderDate = cellValue.toLocaleDateString();
}