{"id":3338,"date":"2021-03-22T14:13:37","date_gmt":"2021-03-22T05:13:37","guid":{"rendered":"https:\/\/taitan916.info\/blog\/?p=3338"},"modified":"2021-03-22T14:13:37","modified_gmt":"2021-03-22T05:13:37","slug":"post-3338","status":"publish","type":"post","link":"https:\/\/taitan916.info\/blog\/archives\/3338","title":{"rendered":"GAS\u3067\u795d\u65e5\u4e00\u89a7(\u5e74\u6708\u65e5\u3068\u795d\u65e5\u540d)\u30c7\u30fc\u30bf\u3092JSON\u51fa\u529b\u3059\u308bAPI\u3092\u4f5c\u6210"},"content":{"rendered":"<p>PHP\u306e\u30b7\u30b9\u30c6\u30e0\u6848\u4ef6\u3067\u795d\u65e5\u3092\u53d6\u5f97\u3057\u3001\u51e6\u7406\u3092\u884c\u3046\u7b87\u6240\u304c\u3042\u3063\u305f\u3002Google\u30ab\u30ec\u30f3\u30c0\u30fc\u306eAPI\u306b\u63a5\u7d9a\u3057\u3066\u3069\u3046\u3053\u3046\u3057\u3088\u3046\u304b\u3068\u8003\u3048\u305f\u304c\u3001\u30ab\u30ec\u30f3\u30c0\u30fcAPI\u3092\u4f7f\u3063\u305f\u3053\u3068\u304c\u7121\u3044\u306e\u3068\u3001GoogleMapsAPI\u307f\u305f\u3044\u306bAPI\u30ad\u30fc\u304c\u5fc5\u9808\u3060\u3063\u305f\u5834\u5408\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u69d8\u306b\u30ad\u30fc\u3092\u767a\u884c\u3044\u305f\u3060\u304f\u5fc5\u8981\u304c\u3042\u308a\u305d\u306e\u8fba\u308a\u304c\u9762\u5012\u3060\u3063\u305f\u3002\u7d50\u5c40\u306e\u3068\u3053\u308d<a href=\"https:\/\/holidays-jp.github.io\/\" target=\"_blank\" rel=\"noopener\">\u4ed6\u6240\u69d8\u306eAPI<\/a>\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u88c5\u3057\u305f\u304c\u3001\u3088\u304f\u3088\u304f\u8003\u3048\u308c\u3070\u3053\u3046\u3044\u3063\u305f\u6642\u3053\u305dGoogle Apps Script\u306e\u51fa\u756a\u3058\u3083\u306a\u3044\u304b\u3068\u601d\u3063\u305f\u3002\u4ee5\u4e0b\u306b\u795d\u65e5API\u306e\u4f5c\u6210\u65b9\u6cd5\u3092\u30e1\u30e2\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u3084\u308a\u305f\u3044\u3053\u3068<\/h2>\n<p>\u4eca\u5e74\u3068\u6765\u5e74\u306e\u795d\u65e5\u4e00\u89a7(\u5e74\u6708\u65e5\u3068\u795d\u65e5\u540d)\u3092JSON\u51fa\u529b\u3059\u308bAPI\u3092\u4f5c\u6210\u3057\u305f\u3044\u3002<\/p>\n<p>API\u30ea\u30af\u30a8\u30b9\u30c8\u6642\u306bGoogle\u30ed\u30b0\u30a4\u30f3\u8a8d\u8a3c\u306f\u4e0d\u8981\u3060\u304c\u3001GoogleMapsAPI\u306eAPI\u30ad\u30fc\u306e\u3088\u3046\u306bGET\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u3064\u3051\u308b\u3053\u3068\u3067\u8a8d\u8a3c\u3055\u305b\u305f\u3044\u3002\u3082\u3061\u308d\u3093\u30ad\u30fc\u304c\u7121\u3044\u5834\u5408\u306f\u30c7\u30fc\u30bf\u3092\u8fd4\u3055\u306a\u3044\u3002<\/p>\n<p>\u6cd5\u6539\u6b63\u7b49\u306b\u3088\u308b\u795d\u65e5\u306e\u5909\u66f4\u3092\u8003\u616e\u3057\u30011\u65e5\u306b1\u56deGoogle\u30ab\u30ec\u30f3\u30c0\u30fc\u304b\u3089\u795d\u65e5\u30c7\u30fc\u30bf\u3092\u5f15\u3063\u5f35\u3063\u3066\u304d\u3066\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306b\u4fdd\u5b58\u30fb\u66f4\u65b0\u3057\u305f\u3044\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>GAS\u5074\u306e\u4f5c\u696d<\/h2>\n<p>\u5fc5\u8981\u306a\u4f5c\u696d\u306f\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4f5c\u6210\u3001\u30c8\u30ea\u30ac\u30fc\u306e\u8a2d\u5b9a\u3001\u30c7\u30d7\u30ed\u30a4\u306b\u306a\u308b\u3002<\/p>\n<h3>\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4f5c\u6210<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"default\" data-enlighter-title=\"\">\/\/\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306b\u795d\u65e5\u30c7\u30fc\u30bf\u3092\u66f8\u304d\u8fbc\u3080\u95a2\u6570\r\nfunction setHoliday(){\r\n\r\n\t\/\/\u958b\u59cb\u5e74\u6708\u65e5\u3068\u7d42\u4e86\u5e74\u6708\u65e5\u306e\u6307\u5b9a\r\n\tconst now = new Date();\r\n\tconst start = now.getFullYear() + &#039;-01-01&#039;;\r\n\tconst end = (now.getFullYear() + 1) + &#039;-12-31&#039;;\r\n\r\n\t\/\/Google\u30ab\u30ec\u30f3\u30c0\u30fc\u304b\u3089\u795d\u65e5\u53d6\u5f97\r\n\tconst calendar = CalendarApp.getCalendarById(&#039;ja.japanese#holiday@group.v.calendar.google.com&#039;).getEvents(new Date(start), new Date(end));\r\n\r\n\t\/\/\u795d\u65e5\u30c7\u30fc\u30bf\u3092\u6574\u5f62\u3057\u3066\u914d\u5217\u306b\u683c\u7d0d\r\n\tconst result = [];\r\n\tfor( var i = 0; i &lt; calendar.length; i++ ){\r\n\t\tresult[i] = [\r\n\t\t\tUtilities.formatDate(calendar[i].getStartTime(), &#039;JST&#039;, &#039;yyyy-MM-dd&#039;), \r\n\t\t\tcalendar[i].getTitle()\r\n\t\t];\r\n\t}\r\n\r\n\t\/\/\u6b63\u5e38\u306b\u30c7\u30fc\u30bf\u304c\u53d6\u5f97\u3067\u304d\u305f\u5834\u5408\u3001\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306b\u66f8\u304d\u8fbc\u307f\r\n\tif( result.length ){\r\n\r\n\t\t\/\/\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306e\u8aad\u307f\u8fbc\u307f\r\n\t\tconst sheet = SpreadsheetApp.getActiveSpreadsheet();\r\n\r\n\t\t\/\/\u30b7\u30fc\u30c8\u3092\u6307\u5b9a\r\n\t\tconst holidaySheet = sheet.getSheetByName(&#039;holiday&#039;);\r\n\r\n\t\t\/\/\u30b7\u30fc\u30c8\u306b\u307e\u3068\u3081\u3066\u66f8\u304d\u8fbc\u307f\r\n\t\tholidaySheet.getRange(1, 1, result.length, result[0].length).setValues(result);\r\n\t}\r\n}\r\n\r\n\/\/\u795d\u65e5\u30c7\u30fc\u30bf\u3092JSON\u51fa\u529b\r\nfunction getHoliday(){\r\n\r\n\t\/\/\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306e\u8aad\u307f\u8fbc\u307f\r\n\tconst sheet = SpreadsheetApp.getActiveSpreadsheet();\r\n\r\n\t\/\/\u30b7\u30fc\u30c8\u3092\u6307\u5b9a\r\n\tconst holidaySheet = sheet.getSheetByName(&#039;holiday&#039;);\r\n\r\n\t\/\/\u30b7\u30fc\u30c8\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\r\n\tconst holidaySheetVal = holidaySheet.getDataRange().getValues();\r\n\r\n\t\/\/\u5e74\u6708\u65e5\u306e\u6574\u5f62\r\n\tfor( let i = 0; i &lt; holidaySheetVal.length; i++ ){\r\n\t\tholidaySheetVal[i][0] = Utilities.formatDate(holidaySheetVal[i][0], &#039;JST&#039;, &#039;yyyy-MM-dd&#039;);\r\n\t}\r\n\r\n\t\/\/\u30c7\u30fc\u30bf\u3092JSON\u5316\r\n\tconst payload = JSON.stringify(holidaySheetVal);\r\n\r\n\t\/\/Textoutput\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u751f\u6210\r\n\tconst output = ContentService.createTextOutput();\r\n\r\n\t\/\/Mime Type\u3092JSON\u306b\u8a2d\u5b9a\r\n\toutput.setMimeType(ContentService.MimeType.JSON);\r\n\r\n\t\/\/\u30c7\u30fc\u30bf\u51fa\u529b\r\n\toutput.setContent(payload);\r\n\r\n\treturn output;\r\n}\r\n\r\n\/\/API\u547c\u3073\u51fa\u3057\u6642\u306b\u52d5\u4f5c\u3059\u308b\u95a2\u6570\r\nfunction doGet(e){\r\n\tconst key = e.parameter.key;\r\n\tconst apiKey = &#039;xxxxxxxxxxxxxxxx&#039;;\r\n\r\n\tif( key != apiKey ){\r\n\t\treturn ContentService.createTextOutput(&#039;\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002&#039;);\r\n\t}\r\n\r\n\treturn getHoliday();\r\n}<\/pre>\n<h4>\u30ab\u30ec\u30f3\u30c0\u30fc\u30c7\u30fc\u30bf\u53d6\u5f97\u90e8\u5206\u306e\u30a8\u30e9\u30fc<\/h4>\n<p>\u300cCalendarApp.getCalendarById('ja.japanese#holiday@group.v.calendar.google.com')\u300d\u306e\u90e8\u5206\u304c\u52d5\u304b\u305a\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u304c\u8868\u793a\u3055\u308c\u305f\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"default\" data-enlighter-title=\"\">TypeError: Cannot find function getEvents in object<\/pre>\n<p>GAS\u3067\u4f7f\u3046Google\u30a2\u30ab\u30a6\u30f3\u30c8\u304c\u6700\u8fd1\u53d6\u5f97\u3057\u305f\u3070\u304b\u308a\u3067Google\u30ab\u30ec\u30f3\u30c0\u30fc\u3092\u4f5c\u6210\u3057\u3066\u3044\u306a\u304b\u3063\u305f\u3002Google\u30ab\u30ec\u30f3\u30c0\u30fc\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u3067\u4e0a\u8a18\u30a8\u30e9\u30fc\u304c\u6539\u5584\u3055\u308c\u305f\u3002<\/p>\n<p>\u4ed6\u306b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5834\u5408\u3082\u3042\u308b\u6a21\u69d8\u3002<\/p>\n<p><a href=\"https:\/\/teratail.com\/questions\/189134\" target=\"_blank\" rel=\"noopener\">https:\/\/teratail.com\/questions\/189134<\/a><\/p>\n<p><a href=\"http:\/\/syasuda.com\/d\/blog\/2016\/11\/googleid.html\" target=\"_blank\" rel=\"noopener\">http:\/\/syasuda.com\/d\/blog\/2016\/11\/googleid.html<\/a><\/p>\n<h4>API\u547c\u3073\u51fa\u3057\u6642<\/h4>\n<p>\u300cconst apiKey = 'xxxxxxxxxxxxxxxx';\u300d\u306e\u90e8\u5206\u306f\u9069\u5b9c\u5909\u66f4\u3059\u308b\u3002<\/p>\n<h4>\u5e74\u6708\u65e5\u306e\u30d5\u30a9\u30fc\u30de\u30c3\u30c8<\/h4>\n<p>\u300cUtilities.formatDate(\u5e74\u6708\u65e5\u30c7\u30fc\u30bf, 'JST', 'yyyy-MM-dd')\u300d\u3067\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u3059\u308b\u3002\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u6307\u5b9a\u6587\u5b57\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3002<\/p>\n<p><a href=\"https:\/\/sites.google.com\/site\/gasgaidobukku\/utilities\/formatdate\" target=\"_blank\" rel=\"noopener\">https:\/\/sites.google.com\/site\/gasgaidobukku\/utilities\/formatdate<\/a><\/p>\n<h4>API\u30ea\u30af\u30a8\u30b9\u30c8\u6642\u306eGET\u30d1\u30e9\u30e1\u30fc\u30bf\u53d6\u5f97<\/h4>\n<p>API\u30ea\u30af\u30a8\u30b9\u30c8\u6642\u306fdoGet\u95a2\u6570\u304c\u52d5\u4f5c\u3059\u308b\u3002GET\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u306f\u300ce.parameter.\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u30ad\u30fc\u300d\u3067\u53d6\u5f97\u3067\u304d\u308b\u3002\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3002<\/p>\n<p><a href=\"https:\/\/developers.google.com\/apps-script\/guides\/web\" target=\"_blank\" rel=\"noopener\">https:\/\/developers.google.com\/apps-script\/guides\/web<\/a><\/p>\n<h3>\u30c8\u30ea\u30ac\u30fc\u306e\u8a2d\u5b9a<\/h3>\n<p>setHoliday\u95a2\u6570\u304c1\u65e51\u56de\u52d5\u4f5c\u3059\u308b\u3088\u3046\u30c8\u30ea\u30ac\u30fc\u3092\u8a2d\u5b9a\u3057\u3066\u304a\u304f\u3002<\/p>\n<h3>\u30c7\u30d7\u30ed\u30a4<\/h3>\n<p>\u300c\u53f3\u4e0a\u306e\u30c7\u30d7\u30ed\u30a4\u30dc\u30bf\u30f3\u2192\u65b0\u3057\u3044\u30c7\u30d7\u30ed\u30a4\u300d\u306b\u9032\u307f\u300c\u8aac\u660e\u300d\u306f\u9069\u5f53\u3001\u300c\u7a2e\u985e\u306e\u9078\u629e\u300d\u306f\u30a6\u30a7\u30d6\u30a2\u30d7\u30ea\u3001\u300c\u6b21\u306e\u30e6\u30fc\u30b6\u30fc\u3068\u3057\u3066\u306e\u5b9f\u884c\u300d\u3092\u81ea\u5206\u3001\u300c\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u30e6\u30fc\u30b6\u30fc\u300d\u3092\u5168\u54e1\u3067\u8a2d\u5b9a\u3059\u308b\u3002\u4f55\u6545\u304b\u521d\u56de\u30c7\u30d7\u30ed\u30a4\u6642\u306f\u300c\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u30e6\u30fc\u30b6\u30fc\u300d\u306b\u5168\u54e1\u306e\u9078\u629e\u80a2\u304c\u7121\u304b\u3063\u305f\u3002\u300c\u30c7\u30d7\u30ed\u30a4\u3092\u7ba1\u7406\u2192\u30a2\u30fc\u30ab\u30a4\u30d6\u300d\u3067\u524a\u9664\u3059\u308b\u30682\u56de\u76ee\u4ee5\u964d\u304b\u3089\u306f\u5168\u54e1\u3092\u9078\u629e\u3067\u304d\u305f\u3002<\/p>\n<p>\u30c7\u30d7\u30ed\u30a4\u304c\u5b8c\u4e86\u3059\u308b\u3068URL\u304c\u767a\u884c\u3055\u308c\u308b\u306e\u3067\u30d6\u30e9\u30a6\u30b6\u304b\u3089\u958b\u3044\u3066\u307f\u308b\u3068\u6b63\u5e38\u306b\u52d5\u4f5c\u3057\u305f\u3002\u5c1a\u3001API\u30ad\u30fc\u3092GET\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066\u4ed8\u4e0e\u3059\u308b\u306e\u3092\u5fd8\u308c\u306a\u3044\u3088\u3046\u6ce8\u610f\u3059\u308b\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>API\u5229\u7528\u6642\u306e\u6ce8\u610f<\/h2>\n<p>GAS\u306e\u5229\u7528\u5236\u9650\u3084\u4e0a\u9650\u306b\u3072\u3063\u304b\u304b\u3089\u306a\u3044\u3088\u3046\u3001API\u5229\u7528\u6642\u306f\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u5f8c\u306b\u30d5\u30a1\u30a4\u30eb\u5316\u3059\u308b\u306e\u304c\u826f\u3055\u305d\u3046\u3002\u30c7\u30fc\u30bf\u3068\u306f\u5225\u306b\u30d5\u30a1\u30a4\u30eb\u306e\u6709\u52b9\u671f\u9650\u3092\u8a2d\u5b9a\u3057\u3066\u304a\u304d\u3001\u6709\u52b9\u671f\u9650\u304c\u5207\u308c\u3066\u3044\u308c\u3070\u518d\u5ea6API\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u3088\u3046\u306a\u5f62\u304c\u671b\u307e\u3057\u3044\u304b\u3068\u601d\u308f\u308c\u308b\u3002\u3056\u3063\u304f\u308a\u3068\u3060\u3051\u3069\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5f62\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"default\" data-enlighter-title=\"\">\/\/\u795d\u65e5API\u306b\u30ea\u30af\u30a8\u30b9\u30c8\r\n$api = file_get_contents(&#039;http:\/\/api.com\/&#039;);\r\n$holiday = json_decode($api, true);\r\n\r\n\/\/JSON\u30d5\u30a1\u30a4\u30eb\u7528\u306e\u5236\u9650\u6642\u9593\r\n$holiday[&#039;limit&#039;] = date(&#039;Y-m-d H:i:s&#039;, strtotime(&#039;+1 day&#039;));\r\n\r\n\/\/JSON\u30d5\u30a1\u30a4\u30eb\u4e0a\u66f8\u304d\u4fdd\u5b58\r\nfile_put_contents(&#039;.\/holiday.json&#039;, json_encode($holiday));<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u6240\u611f<\/h2>\n<p>GAS\u306f\u30a4\u30de\u30a4\u30c1\u696d\u52d9\u3067\u306e\u4f7f\u3044\u3069\u3053\u308d\u304c\u601d\u3044\u3064\u304b\u306a\u3044\u306a\u3041\u3001\u3068\u304b\u8003\u3048\u306a\u304c\u3089\u52c9\u5f37\u3057\u3066\u3044\u305f\u304c\u3084\u3063\u3068\u5f79\u306b\u7acb\u3061\u305d\u3046\u306a\u30b1\u30fc\u30b9\u304c\u51fa\u3066\u304d\u3066\u5b09\u3057\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u306e\u30b7\u30b9\u30c6\u30e0\u6848\u4ef6\u3067\u795d\u65e5\u3092\u53d6\u5f97\u3057\u3001\u51e6\u7406\u3092\u884c\u3046\u7b87\u6240\u304c\u3042\u3063\u305f\u3002Google\u30ab\u30ec\u30f3\u30c0 ... <\/p>\n","protected":false},"author":1,"featured_media":3294,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[72,70],"tags":[],"class_list":["post-3338","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-google-apps-script","category-google"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/3338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/comments?post=3338"}],"version-history":[{"count":0,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/3338\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/media\/3294"}],"wp:attachment":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/media?parent=3338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/categories?post=3338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/tags?post=3338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}