Gmailの受信日を会計処理に活用!GmailMessage.getDateで経費精算を効率化
Gmailで受信した請求書や領収書のメールから、受信日を自動的に取得して経費精算に役立てる方法をご紹介します。GmailMessage.getDate()を使うことで、手作業での転記ミスをなくし、会計処理を大幅に効率化できます。
GmailMessage.getDate()とは
GmailMessage.getDate()
は、Google Apps Script (GAS) でGmailのメッセージオブジェクトから受信日時を取得するための関数です。取得できる値はDateオブジェクトで、様々な形式に変換して利用できます。
getDate()の基本
GmailMessageオブジェクトに対してgetDate()
メソッドを使用することで、メールの受信日時を表すDateオブジェクトを取得できます。
function exampleGetDate() {
const threads = GmailApp.search('件名:請求書');
if (threads.length > 0) {
const messages = threads[0].getMessages();
const firstMessage = messages[0];
const receivedDate = firstMessage.getDate();
Logger.log(receivedDate); // ログに受信日時が出力される
}
}
経費精算におけるGmailMessage.getDate()の活用
経費精算では、領収書や請求書の日付を手入力することが多く、ミスが発生しやすいです。GmailMessage.getDate()を使えば、Gmailで受信したこれらのメールから自動的に日付を取得し、スプレッドシートなどに転記できます。
実装例1:受信日をスプレッドシートに自動入力
Gmailで受信した請求書のメールから、受信日をスプレッドシートに自動で入力するスクリプトです。
function recordInvoiceDates() {
// 検索クエリを設定 (例: ラベルが「請求書」のメール)
const threads = GmailApp.search('label:請求書');
// スプレッドシートを開く
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('請求書一覧');
// ヘッダー行を定義
sheet.appendRow(['メール件名', '受信日']);
// 各スレッド(メールのまとまり)を処理
threads.forEach(thread => {
const messages = thread.getMessages();
messages.forEach(message => {
const subject = message.getSubject();
const receivedDate = message.getDate();
// Dateオブジェクトを文字列にフォーマット
const formattedDate = Utilities.formatDate(receivedDate, 'JST', 'yyyy/MM/dd HH:mm:ss');
// スプレッドシートに行を追加
sheet.appendRow([subject, formattedDate]);
});
});
Logger.log('請求書の日付を記録しました。');
}
実装例2:特定の期間のメールを検索して日付を取得
特定の期間(例:先月)に受信したメールを検索し、その日付を取得するスクリプトです。
function getLastMonthInvoiceDates() {
const today = new Date();
const lastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
const lastDayOfLastMonth = new Date(today.getFullYear(), today.getMonth(), 0);
const startDate = Utilities.formatDate(lastMonth, 'JST', 'yyyy/MM/dd');
const endDate = Utilities.formatDate(lastDayOfLastMonth, 'JST', 'yyyy/MM/dd');
// 検索クエリを設定
const searchQuery = 'label:請求書 after:' + startDate + ' before:' + endDate;
const threads = GmailApp.search(searchQuery);
// スプレッドシートを開く
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('先月請求書');
// ヘッダー行を定義
sheet.appendRow(['メール件名', '受信日']);
// 各スレッド(メールのまとまり)を処理
threads.forEach(thread => {
const messages = thread.getMessages();
messages.forEach(message => {
const subject = message.getSubject();
const receivedDate = message.getDate();
// Dateオブジェクトを文字列にフォーマット
const formattedDate = Utilities.formatDate(receivedDate, 'JST', 'yyyy/MM/dd HH:mm:ss');
// スプレッドシートに行を追加
sheet.appendRow([subject, formattedDate]);
});
});
Logger.log('先月の請求書の日付を記録しました。');
}
実装例3:特定の送信者からのメールの日付を取得
特定の送信者(例:取引先)からのメールを検索し、その日付を取得するスクリプトです。
function getSpecificSenderInvoiceDates() {
const senderEmail = 'invoice@example.com'; // 送信者のメールアドレス
// 検索クエリを設定
const searchQuery = 'from:' + senderEmail + ' subject:請求書';
const threads = GmailApp.search(searchQuery);
// スプレッドシートを開く
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('取引先請求書');
// ヘッダー行を定義
sheet.appendRow(['メール件名', '受信日']);
// 各スレッド(メールのまとまり)を処理
threads.forEach(thread => {
const messages = thread.getMessages();
messages.forEach(message => {
const subject = message.getSubject();
const receivedDate = message.getDate();
// Dateオブジェクトを文字列にフォーマット
const formattedDate = Utilities.formatDate(receivedDate, 'JST', 'yyyy/MM/dd HH:mm:ss');
// スプレッドシートに行を追加
sheet.appendRow([subject, formattedDate]);
});
});
Logger.log('特定の送信者からの請求書の日付を記録しました。');
}
よくある問題とトラブルシューティング
- 日付が正しく取得できない:Gmailのタイムゾーン設定とスクリプトのタイムゾーン設定が一致しているか確認してください。
Utilities.formatDate()
でタイムゾーンを指定することで問題を解決できます。 - スクリプトの実行に時間がかかる:Gmailの検索クエリを最適化し、処理対象のメールを絞り込むことで、実行時間を短縮できます。
- 権限エラー:スクリプト実行時に必要なGmail APIの権限が付与されているか確認してください。
カスタマイズ方法と応用例
- 日付の形式を変更する:
Utilities.formatDate()
で日付の表示形式を自由にカスタマイズできます。 - 他の情報と組み合わせて記録する:メールの送信者、件名、本文などの情報を組み合わせてスプレッドシートに記録できます。
- 定期的に自動実行する:トリガーを設定することで、スクリプトを定期的に自動実行できます。
まとめ
GmailMessage.getDate()を活用することで、Gmailの受信日を効率的に取得し、経費精算などの会計処理を自動化できます。この記事で紹介したコード例を参考に、ぜひ業務効率化に役立ててください。