隐藏电子表格中特定用户的工作表 [英] Hiding worksheets from specific users inside a spreadsheet

查看:48
本文介绍了隐藏电子表格中特定用户的工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我对编码不熟悉,我有一个包含多个工作表的电子表格,已与许多人共享,我试图与新用户共享时将这些表默认隐藏,并将一些用户定义为一个管理员用户以及每个位置的用户组,我可以运行该代码,并且在较短的时间内可以运行,我知道可能有最简单的方法,但是您相信有一个特定的原因,我无法使代码运行,除非删除IF语句,否则用户仍会看到隐藏的工作表.

Sorry I'm not familiar with coding, I have a spreadsheet with multiple worksheets, it's shared with many people, I'm trying to make those sheets hidden by defaults when it's shared with a new person, and define some users as an admin users, and user groups for each location, I could run the code, and it worked when it was shorter, I know there might be easiest ways, however do you beilive there is a speific reason I'm getting nothing running the code, and users still see the hidden sheets unless I delete the IF statements.

非常感谢.

function onOpen() {
      var Adminusr= ['amw22test.com','taaa@test.com','od22@test.com'];
      var Germanyusr = ['brs@test.com', 'ya32@test.com'];
      var Austriausr = ['dr32@test.com', 'ok42@test.com'];
      var Greeceusr = ['karw@test.com'];
      var Polandusr = ['shi0@test.com, Ma@test.com'];
      var Spainusr = ['sad2@test.com, ko12@test.com, tes4@test.com, kkwi@test.com']; 


        SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Germany').hideSheet()
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Austria').hideSheet()
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Greece').hideSheet()
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Poland').hideSheet()
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Spain').hideSheet()



      if (Adminusr.indexOf(Session.getEffectiveUser().getEmail()) >= 0 || Germanyusr.indexOf(Session.getEffectiveUser().getEmail()) >= 0) {
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Germany').showSheet()
      }

      if  (Adminusr.indexOf(Session.getEffectiveUser().getEmail()) >= 0 || Austriausr.indexOf(Session.getEffectiveUser().getEmail()) >= 0) {
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Austria').showSheet()
        }

      if (Adminusr.indexOf(Session.getEffectiveUser().getEmail()) >= 0 || Greeceusr.indexOf(Session.getEffectiveUser().getEmail()) >= 0) {
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Greece').showSheet()
      }

      if (Adminusr.indexOf(Session.getEffectiveUser().getEmail()) >= 0 || Polandusr.indexOf(Session.getEffectiveUser().getEmail()) >= 0) {
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Poland').showSheet()
      }

      if (Adminusr.indexOf(Session.getEffectiveUser().getEmail()) >= 0 || Spainusr.indexOf(Session.getEffectiveUser().getEmail()) >= 0) {
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Spain').showSheet()
      }


    }

推荐答案

您的项目有两个主要问题

Your project has two main problems

  1. 授权
  2. 算法


授权

您缺少 onOpen Class Session 的限制.

onOpen 是打开简单触发器的保留名称.当打开事件触发该事件时,将无法执行需要授权的方法.

onOpen is a reserved name for the open simple trigger. When it's triggered by an open event, then can't execute methods that require authorization.

Session.getEffectiveUser().getEmail() Session.getActiveUser().getEmail()可能起作用,但将返回空字符串当电子表格由编辑者打开时.相关问题: Session.getActiveUser().getEmail()不返回任何值

Session.getEffectiveUser().getEmail() and Session.getActiveUser().getEmail() might work when the owner open the spreadsheet but will return an empty string when the spreadsheet is opened by an editor. Related question: Session.getActiveUser().getEmail() returns no Value

使用自定义菜单将活动的用户电子邮件地址保存在用户属性存储中的一个技巧,然后 onOpen 可以从那里获取用户电子邮件地址,而不是使用 Class Session .相关问题:确定Apps脚本中的当前用户

One trick to use a custom menu to save the active user email address on the User Properties store, then onOpen could get the user email address from there instead of using Class Session. Related question: Determine current user in Apps Script

onOpen ,无论其他用户是否处于活动状态,只要 brs@test.com 打开电子表格,不久之后 amw22test.com 进行相同的操作,第一个用户将看到所有工作表.

onOpen will be executed every time the owner or an editor, any editor, opens the spreadsheet no matter if another user is active, so if brs@test.com opens de the spreadsheet and few moments later amw22test.com do the same, the first user will see all the sheets.

也许不应该为所有用户使用一个电子表格,而应该为每个组创建一个电子表格,为管理组创建一个主电子表格.

Maybe instead of using one spreadsheet for all users you should create one spreadsheet for each group and one master spreadsheet for the admin group.

其他相关问题

这篇关于隐藏电子表格中特定用户的工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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