[英]How can I make a button within Google Sheets that toggles the values 1 and 0 in a destination cell?
I am trying to learn to do basic buttons within a spreadsheet to help with a project at school.我正在尝试学习在电子表格中使用基本按钮来帮助学校的一个项目。 I am a simple teacher that is trying to find resources to help.我是一个简单的老师,正在努力寻找帮助的资源。 I have learned how to create a button that adds one or subtracts one with the value which will allow me to do what I need to do, but ideally I am looking for script code to make a button that would toggle between the values of 1 and 0 upon pressing the button.我已经学会了如何创建一个按钮,该按钮将允许我做我需要做的事情的值加一或减一,但理想情况下,我正在寻找脚本代码来制作一个可以在值之间切换的按钮 1 和0 按下按钮。
Thanks for any help.谢谢你的帮助。
function plus1() {
SpreadsheetApp.getActiveSheet().getRange('A1').setValue(
SpreadsheetApp.getActiveSheet().getRange('A1').getValue() + 1
);
}
I believe your goal as follows.我相信你的目标如下。
1
and 0
at the cell "A1" on the active sheet by clicking a button.您想通过单击按钮在活动工作表上的单元格“A1”中切换1
和0
的数量。In this case, at first, it is required to retrieve the value from the cell "A1" on the active sheet.在这种情况下,首先需要从活动工作表上的单元格“A1”中检索值。 And, the value is put by checking the retrieved value.并且,通过检查检索到的值来放置值。 So how about the following sample script?那么下面的示例脚本怎么样?
function sample() {
var range = SpreadsheetApp.getActiveSheet().getRange('A1');
var value = range.getValue();
if (value == 1) {
range.setValue(0);
} else if (value == 0) {
range.setValue(1);
}
}
1
or 0
, the value in the cell is not modified.在这种情况下,当单元格“A1”不是1
或0
时,单元格中的值不会被修改。function toggleActiveCell() {
const sh=SpreadsheetApp.getActiveSheet();
const rg=sh.getActiveCell();
rg.setValue(rg.getValue()?0:1);
}
function toggleUniqueCell() {
const sh=SpreadsheetApp.getActiveSheet();
const rg=sh.getRange(row,col);
rg.setValue(rg.getValue()?0:1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.