简体   繁体   English

如何使用当地时间创建Google日历活动?

[英]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: 我试过了:

  1. Looking for a Google Calendar method that would accept local time (aka send 6:55PM and Google Calendar API convert it to UTC) 寻找可以接受当地时间的Google日历方法(又称为发送6:55 PM,Google日历API将其转换为UTC)
  2. Looking for a native JS method that convert the local time to UTC. 寻找将本地时间转换为UTC的本地JS方法。

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.

相关问题 如何在其他人的Google日历上创建活动 - How to create an event on others google calendar 如何从列中获取时间值,延长持续时间,然后在谷歌日历上创建事件? - How to get value of time from a column, extend the duration, and then create event on google calendar? 如何在事件 object 中为谷歌日历 api 设置开始和结束时间 - How to set start and end time in event object for google calendar api 如何在GAS中转换和传递日期以在Google日历中创建事件? - How to convert and pass the date in GAS to create an event in Google calendar? 将Google Calendar事件结束时间添加到脚本中 - Adding Google Calendar event end time to script 从电子表格创建事件谷歌日历 - Create event google calendar from spreadsheet 完整的日历错误时间点击事件谷歌日历 - Full Calendar wrong time on click event google calendar 如何使用 Google Apps 脚本通过给定的 Meet URL 创建/更改 Google 日历活动? - How do I create/change a Google Calendar Event with a given Meet URL using Google Apps Script? SOS尝试通过Google在Google日历中创建事件(表格+脚本),但失败。 我认为这与时间有关吗? - S.O.S. Trying to create an event in google calendar via google (sheets + script), but failing. I think it has to do with the time? 添加到 Google 日历呈现链接不显示用户的当地时间 - Add to Google Calendar render link not showing user's local time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM