如何使用 Google 脚本处理从表单到电子表格的日期

How to deal with dates from Forms to Spreadsheets using Google Script

我有一个 Google 脚本,它接受 Google 表单字段的日期输入,我想分别保存日、月和年。特别是,我想将月份编号更改为文本,即 03 表示 March。这就是为什么我使用 substring.

关键部分在这里:

const cDay = e.namedValues['Date'][0].substring(0,2);
var cMonth = e.namedValues['Date'][0].substring(3,5);
const cYear = e.namedValues['Date'][0].substring(6,10);

如果用户每天提交的次数少于 10,则代码已损坏。

我们如何解决这个问题不更改 Google 电子表格文件的格式?

我找不到使用 ifswitch 语句的方法。

更多解释

我试过这个代码:

var cDay;
var cMonth;
var cYear;
if (e.namedValues['Date'][0].substring(1,2) == '/') { // Days are 1,..,9
  cDay = e.namedValues['Date'][0].substring(0,1);
  cMonth = changeMonth(e.namedValues['Date'][0].substring(2,4));
  cYear = e.namedValues['Date'][0].substring(5,9);
} else { // Days are 10,..,31
  cDay = e.namedValues['Date'][0].substring(0,2);
  cMonth = changeMonth(e.namedValues['Date'][0].substring(3,5));
  cYear = e.namedValues['Date'][0].substring(6,10);
}

函数changeMonth:

function changeMonth(month) {
  switch (month) {
    case '01':
    case '1':
      cMonth = 'January';
      break;
    case '02':
    case '2':
      cMonth = 'February';
      break;
    case '03':
    case '3':
      cMonth = 'March';
      break;
    case '04':
    case '4':
      cMonth = 'April';
      break;
    case '05':
    case '5':
      cMonth = 'May';
      break;
    case '06':
    case '6':
      cMonth = 'June';
      break;
    case '07':
    case '7':
      cMonth = 'July';
      break;
    case '08':
    case '8':
      cMonth = 'August';
      break;
    case '09':
    case '9':
      cMonth = 'September';
      break;
    case '10':
      cMonth = 'October';
      break;
    case '11':
      cMonth = 'November';
      break;
    case '12':
      cMonth = 'December';
      break;
  }
}

我使用replaceText:

时似乎出现了错误
body.replaceText('<<Day>>', cDay);
body.replaceText('<<Month>>', cMonth); // I get "Invalid argument: replacement"
body.replaceText('<<Year>>', cYear);

我发现一个解决方案是将 String(...) 添加到 replaceText 的参数中,所以我有:body.replaceText(String('<<Month>>'), String(cMonth));.

但是,当我看到结果时,我得到了正确的日期和年份,但月份是 undefined。我们能做什么?

例子

如果用户在表单上提交 03/05/2000,输出必须是:You have submitted the form on the 3º day of the month of May of the year 2000。相反,我有 You have submitted the form on the 3º day of the month of undefined of the year 2000.

您可以使用 JavaScript Date 对象来获取日、月和年,而不是自己解析日期。

注意:日期格式为dd/mm/yyyy

时适用此方案

试试这个:

function formSubmit(e) {
  var dateStr = e.namedValues["Date"][0];
  var dateArr = dateStr.split("/");
  var newDate = dateArr[1]+"/"+dateArr[0]+"/"+dateArr[2];
  var date = new Date(newDate);
  var day = date.getDate();
  var month = date.getMonth();
  var year = date.getFullYear();
  
  const monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
  ];

  Logger.log("You have submitted the form on the "+day+"º day of the month of "+monthNames[month]+" of the year "+year)
}

输出:

参考:

function getMDY(input="01/04/2021") {
  const a=['Day','Month','Year'];
  Logger.log(input.split('/').map((e,i)=>{ return a[i] + ": " + parseInt(e)}).join('\r\n'));
}

Execution log
12:38:40 PM Notice  Execution started
12:38:40 PM Info    
Day: 1
Month: 4
Year: 2021
12:38:40 PM Notice  Execution completed