Give Checkboxes Radio Group Behavior
This onEdit function provide radio button group behavior for any range. You are required to enter the range in A1 Notation by setting the value of cbrg and selecting which direction you want the groups to be in either row or col. It will turn all the other cells in the row or column that you select by clicking it to true. It doesn't support validation values other than true or false.
function onEdit(e) {
//e.source.toast('Entry');
const sh = e.range.getSheet();
const group = 'col';//change which direction that you wish to group in
const cbrg = "C4:D5";//specifying the range the starting column and row must be greater than one.
const rg = sh.getRange(cbrg);
const w = rg.getWidth();
const h = rg.getHeight();
const cs = rg.getColumn();
const ce = cs + w - 1;
const rs = rg.getRow();
const re = rs + h - 1;
//console.log('rs:%s,re:%s,cs:%s,ce:%s', rs, re, cs, ce);
if (sh.getName() == 'Sheet1' && e.range.columnStart > cs - 1 && e.range.columnStart < ce + 1 && e.range.rowStart > rs - 1 && e.range.rowStart < re + 1 && e.value == 'TRUE') {
if (group == 'row') {
//e.source.toast('row');
let cA = new Array(w).fill(false);
cA[e.range.columnStart - cs] = true;
sh.getRange(e.range.rowStart, cs, 1, cA.length).setValues([cA]);
}
if (group == 'col') {
//e.source.toast('col');
let idx = e.range.rowStart - rs;
let cA = [];
for (let i = 0; i < h; i++) {
if (i == idx) cA.push([true]);
else cA.push([false]);
}
sh.getRange(rs, e.range.columnStart, cA.length, 1).setValues(cA);
}
}
}
The one below performs a little faster and is fairly easy to just set the top row (trow), bottom row (brow), left column (lcol), right col (rcol) and the radio group direction 'row' or 'col'. It requires quite few less function calls which are time consuming.
function onEdit(e) {
//e.source.toast('Entry');
const sh = e.range.getSheet();
const trow=4;//top row
const brow=5;//bottom row
const lcol=3;//left column
const rcol=4;//right column
const group = 'row';//change which direction that you wish to group in
//console.log('trow:%s,brow:%s,lcol:%s,rcol:%s', trow, brow, lcol, rcol);
if (sh.getName() == 'Sheet1' && e.range.columnStart >= lcol && e.range.columnStart <= rcol && e.range.rowStart >= trow && e.range.rowStart <= brow && e.value == 'TRUE') {
if (group == 'row') {
//e.source.toast('row');
let cA = new Array(rcol-lcol+1).fill(false);
cA[e.range.columnStart - lcol] = true;
sh.getRange(e.range.rowStart, lcol, 1, cA.length).setValues([cA]);
}
if (group == 'col') {
//e.source.toast('col');
let idx = e.range.rowStart - trow;
let cA = [];
for (let i = 0; i < brow-trow+1; i++) {
if (i == idx) cA.push([true]);
else cA.push([false]);
}
sh.getRange(trow, e.range.columnStart, cA.length, 1).setValues(cA);
}
}
}
Animation:
I changed the range on the animation so I could fit it into the smallest image possible.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…