How to have only one box checked instead of two on Google Apps Script?

前端 未结 2 2046
暗喜
暗喜 2021-01-24 07:20
function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[1];
  if (sheet.getActiveCell() = \"E11\"){
    sheet.getRange(\'E12\'         


        
2条回答
  •  醉酒成梦
    2021-01-24 07:59

    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);
    
      }
    
    }
    

提交回复
热议问题