use*_*633 7 google-apps-script
我正在尝试识别当前用户的名字,以记录谁编辑了这样的内容:
r.setComment("Edit at " + (new Date()) + " by " + Session.getActiveUser().getEmail());
Run Code Online (Sandbox Code Playgroud)
但它不起作用 - 用户的名字是一个空字符串.我哪里做错了?
Wim*_*der 12
好消息:这可以解决这个问题!
我正在使用一些保护功能,揭示文档的用户和所有者,并将其存储在属性中以获得更好的性能.玩得开心!
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;
}
Run Code Online (Sandbox Code Playgroud)
我想你已经将这段代码设置为在onEdit
函数内部执行(或者在编辑触发器上执行).
如果您使用的是消费者帐户,Session.getActiveUser().getEmail()
则会返回空白.仅当脚本的作者和用户位于同一Google Apps域时,它才会返回电子邮件地址.
归档时间: |
|
查看次数: |
15798 次 |
最近记录: |