0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Google Apps Script(GAS)でメールアドレスの一覧を取得と書き込みメモ

Last updated at Posted at 2022-10-30

よくある処理なのでメモ

こんな感じのシートがあってメールのA:A列をまるっと抜き出したいという処理です。

スクリーンショット 2022-10-30 22.02.11.png

## 取得

  • 余計な空行も入ってくるのでfilterで削除
  • 配列が入れ子構造になるのでflat()で入れ子構造を無くします。
function getEmailList() {
  const SSID = `<スプレッドのID>`;
  const sheet = SpreadsheetApp.openById(SSID);
  const range = sheet.getRange(`シート1!A:A`);
  const values = range.getValues();
  values.shift(); //ヘッダー部分、先頭削除
  const emailList = values
                      .filter(email => email[0]) //中身のある要素抽出
                      .flat(); //入れ子構造削除
  console.log(emailList);
}

  • 結果
['aaa@gmail.com'
'bbb@gmail.com'
'ccc@yahoo.co.jp']

こんな感じでメールアドレスのみの配列で取得できました。

書き込み

書き込みは至ってシンプル。1番左の列だけならこれでOK

function addEmailList(email = 'hoge@hoge.com'){
  const SSID = `<スプレッドのID>`;
  const sheet = SpreadsheetApp.openById(SSID);

  sheet.appendRow([email]);
}
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?