こちらの動画のコード部分です。
Gmail からメール情報取得 (0:41~)
// 設定情報
function getConfig() {
return {
spreadSheetId: '',// スプレッドシートの ID
spreadSheetTabName: '',// スプレッドシートのタブ名
searchText: '' // Gmail の検索ワード
};
}
// 実行関数
function main() {
const config = getConfig();
const SEARCH_BOX = config.searchText; // before:取得したい日にちの次の日 after:取得したい日にち
/* スプレッドシートのシートを取得と準備 */
const mySheet = SpreadsheetApp.openById(config.spreadSheetId).getSheetByName(config.spreadSheetTabName); //シートを取得
let messages = [['Subject', 'From', 'To']];
GmailApp
.search(SEARCH_BOX, 0, 500)
.forEach(function (thread) {
thread.getMessages().forEach(function (message) {
let subject = message.getSubject();
let to = message.getTo();
let from = message.getFrom();
messages.push([subject, from, to]);
});
});
if (messages.length === 1) return;
mySheet.getRange('A1:C' + messages.length ).setValues(messages);
}
Gmail の使い方 (5:45~)
Gmail で使用できる検索演算子
今回の動画では、件名に含まれる単語を指定する「subject: 」と指定した期間に送信されたメールを検索する「before: 」と「after: 」を解説しています。
2営業日前のメールを取得 (7:52~)
// 設定情報
function getConfig() {
return {
spreadSheetId: '',// スプレッドシートの ID
spreadSheetTabName: '',// スプレッドシートのタブ名
searchText: '' // Gmail の検索ワード
};
}
// 土日祝判定
function isBusinessDay(date){
// 0が日曜日、6が土曜日
if(date.getDay() == 0 || date.getDay() == 6){
return false;
}
const calendar_ID = "ja.japanese#holiday@group.v.calendar.google.com";
const calendar_JA = CalendarApp.getCalendarById(calendar_ID);
const todayEvent = calendar_JA.getEventsForDay(date);
if(todayEvent.length > 0){
return false;
}
return true;
}
// 実行関数
function main() {
let date = new Date();
let business_flg = false;
// 今日が営業日なのか判定(営業日でなければ終了)
business_flg = isBusinessDay(date);
if(!business_flg) {
return;
}
business_flg = false;
// 1営業日前の日にちを探す
while(!business_flg){
date = new Date(date.getFullYear(), date.getMonth(), date.getDate()-1);
business_flg = isBusinessDay(date);
}
business_flg = false;
// 2営業日前の日にちを探す
while(!business_flg){
date = new Date(date.getFullYear(), date.getMonth(), date.getDate()-1);
business_flg = isBusinessDay(date);
}
business_flg = false;
// 特定の日にちを取得
let dateBefore = new Date(date.getFullYear(), date.getMonth(), date.getDate()+1);
let timeStampBefore = Utilities.formatDate(dateBefore, 'Asia/Tokyo', 'yyyy/MM/dd')
let timeStampAfter = Utilities.formatDate(date, 'Asia/Tokyo', 'yyyy/MM/dd')
const config = getConfig();
const SEARCH_BOX = `subject:${config.searchText} before:${timeStampBefore} after:${timeStampAfter} `; // before:取得したい日にちの次の日 after:取得したい日にち
/* スプレッドシートのシートを取得と準備 */
const mySheet = SpreadsheetApp.openById(config.spreadSheetId).getSheetByName(config.spreadSheetTabName); //シートを取得
let messages = [['Subject', 'From', 'To']];
GmailApp
.search(SEARCH_BOX, 0, 500)
.forEach(function (thread) {
thread.getMessages().forEach(function (message) {
let subject = message.getSubject();
let to = message.getTo();
let from = message.getFrom();
messages.push([subject, from, to]);
});
});
if (messages.length === 1) return;
mySheet.getRange('A1:C' + messages.length ).setValues(messages);
}
プログラミングが学べる体験会を実施中
テクノロジーを身につけて、社会問題を解決したい方はぜひ、TSfCMの体験説明会にお越しください!








【メタバース入門】Unity × Cluster ワールド作ろう
【1,000円 OFF優待!】
プログラミングで作るLINEチャットボット|初心者向け個別レッスン
【1,000円 OFF優待!】





