I created a custom menu in Google sheets that can hides / unhides columns depending on which metrics the person is interested to view. It's working great but it only works on "active sheet". I would like the script to hide certain columns in a sheet and hide other columns in another sheet.
The custom menu should contain 3 items: 'Karen view', 'Patrick View' and 'View all'
- Karen view:
- Hide columns 1 and 3 in sheet 1
- Hide columns 4 and 5 in sheet 2
- Patrick view:
- Hide columns 1, 2 and 4 in sheet 1
- Hide columns 4 and 5 in sheet 2
- View all:
- Unhide all columns across all the sheets
Below the script I'm currently using: (I'm a bit confused on how to use getactivespreadsheet along with getactivesheet, that's where I'm stuck)
var sh = SpreadsheetApp.getActiveSheet()
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Hide metrics')
.addItem('Patrick View', 'hideColumnsP')
.addItem('Karen View', 'hideColumnsK')
.addItem('View all', 'showColumns')
.addToUi();
}
function showColumns() {
sh.unhideColumn(sh.getRange(1, 1, 1, sh.getLastColumn()))
}
function hideColumnsK(){
showColumns()
sh.hideColumns(1)
sh.hideColumns(3)
}
function hideColumnsP(){
showColumns()
sh.hideColumns(1,2)
sh.hideColumns(4)
}
Thanks in advance for your help