LoginSignup
0
1

More than 3 years have passed since last update.

【GAS】Google Formで送信者のアドレスを取得する方法

Last updated at Posted at 2020-12-14

Google Formで、フォーム送信者のアドレスを取得したい場合には、Sessionオブジェクトではなくevent.responseオブジェクトから取得する。

function sendEmail(e) {
  const email = e.response.getRespondentEmail();
  GmailApp.sendEmail(email, "Confirmation", "Thank you for submitting.");
}

Session.getActiveUser().getEmail()を使っていたのだけど、それだとアプリ作成者のアドレスを取得してしまうようだった(詳しい違いは未調査)。

自分が実務で使う場合、フォームの回答者は数人に限られているので、予め登録しておいたユーザーの中からアドレスをもとに名前を表示させることができる。

// 取得したアドレスを元に、フォーム記入者の名前を表示する。
function buildEmailBody(email) {
  const users = [
    { name: "John Doe",   email: "john@example.com" },
    { name: "Lara Croft", email: "lara@example.com" },
  ];

  const sender = users.find(user => user.email === email);

  return sender + "さんからフォーム回答がありました";
}

Session.getActiveUser()の役割

経験上Sessionは、ウェブアプリとして公開したときのユーザー取得に使えることが分かっている。

コード.gs
// ウェブアプリとして公開されているアプリ
function doGet(e) {
  return HtmlService.createTemplateFromFile("index").evaluate();
}

function getUser() {
  const users = [
    { name: "John Doe",   email: "john@example.com" },
    { name: "Lara Croft", email: "lara@example.com" },
  ];  
  const user = users.find(user => user.email === email);

  return "こんにちは" + user + "さん";
}
index.html
<? const user = getUser(); ?> // .gsのコードを呼び出す
<p>こんにちは <?!= user ?> さん</p>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1