花了几个小时来确定一个理性或解决方案,我现在希望这个社区!
我正在试图获取(“阅读”)用户在google spreasheet中输入的时间,并在谷歌应用脚本中正确使用它,例如创建谷歌日历活动。
所需格式为“HH:mm”
我的出发点是https://developers.google.com/apps-script/quickstart/forms
上提供的google apps脚本示例在这个例子中,我使用“Change locale and time zone”指令修改了spreasheet的参数(对不起法国人!): settings illustration
我还更改了列'C'和'D'的显示格式,以便在初始示例中没有放置AM / PM:
Start Time End Time
13:00:00 14:55:00
13:00:00 14:55:00
...
要在脚本编辑器中启用调试,我在setUpConference的末尾删除了“_”(第14行)。
我在debug中启动了脚本“setUpConference”来检查从数据表中读取的值。 我的惊喜是第一条数据线
Ethics for monsters 5/15/2013 13:00:00 14:55:00 Rm 323: Minotaur's Labyrinth
变量“session”的相应数据
["Ethics for monsters", (new Date(1368568800000)), (new Date(-2209115361000)), (new Date(-2209108461000)), "Rm 323: Minotaur's Labyrinth"]
和sessions[2]
在脚本编辑器中显示为:
1899年12月30日星期六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:'Set up conference',functionName:'setUpConference'}]; SpreadsheetApp.getActive()。addMenu('Conference',menu); }
/**
* 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;
}
答案 0 :(得分:2)
在一些评论表中链接,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;
}