如何在 Google Apps Script 上只选中一个而不是两个? [英] How to have only one box checked instead of two on Google Apps Script?

查看:27
本文介绍了如何在 Google Apps Script 上只选中一个而不是两个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[1];
  if (sheet.getActiveCell() = "E11"){
    sheet.getRange('E12').activate();
    sheet.getCurrentCell().setValue('FALSE');
    sheet.getRange('E13').activate();
    sheet.getCurrentCell().setValue('FALSE');
    sheet.getRange('E14').activate();
    sheet.getCurrentCell().setValue('FALSE');
  }
  if (sheet.getActiveCell() = "E12"){
    sheet.getRange('E11').activate();
    sheet.getCurrentCell().setValue('FALSE');
    sheet.getRange('E13').activate();
    sheet.getCurrentCell().setValue('FALSE');
    sheet.getRange('E14').activate();
    sheet.getCurrentCell().setValue('FALSE');
  }
}

所以我想这样做,如果输入的是 TRUE 语句,则另一个复选框将为 FALSE,反之亦然.

So I want to make it so that if a TRUE statement is inputted, the other checkbox will be FALSE and viceversa.

推荐答案

GAS 官方文档中有很多有用的例子,遇到困难时请参考.下面的示例可能会对您有所帮助.这适用于第 1 列中两个复选框直接位于彼此下方(第 1 行和第 2 行)的情况.

The official GAS documentation contains lots of useful examples, so please refer to it whenever you are stuck. The example below might help you. This is for the scenario where both checkboxes are directly below each other (rows 1 & 2) in column 1.

function onEdit(e){

  //Checkbox coordinates. 
  var checkboxColumn = 1;
  var checkboxRows = [1, 2];
  var sheetName = "YOUR_SHEET_NAME";

  //Get the edited cell value 
  var value = e.value;

  //Get the old value
  var oldValue = e.oldValue;

  var editedRange = e.range;
  var editedSheet = editedRange.getSheet();

  if (editedSheet.getName() == sheetName && editedRange.getColumn() == checkboxColumn && checkboxRows.indexOf(editedRange.getRow()) != -1) {  

    //Get the row coordinate of the other checkbox
    var checkboxRow = checkboxRows.filter(function(rowNum) { return rowNum != editedRange.getRow();})[0];

    //set its value to the old value of the edited checkbox
    var range = editedSheet.getRange(checkboxRow, checkboxColumn).setValue(oldValue);

  }

}

这篇关于如何在 Google Apps Script 上只选中一个而不是两个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆