在花了几个小时确定一个合理的或解决方案之后,我现在对这个社区寄予希望!
我拼命地尝试获取(“读取”)用户在谷歌电子表格中输入的时间,并在谷歌应用程序脚本中正确使用它,例如创建谷歌日历事件。
所需格式为“HH:mm”
我的出发点是 https://developers.google.com/apps-script/quickstart/forms 上提供的 google apps 脚本示例
在这个例子中,我使用“Change locale and time zone”指令修改了电子表格的参数(抱歉是法语!): settings illustration
我还更改了列“C”和“D”的显示格式,以便在初始示例中不显示上午/下午:
Start Time End Time
13:00:00 14:55:00
13:00:00 14:55:00
...
为了在脚本编辑器中启用调试,我删除了 setUpConference 末尾的“_”(第 14 行)。
我在调试中启动了脚本“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)
一些带有更多信息的编辑:
- 我 share the google spreadsheet我用于测试
我简化了我的谷歌应用程序脚本的代码以专注于这个问题(初始代码是谷歌在 https://developers.google.com/apps-script/quickstart/forms 上提供的代码
/** * 在电子表格打开时插入自定义菜单的特殊功能。 */ 打开函数(){ var menu = [{name: '设置 session ', functionName: 'setUpConference'}]; SpreadsheetApp.getActive().addMenu(' session ', 菜单);
/** * 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; }
最佳答案
正如一些评论表中所链接的那样,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;
}
关于time - 如何使用 google apps 脚本在 googlespreadsheet 中读取时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49628060/