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.

【GAS】実行ユーザーの名前を取得する方法

Last updated at Posted at 2022-05-29

結論

プロパティに名前を登録しておき、プロパティから情報を取得する。
公式ドキュメント:Properties Service

Google連絡先に名前を登録しておき、情報を取得する方法もあるが、実行時間が約10倍かかります。
公式ドキュメント:Class ContactsApp

サンプルコード

sample.gs
// プロパティに情報を登録しておく
function setProp() {
  let scriptProperties = PropertiesService.getScriptProperties();
  //'example@gmail.com'は実行ユーザーのメールアドレスに置き換える
  scriptProperties.setProperties({
    'example@gmail.com_familyname': '山田',
    'example@gmail.com_fullname': '山田太郎'
  });
}

// プロパティから情報を取得
function getProp(){
  console.time('getProp()の実行時間');
  // 実行ユーザーのメールアドレス取得
  let userEmail = Session.getActiveUser().getEmail();
  // 取得したメールアドレスと「_familyname」「_fulname」を組み合わせて、プロパティから名前を取得
  let scriptProperties = PropertiesService.getScriptProperties();
  let propData = scriptProperties.getProperties();
  console.log('getProp():' + propData[userEmail + '_familyname']);
  console.log('getProp():' + propData[userEmail + '_fullname']);
  console.timeEnd('getProp()の実行時間');
}

// Google連絡先に名前を登録しておく。
// Google連絡先から情報を取得
function getGoogleContent(){
  console.time('getGoogleContent()実行時間');
  // 実行ユーザー情報取得 
  let userData = Session.getActiveUser();
  // 取得したユーザー情報で、Google連絡先に登録されている連絡先>苗字を取得
  let familyName = ContactsApp.getContact(userData).getFamilyName();
  // 取得したユーザー情報で、Google連絡先に登録されている連絡先>フルネームを取得
  let fullName = ContactsApp.getContact(userData).getFullName(); 
  console.log('getGoogleContent():' + familyName);
  console.log('getGoogleContent():' + fullName);
  console.timeEnd('getGoogleContent()実行時間');
}

//実行時間を比較
function measureTime(){
  getProp();
  getGoogleContent();
}

実行結果(prop_contact).png

参考文献

・実行時間をログ出力する方法

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?