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

前端 未结 2 2049
暗喜
暗喜 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

    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

提交回复
热议问题