只有一个TRUE复选框 [英] Only one TRUE checkbox
问题描述
我有一列复选框:
。
如果选中一个框,它将为另一个工作表中的单元格设置一个值。
If a box is checked it sets a value to a cell in another sheet.
如果我勾选1号框,则它变为true,而其余的仍为假
,然后,如果我勾选2号框,则它也变为true,并且框号为1,其余的仍为false。这是正常的操作,但是我需要,当我选中一个框时,它变为true,而其他所有框都变为false,则选中或不选中它们。换句话说,我希望一次选中一个框。
If I check box no.1 ,it turns true and the remaining still false then if I check box no.2 it also turns true long with box no.1 and the remaining still false. This is the normal operation but I need that, when I check a box it turns true and all the other boxes turn false, either they are checked or not.In other words, I want one box to be checked at a time.
我可以这样做吗?
如果选中此框,这是我的代码来设置值:
This is my code to set a value if the box is checked:
var hasValue = sheet.getRange("B2:B").getValues();
for (var i = 0; i < hasValue.length; i++) {
if (hasValue[i][0] == true) {
var transfer = sheet2.getRange(2, 2, 1, 1).setValue(i + 1);
}
}
推荐答案
此这种行为被称为单选按钮。
This kind of behavior is known as a "radio button".
最简单的实现方法是绑定简单的编辑触发器:
The simplest method to achieve it is to bind the simple edit trigger:
- 检查已编辑的范围,以确定它是否位于您的复选框区域,如果没有,则退出。
- 将所有复选框设置为false
- 将编辑后的单元格设置为事件对象中的适当值
- 如果需要,请执行更新
- inspect the edited range to determine if it was to your checkbox region and quit if not.
- set all checkboxes to false
- set the edited cell to the appropriate value from the event object
- if required, perform the update
必须配置的极小样本,并且仅配置用于单单元格编辑。
An extremely minimal sample which you will have to configure, and which is only configured for single-cell edits.
function onEdit(e) {
if (!e || e.value === undefined)
return; // The function was run from the Script Editor, or a multi-cell range was edited.
const edited = e.range;
const s = edited.getSheet();
if (s.getName() !== "some name")
return; // A cell on the wrong sheet was edited
if (isCheckboxRow_(edited.getRow()) && isCheckboxCol_(edited.getColumn())) {
// The cell edited was in a row and a column that contains a checkbox
updateCheckboxes_(s, edited, e);
}
}
function isCheckboxRow_(row) {
// Assumes checkboxes are only in rows 5, 6, 7, 8, 9, and 10
return row >= 5 && row <= 10;
}
function isCheckboxCol_(col) {
// Assumes checkboxes are in column A
return col === 1;
}
function updateCheckboxes_(sheet, editRange, eventObject) {
if (!sheet || !edit || !eventObject)
return; // Make sure all required arguments are defined (i.e. this was called and not run from the Script Editor)
const cbRange = sheet.getRange("A5:A10"); // location of the checkboxes in a radio group.
cbRange.setValue(false);
editRange.setValue(eventObject.value);
// Reference some other sheet
const targetSheet = eventObject.source.getSheetByName("some other sheet name")
if (!targetSheet)
return; // the sheet name didn't exist in the workbook we edited.
// Reference a cell in the same row as the cell we edited, in column 1
const targetCell = targetSheet.getRange(editRange.getRow(), 1);
if (eventObject.value) {
// when true, give the target cell the value of the cell next to the edited checkbox
targetCell.setValue(editRange.offset(0, 1).getValue());
// do other stuff that should be done when a checkbox is made true
} else {
// the checkbox was toggled to false, so clear the target cell
targetCell.clear();
// do other stuff that should be done when a checkbox is made false
}
}
上面暗示了一些建议的做法,例如使用辅助函数封装和抽象逻辑,从而使函数更易于理解。
The above hints at some suggested practices, such as using helper functions to encapsulate and abstract logic, resulting in easier to understand functions.
:
- Simple Triggers
- Event Objects
- Spreadsheet Service
这篇关于只有一个TRUE复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!