Last active
February 24, 2023 04:20
-
-
Save bvlion/69eb8900b9f4a8ecf0d85be87efc43ab to your computer and use it in GitHub Desktop.
GAS で土日祝日一覧を作成する
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
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
function doGet(e) { | |
if (e.parameter == undefined) { | |
throw new Error("固定パラメータなし"); | |
} | |
// 日付を取得 | |
var date = e.parameter.date; | |
// シートを取得(yyyyMMdd 形式で取得できる前提) | |
var sheet = spreadsheet.getSheetByName(date.substring(0, 6)); | |
var data = { | |
holiday: sheet.getRange(parseInt(date.substring(6)) + 1, 2).getValue() | |
} | |
return ContentService.createTextOutput(JSON.stringify(data)).setMimeType(ContentService.MimeType.JSON) | |
} | |
function insertNewSheet() { | |
var date = new Date(); | |
date.setMonth(date.getMonth() + 1); | |
var sheetName = Utilities.formatDate(date, 'Asia/Tokyo', 'yyyyMM'); | |
var sheet = spreadsheet.getSheetByName(sheetName); | |
// 保存対象シートがなければ新規作成 | |
if (sheet == null) { | |
spreadsheet.insertSheet(sheetName, 0); | |
var sheet = spreadsheet.getSheets()[0]; | |
sheet.insertRows(1); | |
// ヘッダ設定 | |
sheet.getRange("A1").setValue("日付"); | |
sheet.getRange("B1").setValue("休日"); | |
sheet.deleteColumns(3, 24); | |
sheet.setColumnWidth(1, 100); | |
sheet.setColumnWidth(2, 50); | |
// 月末日付を取得 | |
date.setMonth(date.getMonth() + 1); | |
date.setDate(0); | |
var endDate = new Date(date); | |
date.setDate(1); | |
// 対象月の祝日を取得 | |
var events = CalendarApp.getCalendarById('ja.japanese#[email protected]').getEvents(date, endDate); | |
// 日付分回す | |
for (var i = 1; i <= endDate.getDate(); i++) { | |
sheet.getRange("A" + (i + 1)).setValue(Utilities.formatDate(date, 'Asia/Tokyo', 'yyyy/MM/dd')); | |
sheet.getRange("B" + (i + 1)).setValue("0"); | |
// 土日設定 | |
if (date.getDay() == 0 || date.getDay() == 6) { | |
sheet.getRange("B" + (i + 1)).setValue("1"); | |
} | |
// 祝日判定 | |
events | |
.filter(event => Utilities.formatDate(event.getStartTime(), 'Asia/Tokyo', 'yyyyMMdd') == Utilities.formatDate(date, 'Asia/Tokyo', 'yyyyMMdd')) // もっとスマートな方法は絶対ある… | |
.forEach(_ => sheet.getRange("B" + (i + 1)).setValue("1")); | |
date.setDate(date.getDate() + 1); | |
} | |
} | |
} |
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
#!/bin/bash | |
SHEET_ID= | |
DATE= | |
REMO_TOKEN= | |
REMO_ID= | |
JSON=$(/usr/bin/curl -L "https://script.google.com/macros/s/$SHEET_ID/exec?date=$DATE") | |
HOLIDAY=$(echo $JSON | /usr/bin/jq '.holiday') | |
if [ $HOLIDAY -eq 0 ]; then | |
curl -H "accept: application/json" -H "Authorization: Bearer $REMO_TOKEN" -X POST "https://api.nature.global/1/signals/$REMO_ID/send" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment