问题描述
我有一个 Google 脚本,它需要输入 Google 表单字段的日期,我想分别保存日、月和年。特别是,我想将月份编号更改为文本,即 03
表示 march
。这就是我使用 substring
的原因。
- Google 表单中的日期格式:
dd/mm/yyyy
。 - Google 电子表格中的日期格式:
d/mm/yyyy
ifday<10
和dd/mm/yyyy
ifday>=10
。
关键部分在这里:
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);
我们如何解决这个问题不更改 Google 电子表格文件的格式?
我找不到使用 if
或 switch
语句的方法。
更多说明
我尝试使用此代码:
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)
}
输出:
参考:
,
if (P.equalsIgnoreCase("Margarita")){
System.out.println("That will be " + (Margarita) + ".");
}
else if (P.equalsIgnoreCase("Vesuvio")){
System.out.println("That will be " + (Vesuvio) + ".");
}
else if (P.equalsIgnoreCase("Hawaii")){
System.out.println("That will be " + (Hawaii) + ".");
}
else if (P.equalsIgnoreCase("Kebab Pizza")){
System.out.println("That will be " + (Kebab_Pizza) + ".");
}
else {
System.out.println("We don't serve that here.");
}