简体   繁体   English

如何在 Google 表格中创建一个按钮来切换目标单元格中的值 1 和 0?

[英]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.我相信你的目标如下。

  • You want to switch the number of 1 and 0 at the cell "A1" on the active sheet by clicking a button.您想通过单击按钮在活动工作表上的单元格“A1”中切换10的数量。

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?那么下面的示例脚本怎么样?

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);
  }
}
  • In this case, when the cell "A1" is not 1 or 0 , the value in the cell is not modified.在这种情况下,当单元格“A1”不是10时,单元格中的值不会被修改。

References:参考:

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.

相关问题 用于打开/关闭一排复选框的 Google 表格按钮 - Google Sheets button that toggles a row of checkboxes on/off 如何为每个用户制作一个单独的按钮,以便在按下它时切换 isFollowingUser Bool 并更改为关注? - How can i make an individual button for each user so when its pressed it toggles the isFollowingUser Bool and changes to following? 如何在 google docs 按钮中换行? - How can I make a line break in a google docs button? 如何制作一个按钮以跳到Google Spreadsheet中另一个工作表中的特定单元格? - How to make a button to jump to a specific cell in another sheet in Google Spreadsheet? 如何在按钮内垂直对齐Google图标? (IE11) - how can I vertically align a google icon within a button? (ie11) 如何创建一个在反应时切换打开模式的按钮 - How to create a button that toggles open modal on react 单击按钮时如何制作浏览窗口以选择目的地 - How to make a browse window for selecting destination when button is clicked 如何使此删除按钮起作用? - How can I make this delete button work? 我怎样才能使按钮盒连接? - How can I make the button boxes connect? 我如何为按钮制作固定尺寸 - How can i make a fixed size for button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM