Google表格的有效VS有效用户 [英] Active VS Effective User for Google Sheets

查看:49
本文介绍了Google表格的有效VS有效用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的Google应用程序脚本应用程序,该应用程序将根据请求页面的用户来执行一些数据操作.

I'm writing a simple google app script application which performs some data manipulation depends of the user who requests the page.

根据 google文档对象Session具有 getActiveUser() getEffectiveUser()(我目前用来确定用户).看一下代码:

According to google documentation object Session has getActiveUser() and getEffectiveUser() which I currently use in order to determine the user. Have a look at the code:

var email = Session.getActiveUser().getEmail();
  switch (email){
    case 'test@gmail.com':
      /*Some code here*/
      return true;
    case 'test2@gmail.com':
      /*Some code here*/
      return true;
    default:
      return false;
  }

它似乎应该可以工作,但不幸的是,它没有按预期工作(至少对我而言).

It looks like that it should work, unfortunately it doesn't work as expected (at least for me).

上面的代码在触发onOpen触发器时运行,并且所有权限都在用户首次尝试运行此代码时设置.

The code above runs when onOpen trigger fires, and all permissions are set when the user attempts to run this code for the first time.

因此,我决定执行跟踪,并发现 Session.getActiveUser().getEmail(); Session.getEffectiveUser().getEmail(); >为用户返回错误的电子邮件.

So, I've decided to perform tracing and found out that Session.getActiveUser().getEmail(); and Session.getEffectiveUser().getEmail(); return wrong emails for the users.

对于创建脚本 Session.getActiveUser().getEmail(); 的第一用户(我)返回正确的电子邮件,但对于所有其他用户,它也返回我的电子邮件.好的,我决定将 Session.getActiveUser().getEmail(); 替换为 Session.getEffectiveUser().getEmail(); 和BOOM-它适用于其他人但对我不起作用...

For the 1-st user (me) who created a script Session.getActiveUser().getEmail(); returns correct email, but for all the others it returns my email as well. Ok, I've decided to replace Session.getActiveUser().getEmail(); with Session.getEffectiveUser().getEmail(); and BOOM - it works for others but doesn't work for me...

怎么可能?有什么想法吗?

How could it be? Any thoughts?

  • 我注意到,当我从ScriptEditor运行代码时,代码对所有用户来说都是一个魅力,但是当它在onOpen触发时运行时,则无法预测.

此电子表格可以与多个人共享.

This spreadsheet is shared with several persons.

感谢您的帮助.

推荐答案

简短答案

代码运行了错误的语句,因为没有break语句来避免执行它们,另一方面,对于消费者帐户(gmail.com),只有活动用户运行脚本时,getActiveUser才会返回值 1 .

1 : https://developers.google.com/apps-script/reference/base/session#getActiveUser()

您应该在每个case语句集的末尾添加 break; ,否则下一个语句也将被执行.相反,请考虑以下条件供您测试.

You should add break; at the end of each case set of statements, otherwise the next the statements will be executed too. Instead, consider the following to for you tests.

function onOpen(){
  myFunction('On open - simple trigger');
}

function onOpenInstallable(){
  myFunction('On open - installable trigger');
}

function runFromScriptEditor(){
  myFunction('Run - script editor');
}

function myFunction(context) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow();
  var range = sheet.getRange(lastRow + 1,1,1,4);
  var output = [[
    new Date(),
    context,
    Session.getActiveUser().getEmail(),
    Session.getEffectiveUser().getEmail()
    ]];
  range.setValues(output); 
}

结果

所有者:testA@gmail.com
编辑者:testB@gmail.com

Result

Owner: testA@gmail.com
Editor: testB@gmail.com

  • testA@gmail.com打开电子表格时添加的行1和行
  • testB@gmail.com打开电子表格时添加的行3和行
  • 通过testB@gmail.com单击脚本编辑器的运行"按钮来触发分割时的第6行地址
+---+------------+-------------------------------+-----------------+-----------------+
|   |     A      |               B               |        C        |        D        |
+---+------------+-------------------------------+-----------------+-----------------+
| 1 | Timestamp  | Context                       | Active User     | Effective User  |
| 2 | 10/29/2016 | On open - simple trigger      | testA@gmail.com | testA@gmail.com |
| 3 | 10/29/2016 | On open - installable trigger | testA@gmail.com | testA@gmail.com |
| 4 | 10/29/2016 | On open - simple trigger      |                 |                 |
| 5 | 10/29/2016 | On open - installable trigger |                 | testA@gmail.com |
| 6 | 10/29/2016 | Run - script editor           | testB@gmail.com | testB@gmail.com |
+---+------------+-------------------------------+-----------------+-----------------+

这篇关于Google表格的有效VS有效用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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