有没有办法使用脚本将Google表单提交中的日期从工作表中提取到Gmail?

时间:2019-03-29 12:37:24

标签: datetime google-apps-script google-sheets gmail google-form

我没有编写脚本的经验,但是我能够找到一个脚本并对其进行编辑(经过反复试验),以满足我的需要。

我有一个Google表单,其中第一个问题允许用户选择日期,但不一定是用户填写表单的日期。结果将导出到Google表格中,并且我有一个脚本可以发送带有表单回复的电子邮件。

在夏令时之前,它的运作非常漂亮。现在,电子表格中的日期是正确的,但电子邮件中的日期是一天休息。

电子邮件示例:

  

您的孩子NAME在2019年3月27日星期三受到着装要求的侵犯   23:00:00 GMT-0600(CST),无ID。

夏令时之前,时间显示为00:00:00

在代码中,第[2]行是从电子表格中提取的日期。

var EMAIL_SENT = "EMAIL_SENT";

function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Script");
var startRow = 2;
var numRows = 5000;
var dataRange = sheet.getRange(startRow, 1, numRows, 5000)
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[12];
var message = "Your child, " + row[10] + ", received a dress code violation on " + row[2] + ", for " + row[11] + ".\nIf you have any questions, please email NAME at name.name@name.org\n\nThank you,\n\nNAME\nAssistant Principal";
var emailSent = row[13];
if (emailSent != EMAIL_SENT) {
var subject = "Uniform Violation - Do Not Reply";
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 14).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
    } 
  }
 }

理想情况下,电子邮件将以MM/DD/YYYY格式从电子表格中准确提供日期。

相反,电子邮件显示的是前一天的晚上11点。

1 个答案:

答案 0 :(得分:0)

尝试一下:

var EMAIL_SENT = "EMAIL_SENT";

function sendEmails2() {
  var ss=SpreadsheetApp.getActive();//added this
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Script");
  var startRow = 2;
  var numRows = 5000;
  var dataRange = sheet.getRange(startRow, 1, numRows, 5000)
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[12];
    var message = "Your child, " + row[10] + ", received a dress code violation on " + Utilities.formatDate(new Date(row[2]),ss.getSpreadsheetTimeZone(), "MM dd, yyyy HH:mm:ss" ) + ", for " + row[11] + ".\nIf you have any questions, please email NAME at name.name@name.org\n\nThank you,\n\nNAME\nAssistant Principal";//modified this
    var emailSent = row[13];
    if (emailSent != EMAIL_SENT) {
      var subject = "Uniform Violation - Do Not Reply";
      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 14).setValue(EMAIL_SENT);
      SpreadsheetApp.flush();
    } 
  }
}

您可能必须更改日期格式。