Last active
April 27, 2016 13:06
-
-
Save yasu47b/fc62a41b40aeb7f872d12e1932c80c24 to your computer and use it in GitHub Desktop.
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
// 休日のjsonを返す | |
// this gas return json that all holidays in this month | |
// or reutrn specific range of holidays if keys esisted. eg http://<script_url>?start_date=2016-04-10&end_date=2016-05-11 | |
function doGet(e) { | |
var today = new Date(); | |
// キーの取得 | |
var param_start_date = e.parameter.start_date; | |
var param_end_date = e.parameter.end_date; | |
var output = {}; | |
if((param_start_date === undefined || param_end_date === undefined) && ! (param_start_date === undefined && param_end_date === undefined)){ | |
output = {result: [], status: false}; | |
} else if (param_start_date !== undefined && param_end_date !== undefined){ | |
var result1 = param_start_date.match(/(\d{4})-(\d{2})-(\d{2})/); | |
var result2 = param_end_date.match(/(\d{4})-(\d{2})-(\d{2})/); | |
var month_start_date = new Date(result1[1], result1[2]-1, result1[3]); | |
var month_end_date = new Date(result2[1], result2[2]-1, result2[3], 23,59,59); | |
var holiday_list = getHoliday(month_start_date,month_end_date); | |
output = {result:holiday_list, | |
params: { | |
start_date:param_start_date, end_date:param_end_date | |
}, | |
}; | |
} else { | |
// キーの未指定時は当月の休日を返却 | |
var month_start_date = new Date(today.getFullYear(),today.getMonth()); | |
var month_end_date = new Date(today.getFullYear(),today.getMonth()+1,0,23,59,59); | |
var holiday_list = getHoliday(month_start_date, month_end_date); | |
Logger.log(month_start_date); | |
Logger.log(month_end_date); | |
Logger.log(holiday_list); | |
output = {result:holiday_list, status: true}; | |
} | |
return ContentService.createTextOutput(JSON.stringify(output)) | |
.setMimeType(ContentService.MimeType.JSON); | |
} | |
function getHoliday(month_start_date, month_end_date){ | |
var holiday_list = []; | |
var events = CalendarApp.getCalendarById('ja.japanese#holiday@group.v.calendar.google.com').getEvents(month_start_date, month_end_date); | |
for(var i=0; i < events.length; i++){ | |
holiday_list[i] = { | |
name: events[i].getTitle(), | |
description:events[i]. | |
getDescription(), | |
starttime: events[i].getStartTime(), | |
endtime: events[i].getEndTime()}; | |
Logger.log('Loop'); | |
} | |
return holiday_list; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment