Hide columns based on checkbox value - google-apps-script

I would like to implement a code to my production plan. Is there a way to hide column based on the checkbox true value? Lets say I added the checkboxes to the row 5, and I want to hide each column starting from M column separately upon ticking the checkbox. I implemented the code to hide the rows, but for columns it's getting tricky since I'm not experienced in google script.
Would it be perhaps also possible to add custom filter to show all hidden columns at once, lets say if I wanna make changes in one of them, and then hide all of them at once?

Hide columns starting with M
function onEdit(e) {
e.source.toast("Entry")
const sh = e.range.getSheet();
if (sh.getName() == "Enter Your Sheet Name" && e.range.columnStart > 12 && e.range.rowStart == 5 && e.value == "TRUE") {
e.source.toast("Flag1");
sh.hideColumns(e.range.columnStart);
e.range.setValue("FALSE");
}
}

Yes, these is! If you use an onEdit function, you can use e.range.isChecked() to see if a checkbox was checked. After that, you'd just need to run activeSheet.hideColumns(editedColumn, 1) to hide the column.
This code should do the trick:
function onEdit(e) {
var activeSheet = SpreadsheetApp.getActiveSheet();
var editedRange = e.range;
var editedRow = editedRange.getRow();
var editedColumn = editedRange.getColumn();
//If the checkbox isn't checked, return
if (!editedRange.isChecked()) return;
//If the edited row isn't in row 5, return
if (editedRow != 5) return;
//If the edited column is before M, return
if (editedColumn < 12) return;
//Hide the edited column
activeSheet.hideColumns(editedColumn, 1);
}

Related

How Do I Hide and Unhide Columns Using Checkboxes and Script in Google Sheets? [duplicate]

This question already has answers here:
How to show/hide rows in Google Sheet depending on whether check box is ticked
(1 answer)
Hide/unhide rows based on checkbox on Google sheet
(2 answers)
Hide Rows Based on Check Boxes - Google App Script
(2 answers)
Hide rows based on multiple checkbox values
(1 answer)
Closed 6 months ago.
Trying to create a script that will do the following:
Only operate for one specific worksheet within my workbook
Hide multiple non-sequential columns when a checkbox is checked
Unhide multiple non-sequential columns when a checkbox is unchecked
Incorporate three checkboxes total for three separate column groups
Here is what I'd like to set up:
When the checkbox within cell A25 is checked on worksheet 'Product Pricing', columns C,E,I,K,O,Q,U,W,AA,AC,AG,AI,AM,AO,AS,AU,AY,BA,BE,BG,BK,BM will hide.
When the checkbox within cell A25 is unchecked on the same worksheet, the same columns will unhide.
Then I need to repeat the above functions with two additional column sets.
I'm not a programmer by trade but can create some pretty robust spreadsheets using scripts, functions and conditional formatting. I appreciate anyone willing to help me learn something new that I can apply to this project as well as future projects.
Thanks so much!!
It can be something like this:
function onEdit(e) {
const name = e.source.getActiveSheet().getName();
if (name != 'Sheet1') return; // if not Sheet1 do nothing
const col = e.range.columnStart;
if (col != 1) return; // if not column A do nothing
const row = e.range.rowStart;
switch (row) {
case 1: var ranges = ['d','e']; break; // checkbox on row 1
case 2: var ranges = ['f','g']; break; // checkbox on row 2
case 3: var ranges = ['h','i']; break; // checkbox on row 3
default: return;
}
const value = e.value;
if (value == 'TRUE') hide_cols(ranges); // if check --> hide
else unhide_cols(ranges); // if uncheck --> unhide
}
function hide_cols(ranges) {
var sheet = SpreadsheetApp.getActiveSheet();
ranges.forEach(r => sheet.hideColumn(sheet.getRange(r+'1')));
}
function unhide_cols(ranges) {
var sheet = SpreadsheetApp.getActiveSheet();
ranges.forEach(r => sheet.unhideColumn(sheet.getRange(r+'1')));
}
Here is my sheet.
Try this:
function onEdit(e) {
const sh = e.range.getSheet();
if(sh.getName() == "Sheet0" && e.range.columnStart == 1 && e.range.rowStart == 25 && e.value == "TRUE") {
[1,5,9,11,17,21,23,27,29].forEach(c => sh.hideColumns(c));
}
if(sh.getName() == "Sheet0" && e.range.columnStart == 1 && e.range.rowStart == 25 && e.value == "FALSE") {
[1,5,9,11,17,21,23,27,29].forEach(c => sh.showColumns(c));
}
}
You will need to add the rest of the column numbers I got tired of adding them.
Also this may take longer than 30 seconds so you may require an installable onEdit trigger

Sort checked rows to bottom

I am using google sheets to sort school assignments. I am currently using the below coding to auto-sort the sheet by deadline. I have a checkbox in column F that will mark a row as "0" when it is done (when the box is checked). And "unfinished" when the box is not checked. I would like to include a piece in my auto-sort script that automatically puts rows marked "0" (box is checked) below all rows that are marked unfinished (box is not checked). The code below is all working but I am not sure how to go about adding this feature as I am quite new to coding. Any ideas?
(I've posted this before but it was taken down because it had too little detail, sorry if you saw the first one. This one is better)
function autoSort(e){
const ss = SpreadsheetApp.getActiveSpreadsheet()
const ws = ss.getSheetByName("School Assignments")
const range = ws.getRange(2,1,ws.getLastRow()-1,6)
range.sort({column: 5, ascending: true })
}
function onEdit(e){
const row = e.range.getRow()
const column = e.range.getColumn()
if(!(column === 5 && row >= 2)) return
autoSort(e)
}
Sorts checked items in column F to bottom
function onEdit(e) {
const sh = e.range.getSheet();
if(sh.getName() == "School Assignments" && e.range.columnStart == 6 && e.range.rowStart > 1 && e.value == "TRUE") {
sh.getRange(2,1,sh.getLastRow() -1,sh.getLastColumn()).sort({column:6,ascending:true});
}
}
This assume that the checked value is "TRUE" and the unchecked value is "FALSE"

How to set a cell value as TRUE in Google apps script

Column A in my sheet has checkboxes. I'm writing a simple script to find the checkbox that is checked (cell value = TRUE), make it unchecked (change it to FALSE), and then check the next checkbox in the column (make that cell value = TRUE).
Here's my code:
function nextCheckbox() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet()
var checkmarks = ss.getRangeByName("update_checkmarks").getValues().flat(); //this range is column A
Logger.log(checkmarks.indexOf(true)); //this logs 8.0, which is the correct row for the checked box in column A
var rowNum = checkmarks.indexOf(true);
Logger.log(rowNum); // this logs 8.0, as expected
var cell = sheet.getRange(rowNum,1);
cell.setValue(false); //nothing happens here...
var cell = sheet.getRange(rowNum + 1,1);
cell.setValue(true); //nothing happens here...
}
Logging logs the expected row number (8.0). But nothing happens when I use setValue. What am I doing incorrectly?
Regarding getting/setting values for checkboxes
Instead of using setValue consider to use check() / uncheck()
The above because checkboxes might use custom values for checked / unchecked states.
Regarding the use of indexOf / getRange
indexOf will return the index using 0 based numbering, this means 0 corresponds to the first value, 1 for the second and so on.
SpreadsheetApp.Sheet.Range.getRange(row,column) requires 1 based indexes, this means that the first row is 1, the second is 2.
Considering the above replace
var rowNum = checkmarks.indexOf(true);
by
var rowNum = checkmarks.indexOf(true) + 1;
Resources
https://developers.google.com/apps-script/reference/spreadsheet/range#check
https://developers.google.com/apps-script/reference/spreadsheet/range#uncheck
Your array starts at zero while your rows start at one. Currently your setting row 7 to be false(which it already is) and row 8 to be true, which it also already is.
Change this line in your code:
var rowNum = checkmarks.indexOf(true);
to be
var rowNum = checkmarks.indexOf(true)+1;
and you should get your expected results.
function onEdit(e) {
const sh = e.range.getSheet();
if(sh.getName() == 'Sheet1' && e.range.rowStart > 1 && e.range.rowStart < 12 && e.range.columnStart == 1 && e.value == "TRUE") {
e.range.setValue("FALSE");//reset
e.source.toast(e.range.getA1Notation());
}
}
Demo:

How to activate in any G Sheets cell a macro that will copy and paste value in the next one?

I am looking for a simple way with G Script to copy and paste value of any cell that is selected when the macro is launched - or even better, to copy and paste value of a cell defined by its position compared to the selected one when the macro is launched.
Just to make it a bit more explicit, here is my full need: I have two columns in a spreadsheet, one with tick boxes ("TRUE" or "FALSE") and I would like a macro to transform the formula in the next column same line into hard value (copy and paste value), whenever the box from the first column is ticked ("FALSE" to "TRUE").
I was trying sth like this until now, in order 1) to select current cell 2) go to next one on the right 3) copy and paste value from it:
function PasteValue() {
var spreadsheet = SpreadsheetApp.getActive();
var currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.RIGHT).activate();
currentCell.activateAsCurrentCell();
spreadsheet.getActiveRange().copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};
This is probably full nonsense.
You can do that with an onEdit(e) simple trigger, like this:
function onEdit(e) {
const checkboxColumn = 'Sheet1!C2:C';
const columnOffset = 1;
const checkboxes = SpreadsheetApp.getActive().getRange(checkboxColumn);
if (e.value !== 'TRUE'
|| e.range.columnStart !== checkboxes.getColumn()
|| e.range.rowEnd < checkboxes.getRow()
|| e.range.rowStart > checkboxes.getLastRow()
|| e.range.getSheet().getSheetId() !== checkboxes.getSheet().getSheetId()) {
return;
}
const cell = e.range.offset(0, columnOffset);
cell.setValue(cell.getValue());
}

CheckBox - Once Checked you cannot uncheck ? - GoogleAppsScript / GoogleSheets

Is it possible to code a checkbox that once checked it stays checked ?
My code would run again if a person checked - then unchecked - then accidently checked again, something I dont want to happen.
Here is the code
function onEdit(e) {
AddPalletCount(e)
AddCheckBox(e)
}
function AddCheckBox(e){
var row = e.range.getRow();
var col = e.range.getColumn();
if(col === 2 && row > 2 && e.source.getActiveSheet().getName() === "PREPSHEET"){
e.source.getActiveSheet().getRange(row,4).setValue(new Date());
e.source.getActiveSheet().getRange(row,3).setValue("Done");
var sheet = SpreadsheetApp.getActiveSheet();
var activeRange = sheet.getActiveRange();
var criteria = SpreadsheetApp.DataValidationCriteria.CHECKBOX;
var rule = SpreadsheetApp.newDataValidation().requireCheckbox().build();
var range = sheet.getRange(activeRange.getRow(), 5);
range.setDataValidation(rule);
}}
Thanks In Advance
A checkbox that stay's check once it's checked
It doesn't actually stay checked but once you check and try to uncheck it, it rechecks itself. It has to change in order to get the onEdit trigger.
Also note that this has to be an installable trigger because simple triggers can't do things that require permission like changing user data.
function onMyEdit(e) {
//e.source.toast('Entry');//debug
var sh=e.range.getSheet();
//var log=Utilities.formatString('oldValue: %s value: %s',e.oldValue,e.value);//debug
//sh.getRange(sh.getRange(1,9,sh.getLastRow(),1).getValues().filter(String).length+1,9).setValue(log);//debug
if(e.range.columnStart==2 && e.range.rowStart>2 && sh.getName()=='DONATIONS RECEIVED') {
//e.source.toast('Access');//debug
if(e.oldValue=='true' && e.value=='FALSE') {
//e.source.toast('SetValue');//debug
e.range.setValue("TRUE");
}
}
}
You might wish to keep lines 4 and 5 for future onEdit debugging work as I found them quite helpful in solving this problem.