首页 > 解决方案 > 当用户编辑电子表格中的单元格时,是否有脚本函数可以获取用户电子邮件?

问题描述

我在搜索和创建脚本时遇到了一些困难。我想要实现的是在用户编辑我的电子表格中的单元格时获取用户的电子邮件/姓名。我尝试了 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());

如上所述,什么都没有出现。

标签: google-apps-scriptgoogle-sheets

解决方案


您可以对以下代码进行测试,它对我有用:)

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

推荐阅读