---
# System prepended metadata

title: 用Google Apps Script自動寄送表單回應資料
tags: [Google, Apps Script]

---

# 用Google Apps Script自動寄送表單回應資料
於Google表單點選指令碼編輯器，將下方程式碼貼入並設置觸發條件即可於接收到回應時傳送回應資料給使用者。
## 程式碼
```javascript=
/**
 * 載入信件範本。
 * @type {GoogleAppsScript.HTML.HtmlTemplate}
 */
let mt = HtmlService.createTemplateFromFile("MailTemplate");

/**
 * 載入資料範本。
 * @type {GoogleAppsScript.HTML.HtmlTemplate}
 */
let rt = HtmlService.createTemplateFromFile("RowTemplate");

/**
 * 發送郵件的函數。
 * @param {string} touser - 收件者的電子郵件地址
 * @param {string} mailBody - 郵件內容
 */
function sendEmail(touser, mailBody) {
  const title = "報名成功";
  MailApp.sendEmail(touser, title, mailBody, {
    htmlBody: mailBody,
    cc: "aaa@gmail.com", //寄送副本
  });
}

/**
 * 主函數，處理表單提交事件。
 */
function main() {
  var form = FormApp.getActiveForm();
  var formResponses = form.getResponses();
  // 取出最新一筆回應
  var targetRes = formResponses[formResponses.length - 1];
  // 取得回答者email
  var email = targetRes.getRespondentEmail();

  var itemResponses = targetRes.getItemResponses();
  var rowContent = "";
  for (var i = 0; i < itemResponses.length; i++) {
    var row = rt.getRawContent();
    row = row.replace("{{title}}", itemResponses[i].getItem().getTitle());
    row = row.replace("{{val}}", itemResponses[i].getResponse());
    rowContent += row;
  }
  var mailBody = mt.getRawContent();
  mailBody = mailBody.replace("{{Content}}", rowContent);
  Logger.log(mailBody);
  // 發送通知信
  sendEmail(email, mailBody);
}

```
## 信件範本MailTemplate
```htm=
<!DOCTYPE html>
<html>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <style type="text/css">
    body {
      margin: 0;
      padding: 0;
      -ms-text-size-adjust: 100%;
      -webkit-text-size-adjust: 100%;
      font-family: Microsoft JhengHei, MingLiU, sans-serif;
    }

    table {
      mso-table-lspace: 0pt;
      mso-table-rspace: 0pt;
      background: #FFFFFF;
      border: 0;
    }

    img {
      -ms-interpolation-mode: bicubic;
    }

    @media screen and (max-width: 480px) {
      table {
        box-sizing: border-box;
      }

      .m-100,
      .m-100 tbody,
      .m-100 tr {
        display: block;
        padding: 0;
        max-width: 100% !important;
        height: auto !important;
      }
      .m-banner {
        max-width: 190px;
      }
    }

  </style>

  <div style="width: 100%; max-width: 800px; display: block; margin: 0 auto;">
    <p align="center" class="m-100" style="margin: 0; padding: 0; font-size: 24px; color: #212121; font-weight: bold; max-width: 800px; text-align: center">感謝您的回覆</p>
    {{Content}}
  </div>

  
  <div style="text-align: center; color:#555555; font-size: 12px;">
    <p style="margin: 0;">如有任何疑問，歡迎回信告知</p>
  </div>
</html>
```
## 資料範本RowTemplate
```html=
<div style="min-width: 150px;max-width: 800px;font-size: 18px;">
  <div style="background:#000000;color:#FFFFFF;">
    {{title}}
  </div>
  <div>
    {{val}}
  </div>
</div>
```
## 觸發條件設定
![image](https://hackmd.io/_uploads/ryo1zV6b0.png)

## 完成結果
![image](https://hackmd.io/_uploads/HJHeL46b0.png)
