下記のコードをネットか探し、少し変更したのですが、エラーがでていまい、実行できずに困っております。
エラーは下記です。
SyntaxError: Identifier 'GMAIL_SEARCH_CONDITION' has already been declared
(匿名) @ コード のコピー.gs:1
どこを修正すればいいかご教示頂けましたら幸いです。
ーーーーー
let GMAIL_SEARCH_CONDITION = 'is:unread';
// Chatwork
const FORWARD_SERVICE_CHATWORK = 1; // 転送する:1、転送しない:0
//----------------------------------------
// Chatworkへの転送に必要な情報(転送する場合は設定必須)
//----------------------------------------
// API Token
const CHATWORK_TOKEN = 'ed10abf4fff95fb1efe41d7a9dab2a1f';
// 転送先のルームID
const CHATWORK_ROOM_ID = 140732331;
//==================================================
// トリガーとして呼び出される関数
//==================================================
function trigger() {
// Gmailの検索条件に is:unread を含んでいない場合は追加する
if (GMAIL_SEARCH_CONDITION.indexOf('is:unread') == -1) {
GMAIL_SEARCH_CONDITION += ' is:unread';
}
// Gmailの検索条件に after: を含んでいない場合は追加する
if (GMAIL_SEARCH_CONDITION.indexOf('after:') == -1) {
let yesterday = new Date(); // 現在日時
yesterday.setDate(yesterday.getDate() - 1); // 昨日にする
// 昨日受信したメール〜現在受信済みのメールのみを対象とする
GMAIL_SEARCH_CONDITION += ' after:' + Utilities.formatDate(yesterday, "JST", "yyyy/MM/dd");
}
// Gmailから特定条件のスレッドを検索しメールを取得
const myThreads = GmailApp.search(GMAIL_SEARCH_CONDITION, 0, 500); // 条件にマッチしたスレッドを取得
let myMsgs = GmailApp.getMessagesForThreads(myThreads); // スレッドからメールを取得
// 各スレッド×メール
for (let i = myMsgs.length - 1; i >= 0; i--) {
let msgsInThread = myMsgs[i];
for (let j = 0; j < msgsInThread.length; j++) {
let msg = msgsInThread[j];
// 既読の場合はskip if (!msg.isUnread()) { continue; } // 受信日時を取得 let dateFormatted = Utilities.formatDate(msg.getDate(), "JST", "yyyy/MM/dd HH:mm"); // 形式を変換 // メッセージデータ let messageData = { 'subject': msg.getSubject(), // 件名 'body': msg.getPlainBody(), // 本文 'from': msg.getFrom(), // From 'to': msg.getTo(), // To 'cc': msg.getCc(), // Cc 'date': dateFormatted, // 日付 } // Chatworkに転送する場合 if (FORWARD_SERVICE_CHATWORK == 1) { _send2ChatWork(messageData); } //メールを既読にする msg.markRead();
}
}
}
//==================================================
// 本スクリプト内で使用する関数
//==================================================
/**
- Chatworkにメッセージ送信
- @param {Object} messageData メッセージデータ
- @return {string} response 送信結果のレスポンス
*/
function _send2ChatWork(messageData) {
const url = 'https://api.chatwork.com/v2/rooms/' + CHATWORK_ROOM_ID + '/messages';
// 送信するメッセージ
const message = "[info][title]GASからのメール転送[/title]" +
"■件名:" + messageData.subject + "\n[hr]\n" +
"■From:" + messageData.from + "\n[hr]\n" +
"■To:" + messageData.to + "\n[hr]\n" +
"■Cc:" + messageData.cc + "\n[hr]\n" +
"■日付:" + messageData.date + "\n[hr]\n" +
"■本文:\n" + messageData.body.slice(0, 2500) + "[/info]" ;
const payload = {
'body': message,
'self_unread' : '0'
};
const options = {
'method': 'POST',
'headers': {
'Content-Type': 'application/x-www-form-urlencoded',
'X-ChatWorkToken': CHATWORK_TOKEN
},
'payload': payload
};
const response = UrlFetchApp.fetch(url, options);
return response;
}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/03/21 12:01