Last active
August 18, 2016 07:17
-
-
Save eightyknots/36ce8ac6bdc7a55f1669a6580c8835d1 to your computer and use it in GitHub Desktop.
A column filter based on a cell's value in Google Apps Scripting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Basically, we only want this to run when you edit something in this spreadsheet. | |
* | |
* The "onEdit" function is a reserved function that is triggered whenever you edit | |
* anything in the sheet. | |
* | |
* Save this, then make sure you've selected "onEdit" as the function to run. Whenever | |
* you edit the filterCell value, it should automatically run this function without | |
* you having this window open. | |
* | |
* @param {event} Google Apps Scripting event object | |
*/ | |
function onEdit(e) { | |
// Documentation for "e": https://developers.google.com/apps-script/guides/triggers/events | |
// Basically we only care if the cell edited was the "filter" cell | |
var filterCell = "L4"; | |
var changedCell = e.range.getA1Notation(); | |
var changedSheet = e.source.getActiveSheet(); | |
// Now, we check to make sure the cell that was edited was the cell in question (filterCell) | |
if (changedCell == filterCell) { | |
// It is, so we get the value and pass it onto filterSubjects() so we don't have to look it up again later | |
// This is good engineering: filterSubjects() has all the context it needs to operate. Don't need to | |
// look anything up | |
var filterCellValue = e.range.getValue(); | |
return filterSubjects(changedSheet, filterCellValue); | |
} else { | |
return; | |
} | |
} | |
/** | |
* This is the main function that runs, given a sheet, what to hide and what to show. | |
* You'll want to edit the cases here and duplicate as neccessary. | |
* | |
* @param {Sheet} A Google Spreadsheets Sheet object | |
* @param {String} The value of the filtered cell to act upon | |
*/ | |
function filterSubjects(sheet, filterCellValue) { | |
// First we hide all the subject columns. | |
// Assuming all sheets are laid out the same...if not you'd need additional logic here | |
sheet.hideColumns(13,11); | |
// Next, to make this work no matter how someone puts in the filter, just make the | |
// comparison case-insensitive by making everything lowercase. | |
filterCellValue = filterCellValue.toLowerCase(); | |
// Here is your switch/case statement. Basically it tries to match exactly each case. | |
// If it does not, it goes to the default, which is to show everything. | |
switch (filterCellValue) { | |
case "english": | |
sheet.showColumns(13); | |
break; | |
case "maths": | |
sheet.showColumns(14); | |
break; | |
default: | |
// Just in case something invalid was entered, show everything | |
sheet.showColumns(13,11); | |
break; | |
} | |
// We done! | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment