2016-10-29 8 views
0

スプレッドシートにボタンを追加し、それにスクリプトを割り当てました。クリックしたユーザーのユーザーの電子メールを判別する方法はありますか?スクリプトはonEditトリガーが機能するはずなので、このトリガーで設定されたSession.getActiveUser().getEmail()の機能はユーザーを認識しないようにデータを編集します。スプレッドシート内のボタンをクリックしたユーザーを取得する

ありがとうございました

+0

これはドメインまたは公開側のスクリプトですか? –

+1

「通常の」Gmailアカウントを使用している場合、ユーザーがスプレッドシートにアクセスしたときにそのユーザーの電子メールを取得することはできません。ビジネスまたは教育用のバージョンでは可能です。 –

+1

@Sergeinsasああ、私はそれを知らなかった。それは吸う。しかし、情報に感謝します。ちょうどそれをテストし、あなたが正しいです。 – Wlad

答えて

4

この回避策では、通常のGmailアカウントでは可能です。

私はドキュメントのユーザーと所有者を明らかにするいくつかの保護機能を使用していますが、パフォーマンスを向上させるためにプロパティに格納しています。それを楽しみましょう!

function onEdit(e) { 
    SpreadsheetApp.getUi().alert("User Email is " + getUserEmail()); 
} 

function getUserEmail() { 
    var userEmail = PropertiesService.getUserProperties().getProperty("userEmail"); 
    if(!userEmail) { 
    var protection = SpreadsheetApp.getActive().getRange("A1").protect(); 
    // tric: the owner and user can not be removed 
    protection.removeEditors(protection.getEditors()); 
    var editors = protection.getEditors(); 
    if(editors.length === 2) { 
     var owner = SpreadsheetApp.getActive().getOwner(); 
     editors.splice(editors.indexOf(owner),1); // remove owner, take the user 
    } 
    userEmail = editors[0]; 
    protection.remove(); 
    // saving for better performance next run 
    PropertiesService.getUserProperties().setProperty("userEmail",userEmail); 
    } 
    return userEmail; 
} 
+0

ありがとう、スプライス関数が返されました(私はなぜか分かりません)いつもオーナーのメールですが、デバッグでは他のメールが配列内にあることがわかります。 – Wlad

+0

そうです。 splice関数は所有者を削除します。したがって、ログインする場合は、それも返されます。 –

関連する問題