How to get active user email with installed onEdit trigger?

不羁的心 提交于 2021-02-08 06:48:59

问题


I have a Google spreadsheet with some data. I wrote script to track changes of some specific columns.

function onOpen() {
  var ss = SpreadsheetApp.getActive();
  var menuItems = [
    {name: 'Turn on', functionName: 'createSpreadsheetEditTrigger'}
    ];
  ss.addMenu('Tracker', menuItems);
}


function changeTrack(){
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ui = SpreadsheetApp.getUi();
  var ws = ss.getActiveSheet(); 
  const headerRow = 4;
  const editBodyCols = [2, 3, 4, 5];
  const fResultCol = 6;
  var range = ws.getActiveRange();
  var row = range.getRow();
  var col = range.getColumn();
  let target1 = ws.getRange(row, fResultCol);  
  let target2 = ws.getRange(row, fResultCol + 1)
  let activeUser = getCurrentUserEmail();
  if(row > headerRow && editBodyCols.some(x => x === col) === true){
    if(target1.getValue() !== ""){
    target2.setValue(result(ss, ws, row, activeUser)[1]);
    } else {
      target1.setValue(result(ss, ws, row, activeUser)[0])
      target2.setValue(result(ss, ws, row, activeUser)[1])
    }
  }  
}

function createSpreadsheetEditTrigger() {
  var ss = SpreadsheetApp.getActive();
  ScriptApp.newTrigger('changeTrack')
      .forSpreadsheet(ss).onEdit()     
      .create();
}

function date(){
  return  Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy-MM-dd HH:mm:ss");  
}

function result(ss, ws, row, activeUser) {  
  const ssName = ss.getName();  
  let data = `Создал ${activeUser} ${date()}`;
  let exp = `Файл ${ssName}, Лист ${ws.getName()}, изменил ${activeUser}, строка № ${row}, ${date()}`;
  let adds = [];
  adds.push([data],[exp]);
  return adds;
}

function getCurrentUserEmail()
{
  var email=Session.getActiveUser().getEmail();
  return email;
}

My problem is to get active user's email. This script can get it but not all the time. Seems like random success. It means sometimes I can get expected value, sometimes not. I don't understand what is it depends from. Where I'm wrong and how to fix it?


回答1:


From the documentation on Session.getActiveUser():

Gets information about the current user. If security policies do not allow access to the user's identity, User.getEmail() returns a blank string. The circumstances in which the email address is available vary: for example, the user's email address is not available in any context that allows a script to run without that user's authorization, like a simple onOpen(e) or onEdit(e) trigger, a custom function in Google Sheets, or a web app deployed to "execute as me" (that is, authorized by the developer instead of the user).

So this seems pretty expected and there is no hard workaround you can make to retrieve the users mail. You should maybe just ask for it and see if they be willingly want to give it to you.

Although if you are the developer or the users are inside your organization this restrictions may be ignored:

However, these restrictions generally do not apply if the developer runs the script themselves or belongs to the same G Suite domain as the user.



来源:https://stackoverflow.com/questions/61650266/how-to-get-active-user-email-with-installed-onedit-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!