[英]How to create a google calendar event with local time?
I am trying to create a Google Calendar events from a Google Spreadsheet: 我正在尝试从Google Spreadsheet创建Google Calendar事件:
Example Spreadsheet: 电子表格示例:
(the details and dates are irregular so simple reoccurring event doesn't quiet cut it) (详细信息和日期是不规则的,因此简单的重复发生的事件不会使其安静下来)
Code: 码:
function walk_sheet(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("CalendarGenerator");
for (var i = 2; i <= 39; i++) {
var date = sheet.getRange(i, 2).getValue();
var start = parseTime(sheet.getRange(i, 3).getDisplayValues().toString() ) ;
var end = parseTime(sheet.getRange(i, 4).getDisplayValues().toString() ) ;
var summary= sheet.getRange(i, 6).getValue();
var disc = sheet.getRange(i, 7).getValue();
add_bsf_event(date, start, end, summary, disc);
}
}
function add_bsf_event(dateIn, start, end, summary, disc){
start_date= new Date(dateIn.getYear(), dateIn.getMonth(), dateIn.getDate(), start.getHours(), start.getMinutes(),0,0);
end_date = new Date(dateIn.getYear(), dateIn.getMonth(), dateIn.getDate(), end.getHours(), end.getMinutes(),0,0);
...
CalendarApp.getCalendarById(calendarId).createEvent(summary, start_date, end_date);
}
And it works with the exception of Daylight Savings Time. 它与夏令时除外。 Starting after 3/13/2017 events in Google Calendar are 1 hour later then requested (7:55pm instead of 6:55pm).
从2017年3月13日之后开始,Google日历中的事件会在1小时后才被请求(晚上7:55而不是晚上6:55)。
I've tried: 我试过了:
Haven't been able to find either solution. 找不到任何解决方案。 Is there one that I am not finding or is different/better approach to this problem?
是否有我找不到的解决方案,或者是采用其他/更好的方法来解决此问题?
You can try to check the Google Calendar API documentation , it mentions here that you can use a separate timezone for start and end of an event. 您可以尝试查看Google Calendar API 文档 ,此处提到您可以为事件的开始和结束使用单独的时区。
Now, for the method that convert timezone, I think this tutorial can help you. 现在,对于转换时区的方法,我认为本教程可以为您提供帮助。 It explains here step by step the things that you need to do in order to achieve this conversion.
它一步一步地说明了实现此转换所需要做的事情。
For more information, check this related SO question even though this code is in JAVA. 有关更多信息,即使此代码在JAVA中,也请检查此相关的SO问题 。
My solution was to insert the events using the Google Calendar Ruby API. 我的解决方案是使用Google Calendar Ruby API插入事件。 It was bit of pain to setup but once I got the connection established and boiler plate code down, Ruby handle the daylight saving time changes flawlessly.
设置有些麻烦,但是一旦我建立了连接并且简化了样板代码,Ruby就可以完美地处理夏令时。
Google quick start guide does a decent job: https://developers.google.com/google-apps/calendar/quickstart/ruby Google快速入门指南做得不错: https : //developers.google.com/google-apps/calendar/quickstart/ruby
However if your running on Windows chances are SSL certificate will fail to verify; 但是,如果您在Windows上运行,则SSL证书将无法验证; you can disable it by:
您可以通过以下方式禁用它:
require 'openssl'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
Ruby method I used for inserting events: 我用于插入事件的Ruby方法:
def insert_event(summary, description, start_time, end_time, location, cal_id='primary')
event = Google::Apis::CalendarV3::Event.new({
summary:summary,
#color_id:5,
location:location,
description:description,
start:{date_time:start_time.to_datetime.rfc3339},
end:{date_time:end_time.to_datetime.rfc3339},
#reminders:{use_default: true}
})
#puts event.to_json
result = $service.insert_event(cal_id, event)
#puts result
puts "Event created: #{result.html_link}"
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.