LLA*_*LAE 3 time datetime-format google-apps-script
经过几个小时的寻找理性或解决方案后,我现在希望这个社区!
我拼命地试图获取(“读取”)用户在谷歌电子表格中输入的时间,并在谷歌应用程序脚本中正确使用它,例如创建谷歌日历事件。
所需的格式为“HH:mm”
我的起点是https://developers.google.com/apps-script/quickstart/forms上提供的 google apps 脚本示例
在这个例子中,我使用“更改区域设置和时区”说明修改了电子表格的参数(对不起法国人!): 设置插图
我还更改了“C”和“D”列的显示格式,以便不在初始示例中放入 AM/PM:
Start Time End Time
13:00:00 14:55:00
13:00:00 14:55:00
...
Run Code Online (Sandbox Code Playgroud)
为了在脚本编辑器中启用调试,我删除了 setUpConference 末尾的“_”(第 14 行)。
我在调试中启动了脚本“setUpConference”来检查从数据表中读取的值。让我惊讶的是第一条数据线
Ethics for monsters 5/15/2013 13:00:00 14:55:00 Rm 323: Minotaur's Labyrinth
Run Code Online (Sandbox Code Playgroud)
变量“session”对应的数据
["Ethics for monsters", (new Date(1368568800000)), (new Date(-2209115361000)), (new Date(-2209108461000)), "Rm 323: Minotaur's Labyrinth"]
Run Code Online (Sandbox Code Playgroud)
并sessions[2]在脚本编辑器中显示为:
Sat Dec 30 1899 13:50:39 GMT+0100 (CET)
我知道只有“时间”(HH:mm),日期不完整(所以是1899年),但如何获取时间“13:00:00”而不是这个奇怪的“13:50:39”?
PS:我的日历时区也是GMT+0100(CET)
一些包含更多信息的编辑:
我简化了我的谷歌应用程序脚本的代码以专注于该问题(初始代码是谷歌在https://developers.google.com/apps-script/quickstart/forms上提供的代码)
/** * 一种特殊函数,可在电子表格打开时插入自定义菜单。*/ function onOpen() { var menu = [{name: '设置会议', functionName: 'setUpConference'}]; SpreadsheetApp.getActive().addMenu('会议', 菜单); }
/**
* A set-up function that uses the conference data in the spreadsheet to create
* Google Calendar events, a Google Form, and a trigger that allows the script
* to react to form responses.
*/
function setUpConference() {
/* if (ScriptProperties.getProperty('calId')) {
Browser.msgBox('Your conference is already set up. Look in Google Drive!');
}*/
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('Conference Setup');
var range = sheet.getDataRange();
var values = range.getValues();
setUpCalendar(values, range);
}
/**
* Creates a Google Calendar with events for each conference session in the
* spreadsheet, then writes the event IDs to the spreadsheet for future use.
*
* @param {String[][]} values Cell values for the spreadsheet range.
* @param {Range} range A spreadsheet range that contains conference data.
*/
function setUpCalendar(values, range) {
// comment cal for debug
//var cal = CalendarApp.createCalendar('Test Conference Calendar');
for (var i = 1; i < values.length; i++) {
var session = values[i];
var title = session[0];
Logger.log("i= "+i+" - "+ "session[2]= " + session[2] + " | session[3] =" + session[3] );
// This formats the date as Greenwich Mean Time in the format
// year-month-dateThour-minute-second.
var formattedHour = Utilities.formatDate(session[2], "GMT+1", "HH:mm");
Logger.log("formattedHour = "+formattedHour);
var start = joinDateAndTime(session[1], session[2]);
var end = joinDateAndTime(session[1], session[3]);
var options = {location: session[4], sendInvites: true};
// comment cal and event creation
/*var event = cal.createEvent(title, start, end, options)
.setGuestsCanSeeGuests(false);
session[5] = event.getId();*/
}
range.setValues(values);
}
/**
* Creates a single Date object from separate date and time cells.
*
* @param {Date} date A Date object from which to extract the date.
* @param {Date} time A Date object from which to extract the time.
* @return {Date} A Date object representing the combined date and time.
*/
function joinDateAndTime(date, time) {
date = new Date(date);
date.setHours(time.getHours());
date.setMinutes(time.getMinutes());
return date;
}
Run Code Online (Sandbox Code Playgroud)正如一些评论表中所链接的,JS 使用不同的日期纪元,并且它们并不总是表现得很好。
更改var values = range.getValues();为var values = range.getDisplayValues();
这将迫使它将单元格值作为字符串获取。
按如下方式更改日期连接函数将使其处理字符串(可能需要确保电子表格中的日期有前导零):
function joinDateAndTime(date, time) {
var t = new Date(date);
t.setHours(parseInt(time.substring(0, 2)));
t.setMinutes(parseInt(time.substring(3, 5)));
return t;
}
Run Code Online (Sandbox Code Playgroud)