获取点击电子表格中按钮的用户 [英] Get the user who clicked on the button in the spreadsheet

查看:116
本文介绍了获取点击电子表格中按钮的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将按钮添加到电子表格并将脚本分配给它。有没有办法确定用户点击它的人的电子邮件?脚本编辑数据,所以可能 onEdit 触发器应该可以工作,但函数使用 Session.getActiveUser()。getEmail() set通过这个触发器无法识别用户。



谢谢

解决方案

这是正常的Gmail帐户可能的解决方法!

我使用了一些保护功能来揭示文档的用户和所有者,并将其存储在属性中以获得更好的性能。

  function onEdit(e){
SpreadsheetApp.getUi()。alert(User Email是+ getUserEmail());


函数getUserEmail(){
var userEmail = PropertiesService.getUserProperties()。getProperty(userEmail);
if(!userEmail){
var protection = SpreadsheetApp.getActive()。getRange(A1)。protect();
// tric:所有者和用户不能被删除
protection.removeEditors(protection.getEditors());
var editors = protection.getEditors();
if(editors.length === 2){
var owner = SpreadsheetApp.getActive()。getOwner();
editors.splice(editors.indexOf(owner),1); //删除所有者,接收用户
}
userEmail = editors [0];
protection.remove();
//保存以获得更好的性能,然后运行
PropertiesService.getUserProperties()。setProperty(userEmail,userEmail);
}
返回userEmail;
}


I have added the button into spreadsheet and assigned the script to it. Is there a way to determine the user's email of person that clicked it? Script edits the data so probably onEdit trigger should work, however function with Session.getActiveUser().getEmail() set by this trigger doesn't recognize the user.

Thank you

解决方案

It's possible with normal gmail accounts, with this workaround!

I'm using some protection functionality that reveals the user and owner of the document and I'm storing it in the properties for better performance. Have fun with it!

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;
}

这篇关于获取点击电子表格中按钮的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆