google-apps-script - 当用户编辑电子表格中的单元格时,是否有脚本函数可以获取用户电子邮件?
问题描述
我在搜索和创建脚本时遇到了一些困难。我想要实现的是在用户编辑我的电子表格中的单元格时获取用户的电子邮件/姓名。我尝试了 getactiveuser 但它没有显示任何内容。我读到有人要求使用 geteffectiveuser,但我得到的仍然是我的用户名,而不是其他编辑器。
到目前为止,这是我的脚本:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "TEST" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
var email = Session.getActiveUser().getEmail();
if( r.getColumn() == 4 ) { //checks the column for HUB side
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(email());
}
}
}
Logger.log(Session.getActiveUser().getEmail());
如上所述,什么都没有出现。
解决方案
您可以对以下代码进行测试,它对我有用:)
function onEdit(e) {
var s = e.source.getActiveSheet();
if( s.getName() == "TEST" ) { //checks that we're on the correct sheet
var r = e.range;
if( r.getColumn() == 4 ) { //checks the column for HUB side
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(Session.getActiveUser().getEmail());
}
}
}
推荐阅读
- three.js - 操纵gltf模型子
- node.js - 使用请求 js 损坏的文件
- c# - c# Httpclient单例在多个客户端/用户上具有相同的身份验证标头
- opencl - 为什么我的全局同步尝试不起作用?
- python - 根据前一行值创建一个新列并删除当前行
- python - 在系列结束时为 pct_change 扩展数据/收缩窗口
- python - 如何在后台接受新的连接请求?
- mysql - mysql索引在rails中行为不端
- swift - 如何通过 lastModifiedUserRecordID 查询 CloudKit?
- php - 使用 preg_match 检查多个 $_POST