スプレッドシートの内容からgoogleカレンダーを設定する
このブロックでは、 スプレッドシートの内容をもとにカレンダーを設定し、スプレッドシートにカレンダーIDを設定します。
この時点でのスプレッドシートとvalue、rangeの内容
会議予定スプレッドシート
| Session Title | Date | Start Time | End Time | Location | Event |
| Ethics for monsters | 5/15/2013 | 1:00 PM | 2:55 PM | Rm 323: Minotaur’s Labyrinth | |
| Lurking: under the bed or in the closet? | 5/15/2013 | 1:00 PM | 2:55 PM | Rm 160: Castle Dracula | |
| Upcoming trends in young-adult literature | 5/15/2013 | 3:00 PM | 4:55 PM | Rm 206: Loch Ness | |
| How Bigfoot stays hidden | 5/15/2013 | 3:00 PM | 4:55 PM | Rm 144: Imhotep’s Tomb | |
| Techniques to elicit the perfect scream | 5/16/2013 | 9:00 AM | 12:00 PM | Rm 278: Grendel’s Cave | |
| Godzilla vs. YOU | 5/16/2013 | 9:00 AM | 12:00 PM | Rm 395: Baba Yaga’s Hut | |
| Leprechauns: monsters or otherkin? | 5/16/2013 | 1:00 PM | 4:00 PM | Rm 197: The Graveyard | |
| So the cryptozoologists are onto you… | 5/16/2013 | 1:00 PM | 4:00 PM | Rm 010: Frankenstein’s Laboratory |
range
スプレッドシートのデーターが存在する範囲。( row=9行 colum=6列 )
values
rangeの 長方形グリッド Object[][]。スプレッドシートrangeの値。
スプレッドシートから読み取ったvaluesの内容
[19-12-16 17:25:22:685 JST] [ [Session Title Date Start Time End Time Location Event] [Ethics for monsters Wed May 15 16:00:00 GMT+09:00 2013 Sun Dec 31 06:00:00 GMT+09:00 1899 Sun Dec 31 07:55:00 GMT+09:00 1899 Rm 323: Minotaur's Labyrinth ] [Lurking: under the bed or in the closet? Wed May 15 16:00:00 GMT+09:00 2013 Sun Dec 31 06:00:00 GMT+09:00 1899 Sun Dec 31 07:55:00 GMT+09:00 1899 Rm 160: Castle Dracula ] [Upcoming trends in young-adult literature Wed May 15 16:00:00 GMT+09:00 2013 Sun Dec 31 08:00:00 GMT+09:00 1899 Sun Dec 31 09:55:00 GMT+09:00 1899 Rm 206: Loch Ness ] [How Bigfoot stays hidden Wed May 15 16:00:00 GMT+09:00 2013 Sun Dec 31 08:00:00 GMT+09:00 1899 Sun Dec 31 09:55:00 GMT+09:00 1899 Rm 144: Imhotep's Tomb ] [Techniques to elicit the perfect scream Thu May 16 16:00:00 GMT+09:00 2013 Sun Dec 31 02:00:00 GMT+09:00 1899 Sun Dec 31 05:00:00 GMT+09:00 1899 Rm 278: Grendel's Cave ] [Godzilla vs. YOU Thu May 16 16:00:00 GMT+09:00 2013 Sun Dec 31 02:00:00 GMT+09:00 1899 Sun Dec 31 05:00:00 GMT+09:00 1899 Rm 395: Baba Yaga's Hut ] [Leprechauns: monsters or otherkin? Thu May 16 16:00:00 GMT+09:00 2013 Sun Dec 31 06:00:00 GMT+09:00 1899 Sun Dec 31 09:00:00 GMT+09:00 1899 Rm 197: The Graveyard ] [So the cryptozoologists are onto you… Thu May 16 16:00:00 GMT+09:00 2013 Sun Dec 31 06:00:00 GMT+09:00 1899 Sun Dec 31 09:00:00 GMT+09:00 1899 Rm 010: Frankenstein's Laboratory ] ]
setUpCalendar_サンプルコード
/**
* Creates a Google Calendar with events for each conference session in the
* spreadsheet, then writes the event IDs to the spreadsheet for future use.
* @param {Array<string[]>} values Cell values for the spreadsheet range.
* @param {Range} range A spreadsheet range that contains conference data.
*/
function setUpCalendar_(values, range) {
var cal = CalendarApp.createCalendar('Conference Calendar');
for (var i = 1; i < values.length; i++) {
var session = values[i];
var title = session[0];
var start = joinDateAndTime_(session[1], session[2]);
var end = joinDateAndTime_(session[1], session[3]);
var options = {location: session[4], sendInvites: true};
var event = cal.createEvent(title, start, end, options)
.setGuestsCanSeeGuests(false);
session[5] = event.getId();
}
range.setValues(values);
// Store the ID for the Calendar, which is needed to retrieve events by ID.
ScriptProperties.setProperty('calId', cal.getId());
}
CalendarApp.createCalendar(values, range)
CalendarAppクラスの createCalendarメソッドを使って、 ユーザーが所有する新しいカレンダーを作成します。
CalendarApp.createCalendar(‘カレンダー名’):
指定のカレンダー名のgoogleカレンダーを作成。
CalendarApp.createCalendar(‘カレンダー名’, JavaScriptオブジェクト配列 option ):
JavaScriptオブジェクト 配列のoptionに基づいて 指定のカレンダー名のgoogleカレンダーを作成。
option例:サマリーと色の指定
{summary: 'A calendar to plan my travel schedule.',color: CalendarApp.Color.BLUE}
カレンダーにイベントを書き込む
cal.createEvent(title, startTime, endTime, options)招待メールを送信(options) sendInvites : true 送信する。 false 送信しない(デフォルト)
setGuestsCanSeeGuests: ゲストが他のゲストのカレンダーを見れるように設定
session[5] = event.getId():5colum目にカレンダーIDをセット。 デフォルトのカレンダーIDはメールアドレス。
スプレッドシートにデーターを書き込む
スプレッドシートにカレンダーID(メールアドレス)を追加したデーターを書き込む。
range.setValues(values);
スプレッドシートに書き込むvaluesの内容
[ [Session Title Date Start Time End Time Location Event] [Ethics for monsters Wed May 15 16:00:00 GMT+09:00 2013 Sun Dec 31 06:00:00 GMT+09:00 1899 Sun Dec 31 07:55:00 GMT+09:00 1899 Rm 323: Minotaur's Labyrinth 5b763ucndsc30s4kjtfg6n9d00@google.com] [Lurking: under the bed or in the closet? Wed May 15 16:00:00 GMT+09:00 2013 Sun Dec 31 06:00:00 GMT+09:00 1899 Sun Dec 31 07:55:00 GMT+09:00 1899 Rm 160: Castle Dracula crdgjtg8bbog8b779d69k0274c@google.com] [Upcoming trends in young-adult literature Wed May 15 16:00:00 GMT+09:00 2013 Sun Dec 31 08:00:00 GMT+09:00 1899 Sun Dec 31 09:55:00 GMT+09:00 1899 Rm 206: Loch Ness rgqiuqd33ok1kc13sj0vrgqlu0@google.com] [How Bigfoot stays hidden Wed May 15 16:00:00 GMT+09:00 2013 Sun Dec 31 08:00:00 GMT+09:00 1899 Sun Dec 31 09:55:00 GMT+09:00 1899 Rm 144: Imhotep's Tomb jr0v2bjlhqc9j68t1jcpq2mulk@google.com] [Techniques to elicit the perfect scream Thu May 16 16:00:00 GMT+09:00 2013 Sun Dec 31 02:00:00 GMT+09:00 1899 Sun Dec 31 05:00:00 GMT+09:00 1899 Rm 278: Grendel's Cave 043v80cs15naa3lssammfb568o@google.com] [Godzilla vs. YOU Thu May 16 16:00:00 GMT+09:00 2013 Sun Dec 31 02:00:00 GMT+09:00 1899 Sun Dec 31 05:00:00 GMT+09:00 1899 Rm 395: Baba Yaga's Hut br833s21u2k0vit0klrl8drnfk@google.com] [Leprechauns: monsters or otherkin? Thu May 16 16:00:00 GMT+09:00 2013 Sun Dec 31 06:00:00 GMT+09:00 1899 Sun Dec 31 09:00:00 GMT+09:00 1899 Rm 197: The Graveyard 5nore46qlofahi1mdhqkhpjfi0@google.com] [So the cryptozoologists are onto you… Thu May 16 16:00:00 GMT+09:00 2013 Sun Dec 31 06:00:00 GMT+09:00 1899 Sun Dec 31 09:00:00 GMT+09:00 1899 Rm 010: Frankenstein's Laboratory uvd6cfr4kqu1vch0dsfna9c998@google.com] ]
スクリプトにキーと値のペアを設定
ScriptProperties.setProperty(‘calId’, cal.getId())
キー: ‘calId ‘
値: カレンダーID
joinDateAndTime_()サンプルコード
パラメーターで指定された日付のdateオブジェクト生成し開始・終了時間をセット。
/**
* Creates a single Date object from separate date and time cells.
*
* @param {Date} date A Date object from which to extract the date.
* @param {Date} time A Date object from which to extract the time.
* @return {Date} A Date object representing the combined date and time.
*/
function joinDateAndTime_(date, time) {
date = new Date(date);
date.setHours(time.getHours());
date.setMinutes(time.getMinutes());
return date;
}
date = new Date(date) :指定日付オブジェクトを生成。
SetHours:時間のセット
setMinutes:分のセット
setHours setMinutes getHours getMinute メソッド参照。
JdbcTimeクラス 参照。

