Created
June 13, 2025 01:53
-
-
Save imaatsu/1104b1e42c5daad29e5656a8e1503ceb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // === 設定項目:ご自身の環境に合わせて変更してください === | |
| // 何日前に通知を送るか(例: 30日前に通知) | |
| const REMIND_BEFORE_DAYS = 30; | |
| // リマインドメールの件名 | |
| const MAIL_SUBJECT = "【要対応】測定機器の校正時期が近づいています"; | |
| // 通知済みのステータスとしてセルに入力する文字列 | |
| const NOTIFIED_STATUS = "通知済"; | |
| // ======================================================= | |
| /** | |
| * メインの処理:校正期限をチェックしてメールを送信する | |
| */ | |
| function checkCalibrationDueDate() { | |
| // "シート1"は実際のシート名に合わせてください | |
| const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("シート1"); | |
| const today = new Date(); | |
| // スプレッドシートの全データを取得 (ヘッダー行を除く) | |
| const dataRange = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn()); | |
| const values = dataRange.getValues(); | |
| // 1行ずつデータをチェック | |
| for (let i = 0; i < values.length; i++) { | |
| const row = values[i]; | |
| const rowNum = i + 2; // スプレッドシート上の実際の行番号 | |
| // 各列のデータを取得 | |
| const equipmentName = row[1]; // B列: 機器名 | |
| const picEmail = row[4]; // E列: 担当者メールアドレス | |
| const nextDueDate = new Date(row[7]); // H列: 次回校正日 | |
| const notified = row[8]; // I列: 通知ステータス | |
| // 必須データが空欄、または日付が無効、または通知済みならスキップ | |
| if (!picEmail || !nextDueDate.valueOf() || notified === NOTIFIED_STATUS) { | |
| continue; | |
| } | |
| // 次回校正日と今日の日付の差を計算 | |
| const diffTime = nextDueDate.getTime() - today.getTime(); | |
| const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); | |
| // もし期限が設定日数以内になったらメールを送信 | |
| if (diffDays <= REMIND_BEFORE_DAYS && diffDays >= 0) { | |
| const equipmentId = row[0]; | |
| const formattedDueDate = `${nextDueDate.getFullYear()}/${nextDueDate.getMonth() + 1}/${nextDueDate.getDate()}`; | |
| // メール本文を作成 | |
| let mailBody = `${picEmail.split('@')[0]}様\n\n`; // メールアドレスのアットマーク前を名前にする簡易的な方法 | |
| mailBody += `お疲れ様です。\n`; | |
| mailBody += `ご担当の測定機器について、校正の実施時期が近づいています。\n\n`; | |
| mailBody += `-------------------------------------\n`; | |
| mailBody += `管理番号: ${equipmentId}\n`; | |
| mailBody += `機器名 : ${equipmentName}\n`; | |
| mailBody += `校正予定日: ${formattedDueDate}\n`; | |
| mailBody += `-------------------------------------\n\n`; | |
| mailBody += `※本メールはシステムから自動で送信されています。`; | |
| // メールを送信 | |
| GmailApp.sendEmail(picEmail, MAIL_SUBJECT, mailBody); | |
| // 通知ステータスを更新 | |
| sheet.getRange(rowNum, 9).setValue(NOTIFIED_STATUS); // I列に「通知済」と入力 | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment