前提
GAS超初心者です。
GASを使用し、メール送信ツールを作成していますが、行き詰っています。
どなたかアドバイスをいただけますと大変幸いです。
よろしくお願いいたします。
実現したいこと
チェックボックスにチェックがついた宛先のデータのみ取得し、
メールの下書き作成と送信を行いたい。
発生している問題・エラーメッセージ
エラーメッセージは出ていませんが、チェックのついたデータのみを取得できません。
追記
- 参考サイトと異なる部分は宛先シートのセルAにチェックボックスを設けています。
- チェックボックスを押した時点でのメール送信ではなく、チェックが入っているデータを取得→差し込み→送信もしくは下書き作成(ボタン形式)です。
該当のソースコード
/*Step1:グローバル定数 -------------------------------------------------------------*/ const ss = SpreadsheetApp.getActiveSpreadsheet(); const mainSheet = ss.getSheetByName("Main"); /*Step2:シートオープン時にプルダウンの選択肢をセットする -------------------------------------------------------------*/ function onOpen() { setRecipientListPulldown(); } /*Step3:送信/下書きボタン押下時の処理|onClickCreateDrafts -------------------------------------------------------------*/ function onClickSendEmails() { const answer = Browser.msgBox("メールを一括送信します。よろしいですか。", Browser.Buttons.OK_CANCEL); if (answer === "ok") { batchProcessEmails("sendEmail"); Browser.msgBox("処理が完了しました。「処理結果」列を確認してください。"); } } function onClickCreateDrafts() { const answer = Browser.msgBox("メールの下書きを一括作成します。よろしいですか。", Browser.Buttons.OK_CANCEL); if (answer === "ok") { batchProcessEmails("createDraft"); Browser.msgBox("処理が完了しました。「処理結果」列を確認してください。"); } } /*Step4:宛先シート全行分のメールを処理する|batchProcessEmails -------------------------------------------------------------*/ function batchProcessEmails(action) { // Mainシートの入力値を取得 const inputValues = getInputValues(); // ログ列(R列)をクリア ss.getSheetByName(inputValues.recipientListName).getRange("R2:R").clear(); // 宛先シートの入力内容を取得 const targets = getTargets(inputValues.recipientListName); // 宛先シートの全行に対して送信(または下書き)を実行 const logs = []; for (const target of targets) { const result = processEmail(inputValues, target, action); logs.push([result]); } // ログ列(Q列)に結果を貼り付け ss.getSheetByName(inputValues.recipientListName) .getRange(`R2:R${logs.length + 1}`) .setValues(logs); } /*Step5:メールを1件送信または下書き作成する|processEmail -------------------------------------------------------------*/ function processEmail(inputValues, target, action) { try { // タイトル・本文に差込を適用 const replacedMailTitle = getReplacedString(inputValues.mailTitle, target.replaceList); const replacedMailBody = getReplacedString(inputValues.mailBody, target.replaceList); // 添付ファイルを取得 const attachements = [ ...getAttachements(inputValues.attachmentFolderId, inputValues.commonAttachmentNames), ...getAttachements(inputValues.attachmentFolderId, target.eachAttachmentNames) ] /* メールオプションの設定 -------------------------------------------------------------*/ const options = {}; if (inputValues.senderEmail) options.from = inputValues.senderEmail; if (inputValues.senderName) options.name = inputValues.senderName; if (target.cc) options.cc = target.cc; if (target.bcc) options.bcc = target.bcc; if (attachements.length) options.attachments = attachements; /* メール送信 or 下書き作成 -------------------------------------------------------------*/ if (action === "sendEmail") { GmailApp.sendEmail(target.to, replacedMailTitle, replacedMailBody, options); } else { GmailApp.createDraft(target.to, replacedMailTitle, replacedMailBody, options); } return "Success"; } catch(error) { return error; } } /*Step6:文字列, 差込リストから差込後のテキストを取得する|getReplacedString -------------------------------------------------------------*/ function getReplacedString(string, replaceList) { let replacedString = string; for (const item of replaceList) { const before = new RegExp(`\{\{ *${item.before} *\}\}`, "g"); replacedString = replacedString.replace(before, item.after); } return replacedString; } //★:チェックボックスの判定 function getCheckbox() { const myCell = mainSheet.getActiveCell(); const rule = myCell.getDataValidation(); if (rule != null) { const criteria = rule.getCriteriaType(); const status = myCell.getValue() if ( criteria == 'CHECKBOX' && status == true) { const row = myCell.getRow(); getInputValues(); } } } /*Step7:mainシートに入力された値を取得する|getInputValues -------------------------------------------------------------*/ function getInputValues() { const inputValues = { attachmentFolderId: mainSheet.getRange("B2").getValue(), // 添付ファイル格納フォルダID mailTitle: mainSheet.getRange("B5").getValue(), // メールタイトル mailBody: mainSheet.getRange("B6").getValue(), // 本文 commonAttachmentNames: [ mainSheet.getRange("B7").getValue(), // 共通添付ファイル名1 mainSheet.getRange("B8").getValue(), // 共通添付ファイル名2 mainSheet.getRange("B9").getValue(), // 共通添付ファイル名3 ], recipientListName: mainSheet.getRange("B12").getValue(), // 宛先リストシート名 } return inputValues; } /*Step8:宛先リストシート名から、シートの内容を配列で取得する -------------------------------------------------------------*/ function getTargets(recipientListName) { const targetsSheet = ss.getSheetByName(recipientListName); const sheetData = targetsSheet.getDataRange().getValues(); const header = sheetData[0]; const contents = sheetData.slice(1); const targets = contents.map((row) => { return { checkbox: [0], // チェックボックス to: row[1], // 宛先メールアドレス cc: row[2], // ccアドレス bcc: row[3], // bccアドレス replaceList: getReplaceList(header, row), // 置換内容 eachAttachmentNames: [ row[14], // 個別添付ファイル名1 row[15], // 個別添付ファイル名2 row[16], // 個別添付ファイル名3 ] }; }); return targets; } /*Step8:ヘッダー行・値行から、置換前・置換後を格納したオブジェクトの配列を返す -------------------------------------------------------------*/ function getReplaceList(header, row) { const beforeStrings = header.slice(4, 14); // 4 - 12列目が差込項目 const afterStrings = row.slice(4, 14); // 4 - 12列目が差込項目 const replaceList = []; beforeStrings.forEach((beforeString, index) => { if (beforeString) replaceList.push({ before: beforeString, after: afterStrings[index] }); }); return replaceList; } 以下省略
参考サイト
メール送信:
https://web-breeze.net/gmail-insertion-sending/
チェックボックス:
https://masagoroku.com/

回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。