确定Apps Script中的当前用户 [英] Determine current user in Apps Script

查看:86
本文介绍了确定Apps Script中的当前用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  r.setComment(

我试图找出当前用户的名字, Edit at+(New Date())+by+ Session.getActiveUser()。getEmail());

但它不起作用 - 用户名是空字符串。
我在哪里出错了?

解决方案

好消息:这是可能的解决方法!

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

  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'm trying to identify current user's name to make notes of who edited what like this:

  r.setComment("Edit at " + (new Date()) + " by " + Session.getActiveUser().getEmail());

but it won't work - user's name is an empty string. Where did I go wrong?

解决方案

GOOD NEWS: It's possible 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;
}

这篇关于确定Apps Script中的当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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