Google Calendar: Move Event to another Calendar
Moves an event on Google Calendar to another calendar.
Configs: Common
  • Step Name
  • Note
Configs
  • C1: User who connects to Google Calendar *
  • C2: Calendar ID of the current event (Primary Calendar if blank)
  • C3: Destination Calendar ID (Primary Calendar if blank)
  • C4: Event ID *
  • C5: Data item to save Event URL
Script (click to open)

main();

function main() {
    //// == 工程コンフィグの参照 / Config Retrieving ==
    const quser = configs.getObject("conf_User");
    if (quser === null) {
        throw `User not found.`;
    }
    const calendarId = getCalendarId("conf_CalendarId");
    const destination = getCalendarId("conf_Destination");

    const eventId = engine.findData(configs.getObject("conf_EventId"));
    if (eventId === "" || eventId === null) {
        throw "Event ID isn't set";
    }

    const htmlLinkDataDef = configs.getObject("conf_htmlLink");

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

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

    moveEvent(quser, calendarId, eventId, destination);
    getHtmlLink(quser, destination, eventId, htmlLinkDataDef);
}

/**
 * configs から カレンダー ID を取得する
 * @param confName 設定名
 * @returns {string} カレンダー ID
 */
function getCalendarId(confName) {
    let calendarId = configs.get(confName);
    if (calendarId === "" || calendarId === null) {
        return "primary";
    }
    return calendarId;
}

/**
 * Google カレンダーのイベントを別のカレンダーに移動する
 * @param {QuserView} quser ユーザー
 * @param {String} calendarId カレンダーID
 * @param {String} eventId 予定ID
 * @param {String} destination 移動先カレンダーID
 */
function moveEvent(quser, calendarId, eventId, destination) {

    const uri = `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events/${eventId}/move`;

    const myObj = {};
    myObj.destination = destination;

    const response = httpClient.begin()
        .googleOAuth2(quser, "Calendar")
        .body(JSON.stringify(myObj), "application/json")
        .post(uri);

    const status = response.getStatusCode();
    const responseStr = response.getResponseAsString();
    engine.log(`Event ID: ${eventId}`);
    if (status >= 300) {
        engine.log(`Status: ${status}`);
        engine.log(responseStr);
        throw `Failed to move.`;
    }
    engine.log('Succeeded to move');
}

/**
 * イベントを再取得し、HTML Link を保存する
 * @param quser
 * @param calendarId
 * @param eventId
 * @param htmlLinkDataDef
 */
function getHtmlLink(quser, calendarId, eventId, htmlLinkDataDef) {
    if (htmlLinkDataDef === null) {
        return;
    }
    const uri = `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events/${eventId}`;

    const response = httpClient.begin()
        .googleOAuth2(quser, "Calendar")
        .get(uri);

    const status = response.getStatusCode();
    const responseStr = response.getResponseAsString();
    if (status >= 300) {
        engine.log(`Status: ${status}`);
        engine.log(responseStr);
        throw `Failed to get htmlLink.`;
    }
    const respJson = JSON.parse(responseStr);
    const htmlLink = respJson['htmlLink'];
    engine.setData(htmlLinkDataDef, htmlLink);
    engine.log("Succeeded to get htmlLink");
}

Notes

  • Users in C1 need to have a configured connection with Google Calendar in [Account Settings] > [Google Connectivity].
  • Refer to the following page for Calendar ID. [Calendar Settings] > [Calendar Address]
  • Event ID can’t be found in Calendar Page. You should use one from a task such as Start: Google Calendar: Event Started.

Capture

See also

Appendix

  • google-calendar-event-move.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

1 thought on “Google Calendar: Move Event to another Calendar”

  1. Pingback: Start Processes from Appointments in Google Calendar – Questetra Support

Comments are closed.

%d bloggers like this: