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

前端 未结 2 2008
暗喜
暗喜 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);
    
      }
    
    }
    
    0 讨论(0)
  • 2021-01-24 07:59

    Here's an alternative implementation of @Anton's answer that uses the RangeList class to support arbitrarily oriented "radio button"-grouped checkboxes, provided they're all on the same worksheet (the RangeList range sources must be on the same worksheet).

    function onEdit(e) {
      const sheetName = 'some sheet name',
          rbLocations = [
            {r: 1, c: 1}, // A1
            {r: 2, c: 4}, // D2
            {c: 1, r: 8}, // A8
            ...
          ];
    
      if (!e) return; // only run for sheet edits.
    
      const toggle = e.oldValue;
      if (toggle === undefined)
        return; // require edit to be of a single cell.
    
      const sheet = edited.getSheet();
      if (sheet.getName() !== sheetName)
        return; // require edit to be of the desired sheet.
    
      const edited = e.range,
          eRow = edited.getRow(),
          eCol = edited.getColumn(),
          otherRBs = rbLocations.filter(
            function (loc) {
              return loc.r !== eRow || loc.c !== eCol;
            }
          ).map(
            function (l) {
              // Create an R1C1 notation string.
              return "R" + l.r + "C" + l.c; 
            }
          );
    
      // Acquire and use a RangeList to set the same value to all the other checkboxes.
      sheet.getRangeList(otherRBs).setValue(toggle);
    }
    

    References

    • Array#filter
    • Array#map
    • Sheet#getRangeList
    • Event objects - edit
    0 讨论(0)
提交回复
热议问题