简体   繁体   中英

How can I run my hide/show columns script on multiple ranges of columns in one google sheets spreadsheet?

I do not code program in javascript but I managed to find the script below online which allows me to hide one range of columns. Problem is I need to be able to repeat this for multiple ranges of columns in one sheet. so I can hide/show columns C-AG with one button and separately hide/show columns AI - BJ with another button (and do this for multiple months). This is for appointments with lots of data so I can easily hide and show each month of appointments.

Please see below images this demonstrates the functionality. (First image shows January shown, second shows January hidden.)

Spreadsheet before running hide script -

Spreadsheet after running hide script

Please also see image of custom menu that this script puts in my toolbar. Within this menu is two buttons, first is Show C-AG second is Hide C-AG.

Custom menu created by script

This is the script which allows me to hide/show one range of columns (C-AG) and works well but is limited to only one group of columns (one month):

 function onOpen() {
    var menu = [{name: "Show C-AG", functionName: "showColumns"}, {name: "Hide C-
    AG", functionName: "hideColumns"}]
    SpreadsheetApp.getActiveSpreadsheet().addMenu("Custom", menu);
}

  function showColumns() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActiveSheet();
    sheet.showColumns(3,31);   // C-AG, 31 columns starting from 3rd 
}

  function hideColumns() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActiveSheet();
    sheet.hideColumns(3,31);  // C-AG, 31 columns starting from 3rd 
}

Thank you in advance for any help on this, and apologies if my question is too vague and feel free to tell me to add more info/description if necessary.

If you don't want to create a custom menu option for each month and have a function to show and another to hide columns for each month you have to use parameters and figure a way to set and to get those parameters and send them to your functions.

One way is to use a cell in a spreadsheet to set the month. Another way is to use a dialog / sidebar to do that. Will be opt to you if you will use a plain text input field or if you use a dropdown, radio button, etc.

This should do it for you for a while. All you have to do is type in all the column numbers that you want to hide and show. And it's helpful to run the onOpen function in the script editor so that you can authorize it.

The intermediate function showMyCols and hideMyCols just make it possible to write one function to do the showing and hiding and they keep you from having to worry about how to setup the parameters.

function onOpen(){
  SpreadsheetApp.getUi().createMenu('MyTools')
      .addItem('Show', 'showMyCols')
      .addItem('Hide', 'hideMyCols')
      .addToUi();
}

function showMyCols(){
  showhideCols('3,4,5,6,7,8,9,10',true);
}

function hideMyCols(){
  showhideCols('3,4,5,6,7,8,9,10',false);
}

function showhideCols(colscsv,show){
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  if(!colscsv){
    var resp=SpreadsheetApp.getUi().prompt('Enter Column Numbers separated by commas.', SpreadsheetApp.getUi().Button.OK);
    var cA=resp.getResponseText().split(',');
  }else{
    var cA=colscsv.split(',');
  }
  for(var i=0;i<cA.length;i++){
    if(show){
      sh.showColumns(cA[i]);
    }else{
      sh.hideColumns(cA[i]);
    }
  }
}

Thanks for the help I really appreciate it, I ended up fudging the code posted by Cooper. I know this is messy but it works great for what I need. Thanks again!

function onOpen(){
  SpreadsheetApp.getUi().createMenu('MyTools')
      .addItem('HideJanuary', 'hideCols1')
      .addItem('HideFebruary', 'hideCols2')
      .addItem('HideMarch', 'hideCols3')
      .addToUi();
}

function hideCols1(){

showhideCols('3,4,5,6,7,8,9,10,11,12,13,14,15
,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33',false);
}

function hideCols2(){

showhideCols('35,36,37,38,39,40,41,42,43,44,45,
46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62',false);
}

function hideCols3(){

showhideCols('64,65,66,67,68,69,70,71,72,73,74,75,
76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94',false);
}

function showhideCols(colscsv,show){
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  if(!colscsv){
    var resp=SpreadsheetApp.getUi().prompt('Enter Column Numbers separated by 
commas.', SpreadsheetApp.getUi().Button.OK);
    var cA=resp.getResponseText().split(',');
  }else{
    var cA=colscsv.split(',');
  }
  for(var i=0;i<cA.length;i++){
    if(show){
      sh.showColumns(cA[i]);
    }else{
      sh.hideColumns(cA[i]);
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM