0

I have a Google Sheet which hides some sheets from most users, but when an admin user is identified, has a menu option to show all the sheets. It calls my function showAllSheets, as follows:

function showAllSheets() {

  var sheets=SpreadsheetApp.getActiveSpreadsheet().getSheets();

  for(var i = 0; i < sheets.length; i++){

    //Show each sheet
    sheets[i].showSheet();
    console.log('Showing Sheet: ' + sheets[i].getName())
  
  }

}

But this function doesn't always work. I get very mixed results. Sometimes it shows all sheets as expected. Sometimes it shows some of the sheets. Sometimes it eventually shows all or some of the sheets but only after a long delay (1 minute+) and sometimes, it does nothing at all.

I'm checking my execution time in the "Executions" section of Apps Script. This function typically executes in about 2-3 seconds and the console log contains all expected messages. It will say "completed" and still my sheets aren't showing. Sometimes it will eventually show the sheets some time after it says execution is complete and again, sometimes they never show.

I have an onOpen installable trigger and an onSelectionChange simple trigger, so at first I was concerned maybe my scripts are running into each other. However, I've confirmed I still have this issue even if I make sure all other scripts have completed before I run it.

I have no issues with .hideSheet() in my functions that hide the sheets (one being RestoreDefaultView, the other in my onOpen trigger.). They always get hidden immediately.

Here is the menu code I'm using:

  var ui = SpreadsheetApp.getUi();
  
  if (thisuser.group == 'admin') {
    
    ui.createMenu('MyProject(Admin)')
      .addSubMenu(
        ui.createMenu('View')
          .addItem('Restore Default Sheet View', 'restoreDefaultView')
          .addItem('Show All Sheets', 'showAllSheets')
        )
      .addToUi();

  }

What is going on and what can I do to fix it?

EDIT: Per request, here is the function that is hiding some of the sheets:

function restoreDefaultView() {

  const name_Master = 'PROJECT MASTER';
  const name_Lists = 'LISTS';
  const name_Guide = 'GUIDE';

  const name_Access = 'ACCESS';
  const name_ActionForm = 'frm_Action';
  const name_ProjectForm = 'frm_Project';
  const name_NotesForm = 'frm_Notes';
  const name_AdminForm = 'frm_Admin';
  const name_GroupAdmin = 'admin_Groups';

  //const id_ProjectList = '(redacted)'; // Google Drive file ID of Project List'
  //const id_TaskList = ''; //Google Drive file ID of Task List

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sht_Master = ss.getSheetByName(name_Master);
  var sht_Guide = ss.getSheetByName(name_Guide);
  var sht_Lists = ss.getSheetByName(name_Lists);
  var sht_Access = ss.getSheetByName(name_Access);
  var sht_ActionForm = ss.getSheetByName(name_ActionForm);
  var sht_ProjectForm = ss.getSheetByName(name_ProjectForm);
  var sht_NotesForm = ss.getSheetByName(name_NotesForm);
  var sht_AdminForm = ss.getSheetByName(name_AdminForm);
  var sht_GroupAdmin = ss.getSheetByName(name_GroupAdmin);

  //Sheets Normally Displayed
  //Immediately activate Master sheet after making visible to minimize confusion
  sht_Master.showSheet();
    sht_Master.activate();
  
  sht_Guide.showSheet();

  //Sheets Normally Hidden
  sht_Lists.hideSheet();
  sht_Access.hideSheet();
  sht_ActionForm.hideSheet();
  sht_ProjectForm.hideSheet();
  sht_NotesForm.hideSheet();
  sht_AdminForm.hideSheet();
  sht_GroupAdmin.hideSheet();

}
Trashman
  • 1,424
  • 18
  • 27
  • I migrated the question from [webapps.se] because troubleshooting like questions are off-topic there. Please add a [mcve] (it's not clear how the sheet hiding is done as well how it's managing having multiple users opening the spreadsheet while other users have it opened) – Rubén Feb 16 '22 at 20:34
  • My hypothesis is that concurrent users might be causing the issue. To test this, create a new spreadsheet where only an admin is able to access, then test the functions to see if it will always work or not. – NightEye Feb 16 '22 at 20:55
  • Currently I am testing the scripts and there are no other users using it. Sorry for muddying the waters by mentioning other possible users. There will be problems there I will have to deal with but for right now, it's not an issue. I can provide the function that hides some sheets. I don't know of anything else I can provide on this site other than the description I already provided. The issue is observed as the tabs on the bottom for each sheet either reappear or they don't. – Trashman Feb 16 '22 at 21:04
  • So, after a few more weeks of executing and testing, my original statement that restoredefaultview always works is not correct. It is more consistent, but occasionally it also gives me problems. I found this thread: https://stackoverflow.com/questions/12711072/how-to-pause-app-scripts-until-spreadsheet-finishes-calculation and tried it. It's still not 100% consistent, but by using the lock properties it SEEMS to me that it's working a little better - but it's hard to gauge when I'm seeing what appear to me as random results. As before, the longer my sheet is open the better it runs. – Trashman Mar 04 '22 at 20:22
  • After adding LOTS of debug logs and some protections, I think I got it working more stably. It's slow, unfortunately. But I think it's conflicting with my onSelectionChange trigger. It's not supposed to trigger based on script actions - and, in general, it doesn't - but there's one exception. When you change the active sheet via script, onSelectionChange still triggers. So it has been trying to run during my script. I changed my code to try to minimize the times the active sheet has changed and I added a detection code in onSelectionChange to abort quickly if it happened from a sheet change. – Trashman Mar 09 '22 at 23:34

1 Answers1

1

Try this:

function showAllSheets() {
  var sheets=SpreadsheetApp.getActiveSpreadsheet().getSheets();
  for(var i = 0; i < sheets.length; i++){
    sheets[i].showSheet();
    SpreadsheetApp.flush();
    console.log('Showing Sheet: ' + sheets[i].getName())
  }
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • You would think flush would do the trick, right? First time I tried running it, didn't work at all. Later it worked, but so did the original script. I have now noticed the longer my sheet is open, the more consistently my script is working (with or without adding .flush()). It seems to have the most problems in the first few minutes after I open it. After I use it for a while, it works well. I'm not sure if that's 100% true - I think after I leave it for a while and come back, the problem comes back, even if I didn't close. I need to troubleshoot more. This gives me a few more ideas to try. – Trashman Feb 17 '22 at 17:50
  • Lately I've noticed that chrome seems to have problems loading the entire page. Perhaps that part of your problem...Just guessing here. – Cooper Feb 17 '22 at 17:52