如何通过安装onEdit触发器获取活动用户电子邮件? [英] How to get active user email with installed onEdit trigger?

查看:53
本文介绍了如何通过安装onEdit触发器获取活动用户电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些数据的Google电子表格.我编写了脚本来跟踪某些特定列的更改.

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?

推荐答案

来自 Session.getActiveUser() :

获取有关当前用户的信息.如果安全策略不允许访问用户的身份,则User.getEmail()返回一个空字符串. 情况,其中电子邮件地址可用 vary :例如,在任何允许未经用户授权运行脚本的上下文中,用户的电子邮件地址均不可用,例如简单的onOpen(e)或onEdit(e)触发器,Google表格中的自定义函数或部署为以我的身份执行"(即由开发人员而非用户授权)的Web应用.

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:

但是,如果开发人员自己运行脚本或与用户属于同一G Suite域,则这些限制通常不适用.

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.

这篇关于如何通过安装onEdit触发器获取活动用户电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆