|
| 1 | +const TARGET = { |
| 2 | + DATES: [10, 15], // 日付指定 |
| 3 | + NUMBER_OF_BUSINESS_DAYS: [6, 8], // 第 n 営業日 |
| 4 | + HOURS: 9, // トリガー実行時間(時) |
| 5 | + MINUTES: 00, // トリガー実行時間(分) |
| 6 | +}; |
| 7 | + |
| 8 | +// トリガーを設定可能か判定する |
| 9 | +// |
| 10 | +// @return {boolean} |
| 11 | +function canSetTrigger() { |
| 12 | + const today = new Date(); |
| 13 | + const todayDate = today.getDate(); |
| 14 | + if (TARGET.DATES.includes(todayDate)) { |
| 15 | + return true; |
| 16 | + } |
| 17 | + |
| 18 | + const todayNumberOfBusinessDay = numberOfBusinessDay(today); |
| 19 | + if (TARGET.NUMBER_OF_BUSINESS_DAYS.includes(todayNumberOfBusinessDay)) { |
| 20 | + return true; |
| 21 | + } |
| 22 | + |
| 23 | + return false; |
| 24 | +} |
| 25 | + |
| 26 | +// トリガーを設定する |
| 27 | +function setTrigger() { |
| 28 | + if (canSetTrigger()) { |
| 29 | + const date = new Date(); |
| 30 | + date.setHours(TARGET.HOURS); |
| 31 | + date.setMinutes(TARGET.MINUTES); |
| 32 | + |
| 33 | + ScriptApp.newTrigger("myFunction").timeBased().at(date).create(); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// トリガーを削除する |
| 38 | +function delTrigger() { |
| 39 | + const triggers = ScriptApp.getProjectTriggers(); |
| 40 | + for (const trigger of triggers) { |
| 41 | + if (trigger.getHandlerFunction() === "myFunction") { |
| 42 | + ScriptApp.deleteTrigger(trigger); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// 営業日を判定する |
| 48 | +// |
| 49 | +// @return {boolean} |
| 50 | +// * 土日 false |
| 51 | +// * 祝日 false |
| 52 | +// * それ以外 true |
| 53 | +function isBusinessDay(date) { |
| 54 | + if (date.getDay() == 0 || date.getDay() == 6) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + const calJa = CalendarApp.getCalendarById( |
| 59 | + |
| 60 | + ); |
| 61 | + if (calJa.getEventsForDay(date).length > 0) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + return true; |
| 66 | +} |
| 67 | + |
| 68 | +// 第 n 営業日の何日目か算出する |
| 69 | +// |
| 70 | +// @return {number} num |
| 71 | +// * 営業日では無い場合は 0 を返す |
| 72 | +// * 第 n 営業日の場合は n を返す |
| 73 | +function numberOfBusinessDay() { |
| 74 | + const now = new Date(); |
| 75 | + let num = 0; |
| 76 | + let countDayOfMonth; |
| 77 | + |
| 78 | + if (!isBusinessDay(now)) { |
| 79 | + return num; |
| 80 | + } |
| 81 | + |
| 82 | + for (let i = 1; i <= now.getDate(); i++) { |
| 83 | + countDayOfMonth = new Date(now.getFullYear(), now.getMonth(), i); // 次の日をセット |
| 84 | + if (isBusinessDay(countDayOfMonth)) { |
| 85 | + num = num + 1; // 営業日をカウントアップ |
| 86 | + } |
| 87 | + } |
| 88 | + return num; |
| 89 | +} |
| 90 | + |
| 91 | +function postSlackMessage(text) { |
| 92 | + const data = { |
| 93 | + text: text, |
| 94 | + }; |
| 95 | + const options = { |
| 96 | + method: "post", |
| 97 | + contentType: "application/json", |
| 98 | + payload: JSON.stringify(data), // Convert the JavaScript object to a JSON string. |
| 99 | + }; |
| 100 | + |
| 101 | + UrlFetchApp.fetch( |
| 102 | + PropertiesService.getScriptProperties().getProperties().SLACK_URL, |
| 103 | + options |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +function myFunction() { |
| 108 | + delTrigger(); |
| 109 | + postSlackMessage("オハヨウゴザイマス!"); |
| 110 | +} |
0 commit comments