我想,以确定当前用户的名称,以使谁编辑什么像这样的注释:
r.setComment("Edit at " + (new Date()) + " by " + Session.getActiveUser().getEmail());
但它不会工作 - 用户的姓名为空字符串。 我哪里做错了?
我想,以确定当前用户的名称,以使谁编辑什么像这样的注释:
r.setComment("Edit at " + (new Date()) + " by " + Session.getActiveUser().getEmail());
但它不会工作 - 用户的姓名为空字符串。 我哪里做错了?
好消息是:这有可能与此解决办法!
我使用的显示文档的用户和所有者一定的保护功能,我将其存储在性能获得更好的性能。 有它的乐趣!
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;
}
我假设你有这片设置为内部执行代码onEdit
功能(或在编辑触发器)。
如果你是一个消费者账户, Session.getActiveUser)().getEmail()
将返回空白。 它将返回只有当脚本双方的作者和用户都在同谷歌Apps域的电子邮件地址。
我曾与维姆·登·赫德的解决方案麻烦,当我使用触发器运行的脚本。 任何非脚本所有者无法编辑受保护的单元格。 它工作得很好,如果脚本是从一个按钮运行。 但是我需要的脚本定期运行所以这是我的解决方案:
当用户使用纸张上的第一次,他/她应该点击一个按钮,运行以下命令:
function identifyUser(){
var input = Browser.inputBox('Enter User Id which will be used to save user to events (run once)');
PropertiesService.getUserProperties().setProperty("ID", input);
}
这节省了用户的输入用户属性。 它可以在以后随时与此代码回读:
。VAR用户= PropertiesService.getUserProperties()的getProperty( “ID”);
function onEdit(e) {
SpreadsheetApp.getUi().alert("User Email is " + e.user);
}