Trashman
Trashman

Reputation: 1596

Apps Script sheet.showSheet() not always showing sheet

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();

}

Upvotes: 0

Views: 397

Answers (1)

Cooper
Cooper

Reputation: 64140

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())
  }
}

Upvotes: 1

Related Questions