Use Checkboxes as Radio Buttons with a FOR() loop

preview_player
Показать описание
Using a FOR() loop to make checkboxes behave as radio buttons.
Important notes:
DO NOT run the script manually, it will fail.
Make sure the array indexes and the Sheet rows match with some math.

Sheet (to copy - go to File, Make a copy)

Script:
function onEdit(e){
for (let i in checks){
checks[i][0] = false;
}
}

Connect with me:
• Twitter @FarrisSpencer
Рекомендации по теме
Комментарии
Автор

A very instructive script that made me wonder about my education.
Thank you!
The line turned out to be especially important:
if (checks[i][0] == true && +i != e.range.rowStart - 2)
I rewrote your version with = getActiveCell()

function onEdit(e) {
if (e.range.columnStart != 2 || e.range.rowStart == 1 || e.range.rowStart > 8 || e.value != "TRUE") return;
let sw =
let r = sw.getRange(2, 2, 8, 1);
let checks = r.getValues();
let activeCell = sw.getActiveCell().getRow();

for(let i = 2; i <= checks.length; i++){
if(i !== activeCell){
sw.getRange(i, 2).setValue("false")
}
}
}

TrenerZvezdnaia
Автор

That's very helpful, thank you so much, I was struggling to get it right as I have checkboxes lined in one row, but finally it works !
You made it easy even for someone who never edited a single line in a Apps script for Google Sheets (like me :) )

malikastar
Автор

Dear Spencer, thank you very much for sharing your coding which was very helpful for me . The point is I have three columns of checkboxes ( Col B, Col E, Col J) so please let me know what should be changed so I could use Checkboxes as Radio Buttons in all three columns.

mohammadyazdani
Автор

Hello. Tell me how to write the code if the checkboxes are located, for example, horizontally and not vertically. Thank you in advance!

iconcept
Автор

I have an error message like this Cannot read property 'range' of undefined

joseguzman
Автор

It worked. But if you have another check box list with another header in the same column it interferes. How to separate these checkboxes below the same line to be independent?

lucianopessanha
Автор

This is my attempt to apply to my google sheets as opposed to vertical radio buttons
My google sheets need horizontal radio buttons but it still doesnt work

Please help, What did I do wrong??

function onEdit(e){
if (e.range.rowStart != 2 || e.range.columnStart == 2 || e.range.columnStart > 6 || e.value != "TRUE") return;
let r = SpreadsheetApp.getActive().getActiveSheet().getRange(2, 2, 1, 5);
let checks = r.getValues();
for (let i in checks){
if (checks[i][0] == true && +i != e.range.columnStart - 2)
checks[i][0] = false;
}
r.setValues(checks);
}

jukschannel