Google Calendar: Insert Event
Insert an event to Google Calendar.
Configs: Common
  • Step Name
  • Note
Configs
  • C1: User who connects to Google Calendar *
  • C2: Calendar ID (When empty, inserted to the primary calendar)
  • C3: Event Title *#{EL}
  • C4: Data item for Event Start Datetime *
  • C5: Data item for Event End Datetime (Not-selected +1:00)
  • C6: Location#{EL}
  • C7: Description#{EL}
  • C8: Data item that will save Event ID
  • C9: Data item that will save Event URL
  • C10: Data item that will save Google Meet URL
Script (click to open)

main();
function main() {
  //// == 工程コンフィグの参照 / Config Retrieving ==
  const quser = configs.getObject("conf_User");
  if (quser === null) {
    throw `User not found.`;
  }
  let calendarId = configs.get("conf_DataIdB");
  if (calendarId === "" || calendarId === null) {
    calendarId = "primary";
  }
  const eventSummary = configs.get("conf_DataIdC");
  if (eventSummary === "" || eventSummary === null) {
    throw "Event Title is blank";
  }
  const startDate = engine.findData(configs.getObject("conf_DataIdD"));
  if (startDate === null) {
    throw "Event Start Date is blank";
  }
  const endDateDef = configs.getObject("conf_DataIdE");
  let endDate = null;
  if (endDateDef !== null) {
    endDate = engine.findData(endDateDef);
  }
  let eventLocation = configs.get("conf_DataIdF");
  if (eventLocation === null) {
    eventLocation = "";
  }
  let eventDescription = configs.get("conf_DataIdG");
  if (eventDescription === null) {
    eventDescription = "";
  }

  const eventIdDataDef = configs.getObject("conf_eventId");
  const eventUrlDataDef = configs.getObject("conf_eventUrl");
  const meetUrlDataDef = configs.getObject("conf_meetUrl");

  //// == 演算 / Calculating ==
  if (calendarId.search(/[^\w\.@]/) !== -1) {
    throw "Invalid Calendar ID";
  }

  if (endDate === null) {
    endDate = startDate.addHours(1);
  }
  if (endDate.getTime() < startDate.getTime()) {
    throw "Event End Date comes before Event Start Date";
  }

  //// == 予定を追加する / Insert Event ==
  insertEvent(
    quser,
    calendarId,
    eventSummary,
    eventLocation,
    eventDescription,
    startDate,
    endDate,
    eventIdDataDef,
    eventUrlDataDef,
    meetUrlDataDef
  );
}

/**
 * Google カレンダーにイベントを追加する
 * @param {QuserView} quser ユーザー
 * @param {String} calendarId カレンダーID
 * @param {String} eventSummary 予定タイトル
 * @param {String} eventLocation 場所情報
 * @param {String} eventDescription 説明情報
 * @param {AddableTimestamp} startDate 開始日時
 * @param {AddableTimestamp} endDate 終了日時
 * @param {ProcessDataDefinitionView} eventIdDataDef Event ID を保存する文字型データ項目
 * @param {ProcessDataDefinitionView} eventUrlDataDef Event URL を保存する文字型データ項目
 * @param {ProcessDataDefinitionView} meetUrlDataDef Meet URL を保存する文字型データ項目
 */
function insertEvent(
  quser,
  calendarId,
  eventSummary,
  eventLocation,
  eventDescription,
  startDate,
  endDate,
  eventIdDataDef,
  eventUrlDataDef,
  meetUrlDataDef
) {
  const uri = `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events`;

  const sdf = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

  const myObj = {};
  myObj.summary = eventSummary;
  myObj.location = eventLocation;
  myObj.description = eventDescription;
  myObj.start = {};
  myObj.start.dateTime = sdf.format(startDate);
  myObj.start.timeZone = engine.getTimeZoneId();
  myObj.end = {};
  myObj.end.dateTime = sdf.format(endDate);
  myObj.end.timeZone = engine.getTimeZoneId();
  myObj.source = {};
  myObj.conferenceData = {};
  myObj.conferenceData.createRequest = {};
  myObj.conferenceData.createRequest.requestId = `q-${processInstance.getProcessInstanceId()}`;
  myObj.conferenceData.createRequest.conferenceSolutionKey = {};
  myObj.conferenceData.createRequest.conferenceSolutionKey.type =
    "hangoutsMeet";

  let request = httpClient.begin().googleOAuth2(quser, "Calendar");
  if (meetUrlDataDef !== null) {
    request = request.queryParam("conferenceDataVersion", "1");
  }
  const response = request
    .body(JSON.stringify(myObj), "application/json")
    .post(uri);

  const status = response.getStatusCode();
  if (status >= 300) {
    const accessLog = `---POST request---${status}\n${response.getResponseAsString()}`;
    engine.log(accessLog);
    throw `Failed to insert event. status:${status}`;
  }

  const responseStr = response.getResponseAsString();
  const eventJson = JSON.parse(responseStr);
  const eventId = eventJson.id;
  const eventUrl = eventJson.htmlLink;
  const meetUrl = eventJson.hangoutLink;
  if (eventIdDataDef !== null) {
    engine.setData(eventIdDataDef, eventId);
  }
  if (eventUrlDataDef !== null) {
    engine.setData(eventUrlDataDef, eventUrl);
  }
  if (meetUrlDataDef !== null) {
    engine.setData(meetUrlDataDef, meetUrl);
  }
}

Notes

  1. Users in C1 need to have a configured connection with Google Calendar in [Account Settings] > [Google Connectivity]
  2. Refer to the following page for Calendar ID [Calendar Settings] > [Calendar Address]
  3. An event with a start time and end time will be added (you cannot add an all-day schedule)
  4. If an end time Data Item has not been set or the end time has not been input, 60 minutes after start time will be set
  5. If the end time is earlier than the start time the event will not be added to the calendar

Capture

See also

Appendix

  • google-calendar-event-insert.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on
%d bloggers like this: