Is there a way to use radio buttons in a Google Sheets Dialog and save the value, using Google App Scripts?

前端 未结 3 1252
没有蜡笔的小新
没有蜡笔的小新 2021-01-26 20:41

My goal, simply put, is to highlight a cell range on one google sheet (the \'source\') and move it to another, different google sheet document (the \'target\'). The Target has

3条回答
  •  情深已故
    2021-01-26 21:21

    I was able to use the Google Sheets checkboxes with the below script to create a radio button effect (still looks like a checkbox, but when one checkbox in a given row is checked, it unchecks any other checkbox in that row (columns B-E) that had previously been checked).

    My checkboxes are in columns B,C,D,E and I believe (I am learning as I go, so I'm not 100% sure) that is what "for(var i = 2;i<6;i++)" refers to, with Column B=2 and Column E=5 (i.e. <6). In my testing, this meant that in any given row of my spreadsheet, if one checkbox is checked, any checkboxes in columns B-E would be unchecked. This works perfectly for my purposes, as my only checkboxes are in columns B-E, but I did experiment and found that if I added a checkbox in column A, for example, it would cause checkboxes in B-E to be unchecked, but not vice-versa (i.e. checking a box in columns B-E did not uncheck the one in A). This didn't matter for my purposes since I don't need checkboxes anywhere else, so I didn't troubleshoot it, but obviously some additional code would be needed if you don't want this to happen.

       function onEdit(evt) {
    
        var range = evt.range;
        var val = range.getValue();
        var row = range.getRow();
        var col = range.getColumn();
    
        // -------------------------------------
        // --- Only 1 checkbox per row can be ticked ---
        // -------------------------------------
      for(var i = 2;i<6;i++){
        if(i == col) continue;
    
            if (val) {
                SpreadsheetApp.getActiveSheet().getRange(row,i).setValue('FALSE');    
    }
        }}
    

提交回复
热议问题