Google Calendar: Insert Event
Insert an event to Google Calendar.
https://support.questetra.com/addons/services/googlecalendar-insertevent/
2018-12-12 (C) Questetra, Inc. (MIT License)
Configs
  • C1:User Who Connects with Google Calendar *
  • C2:Calendar ID(When empty,inserted to the primary calendar)
  • I1:Event Title * #{EL}
  • I2:Select DATETIME DATA for Event Start *
  • I3:Select DATETIME DATA for Event End (Not-selected +1:00)
  • I4:Location #{EL}
  • I5:Description #{EL}
Script
//// == 工程コンフィグの参照 / Config Retrieving ==
var userId = configs.get( "conf_User" );
var quser = quserDao.findById(parseInt(userId));
if (quser == null) {
  throw "User not found:" + userId;
}
var dataIdB = configs.get( "conf_DataIdB" );
var dataIdC = configs.get( "conf_DataIdC" );
if (dataIdC == "" ||dataIdC == null) {
    throw "Event Title is blank";
  }
var dataIdD = configs.get( "conf_DataIdD" );
var dataIdE = configs.get( "conf_DataIdE" );
var dataIdF = configs.get( "conf_DataIdF" );
var dataIdG = configs.get( "conf_DataIdG" );
//var eventSource = configs.get( "conf_EventSource" ) + "";
//var eventSourceUrl = configs.get( "conf_EventSourceUrl" ) + "";

//// == ワークフローデータの参照 / Data Retrieving ==
var calendarId = "";
if(dataIdB instanceof java.util.ArrayList ){
  calendarId = dataIdB.get(0).getValue() + "";
}else if( dataIdB instanceof java.lang.String ){
  calendarId = dataIdB+ "";
}
if (calendarId == "" ||calendarId == null) {
    calendarId = "primary";
  }
var eventSummary =dataIdC + "";
if (dataIdD == "" || engine.findDataByNumber( dataIdD ) == null) {
  throw "Event Start Date is blank";
}else{
  var eventStart = engine.findDataByNumber( dataIdD ).toString() + "";
}
eventStart = eventStart.replace(" ", "T") + ":00";
if( dataIdE !== "" && engine.findDataByNumber( dataIdE ) !== null ){
  var eventEnd = engine.findDataByNumber( dataIdE ).toString() + "";
  eventEnd = eventEnd.replace(" ", "T") + ":00";
}else{
  var eventEnd = engine.findDataByNumber( dataIdD ).addHours(1).toString() + "";
  eventEnd = eventEnd.replace(" ", "T") + ":00";
}
//日時の検証
var sdf = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
var startDate = sdf.parse(eventStart);
var endDate = sdf.parse(eventEnd);
if (endDate.before(startDate)){
  throw "Event End Date comes before Event Start Date";
}
var eventLocation = "";
if( dataIdF !== "" && dataIdF !== null ){
  eventLocation = dataIdF + "";
}
var eventDescription = "";
if( dataIdG !== "" && dataIdG !== null ){
  eventDescription =dataIdG + "";
}

var timeZone = engine.getTimeZoneOffsetInMinutes()/60;
if (timeZone > 0){
  timeZone = "+" + timeZone
}
var eventTimeZone = "GMT" + timeZone + ":00";
//// == 演算 / Calculating ==
var accessLog = "";
var token;
try{
    token = httpClient.getGoogleOAuth2Token(quser, "Calendar");
  }catch(e){
    throw "This User has not connected with Google Calendar."
  }
var uri = "https://www.googleapis.com/calendar/v3/calendars/";
uri += calendarId;
uri += "/events";

var myObj = {};
myObj.summary = eventSummary;
myObj.location = eventLocation;
myObj.description = eventDescription;
myObj.start = {};
myObj.start.dateTime = eventStart;
myObj.start.timeZone = eventTimeZone;
myObj.end = {};
myObj.end.dateTime = eventEnd;
myObj.end.timeZone = eventTimeZone;
myObj.source = {};
/* if( eventSource !== "" ){
  myObj.source.title = eventSource;
}
if( eventSourceUrl !== "" ){
  myObj.source.url = eventSourceUrl;
}
*/
//debug
//accessLog += JSON.stringify( myObj ) + "\n"; 

var response = httpClient.begin()
  .bearer( token )
  .body( JSON.stringify( myObj ), "application/json" )
  .post( uri );
var status = response.getStatusCode();
accessLog += "---POST request--- " + status + "\n";
accessLog += response.getResponseAsString() + "\n";

//// == ログの出力 / Log Output ==
if (status >= 300) {
    throw accessLog;
}
engine.log(accessLog);

Download

google-calendar-event-insert.xml

Capture

Notes

  1. Refer to the following page for Calendar ID [Calendar Settings] > [Calendar Address]
  2. Schedule (event) with a start time and end time will be added (You cannot add an “all-day schedule”)
  3. When the end time data item has not been set or the end time has not been input, 60 minutes after start time will be set
  4. Schedule which the end time is earlier than the start time will not be added to the calendar