0

So, the goal is to take the emails from the 'Emails' tab (which contains 8 different lists), put them into an array. Then, depending on who is the active user, hide and show specific tabs. I have looked into many different methods of this (I'm doing it for privacy reasons) and this seems to be the only reasonable way of doing it. Each user needs access to edit the tabs that they have access to.

I also have a 'Troubleshooting' tab that I'm kind of using as a settings tab. This contains a list of the different tab names and which list gets access to it. (each tab will only have one list it corresponds to)

My issue is that it is not hiding any of the tabs.

  • updateHiddenSheets() has a trigger 'on open'
  • validUsers() has a trigger 'on edit'

Both run with no errors, so I'm not sure where the issue is here.

/*Things to Do:
    - make the tabs sorted by ward/stakepeople
    - have tab names automatically update in this code .getSheetByName()
    - 
*/
var stakePeople = [];
var ward1 = [];
var ward2 = [];
var ward3 = [];
var ward4 = [];
var ward5 = [];
var ward6 = [];
var ward7 = [];

function validUsers() {
  var activeSheetName = SpreadsheetApp.getActiveSheet().getSheetName();
  if(activeSheetName = 'Emails'){
    var numEmails = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Emails').getRange(52,2).getValue();


    var emailsList = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Emails').getRange(2,1,numEmails,8).getValues();
    var numEmailsAct = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Emails').getRange(51,1,1,8).getValues();

    for(i=0; i<=numEmailsAct[0];i++) {
      ward1.push(emailsList[i][0]);
    }
    for(i=1; i<=numEmailsAct[1];i++) {
        ward2[i] = emailsList[i][1];
    }
    for(i=1; i<=numEmailsAct[2];i++) {
        ward3[i] = emailsList[i][2];
    }
    for(i=1; i<=numEmailsAct[3];i++) {
        ward4[i] = emailsList[i][3];
    }
    for(i=1; i<=numEmailsAct[4];i++) {
        ward5[i] = emailsList[i][4];
    }
    for(i=1; i<=numEmailsAct[5];i++) {
        ward6[i] = emailsList[i][5];
    }
    for(i=1; i<=numEmailsAct[6];i++) {
        ward7[i] = emailsList[i][6];
    }
    for(i=1; i<=numEmailsAct[7];i++) {
        stakePeople[i] = emailsList[i][7];
    }
  }
}

function updateHiddenSheets(){
  var currentUserEmail = Session.getActiveUser().getEmail()
  var wardSheetNames = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Troubleshooting').getRange(24,3,19,2).getValues();

  var ward1B = false;
  var ward2B = false;
  var ward3B = false;
  var ward4B = false;
  var ward5B = false;
  var ward6B = false;
  var ward7B = false;
  var stakePeopleB = false;

  if (ward1.includes(currentUserEmail)) { 
    ward1B = true;
  } 
  if (ward2.includes(currentUserEmail)) { 
    ward2B = true;
  } 
  if (ward3.includes(currentUserEmail)) { 
    ward3B = true;
  } 
  if (ward4.includes(currentUserEmail)) { 
    ward4B = true;
  } 
  if (ward5.includes(currentUserEmail)) { 
    ward5B = true;
  } 
  if (ward6.includes(currentUserEmail)) { 
    ward6B = true;
  } 
  if (ward7.includes(currentUserEmail)) { 
    ward7B = true;
  } 
  if (stakePeople.includes(currentUserEmail)) {
    stakePeopleB = true;
  }

  if(ward1B){
    for(i=0;i<19;i++){
      if(wardSheetNames[i][1]="1"){
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName(wardSheetNames[i][0]).showSheet(); 
      }
    }
  }
  if(ward2B){
    for(i=0;i<19;i++){
      if(wardSheetNames[i][1]="2"){
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName(wardSheetNames[i][0]).showSheet(); 
      }
    }
  }
  if(ward3B){
    for(i=0;i<19;i++){
      if(wardSheetNames[i][1]="3"){
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName(wardSheetNames[i][0]).showSheet(); 
      }
    }
  }
  if(ward4B){
    for(i=0;i<19;i++){
      if(wardSheetNames[i][1]="4"){
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName(wardSheetNames[i][0]).showSheet(); 
      }
    }
  }
  if(ward5B){
    for(i=0;i<19;i++){
      if(wardSheetNames[i][1]="5"){
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName(wardSheetNames[i][0]).showSheet(); 
      }
    }
  }
  if(ward6B){
    for(i=0;i<19;i++){
      if(wardSheetNames[i][1]="6"){
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName(wardSheetNames[i][0]).showSheet(); 
      }
    }
  }
  if(ward7B){
    for(i=0;i<19;i++){
      if(wardSheetNames[i][1]="7"){
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName(wardSheetNames[i][0]).showSheet(); 
      }
    }
  }
  if(stakePeopleB){
    for(i=0;i<19;i++){
      if(wardSheetNames[i][1]="Stake"){
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName(wardSheetNames[i][0]).showSheet(); 
      }
    }
  }
  for(i=0;i<19;i++){
    if(wardSheetNames[i][1]="All"){
      SpreadsheetApp.getActiveSpreadsheet().getSheetByName(wardSheetNames[i][0]).showSheet(); 
    }
  }
} 
Rubén
  • 34,714
  • 9
  • 70
  • 166
Kevin
  • 15
  • 3
  • 1
    Personally I think it would be better to share completely separate spreadsheets with each user if it's a matter of privacy. – Cooper Feb 07 '21 at 23:39
  • 1
    I think you want this `var numEmailsAct = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Emails').getRange(51,1,1,8).getValues()[0];` getValues() always returns a 2d array `numEmailsAct[1]` doesn't exist – Cooper Feb 08 '21 at 01:20

1 Answers1

1

The code doesn't include a hideSheet() method which is required to hide the sheets.

The if expressions are assigning a value to wardSheetNames[i][1] instead of comparing it to the corresponding value

  • = assign operator
  • == abstract equality comparator
  • === strict equality comparator

Please bear in mind that the sheets are hidden / shown for any user no only the active user.

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166