Last active
April 17, 2018 03:21
-
-
Save noriyukitakei/388d98dfcbb2f7b77a90c2260056880f to your computer and use it in GitHub Desktop.
【アレクサ、〇〇さんの今日の予定を教えて】GoogleAppsScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function doGet(e) { | |
// クエリパラメーター「name」で指定された、Googleカレンダー取得対象のユーザーの名前を取得する。 | |
var name = e.parameter.name; | |
// ユーザーが発話した名前(「〇〇さんの今日の予定を教えて」の〇〇の部分)に紐づく | |
// メールアドレスを定義する。Googleカレンダーの予定を取得する際は、 | |
// そのキーにメールアドレスを指定しなければならないので、この変換が必要になる。 | |
var mail = {}; | |
mail['タケイ'] = 'ntakei@contoso.com'; | |
mail['スズキ'] = 'suzuki@contoso.com'; | |
mail['タカハシ'] = 'takahashi@contoso.com'; | |
// 指定したユーザーのGoogleカレンダーの予定を取得する。 | |
var cal = CalendarApp.getCalendarById(mail[name]); | |
// 今日の日付の00:00:00時点のDateを生成する | |
var startDate = new Date(); | |
startDate.setHours(0); | |
startDate.setMinutes(0); | |
startDate.setSeconds(0); | |
// 今日の日付の23:59:59時点のDateを生成する | |
var endDate=new Date(startDate); | |
endDate.setHours(23); | |
endDate.setMinutes(59); | |
endDate.setSeconds(59); | |
// 今日一日のGoogleカレンダーの予定を取得する | |
var myEvents=cal.getEvents(startDate,endDate); | |
// 今日一日のGoogleカレンダーのJSONを生成する。 | |
var body = ''; | |
num = 0; | |
var result = []; | |
for each(var evt in myEvents){ | |
result[num] = { | |
start: convertDate(evt.getStartTime()), | |
end: convertDate(evt.getEndTime()), | |
title: evt.getTitle() | |
} | |
num++; | |
} | |
return ContentService | |
.createTextOutput(JSON.stringify(result)) | |
.setMimeType(ContentService.MimeType.JSON); | |
} | |
function convertDate(date) { | |
// DateオブジェクトをYYYY-MM-DD hh:mm:ssの形式に変換する | |
var year = date.getFullYear(); | |
var month = toDoubleDigits(date.getMonth() + 1); | |
var day = toDoubleDigits(date.getDate()); | |
var hour = toDoubleDigits(date.getHours()); | |
var minute = toDoubleDigits(date.getMinutes()); | |
var second = toDoubleDigits(date.getSeconds()); | |
return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second; | |
} | |
function toDoubleDigits(num) { | |
num += ""; | |
if (num.length === 1) { | |
num = "0" + num; | |
} | |
return num; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment