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

一曲冷凌霜 提交于 2019-12-20 06:19:10

问题


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 multiple sheets(tabs?) and new ones are added often.

IMPORTANT: To be clear, I am NOT copying from one sheet to another sheet, in the same document. I am copying from one google sheet document, to a complexity different google sheet document.

Currently, I have code that allows users to:

  1. Highlight a cell range in the SOURCE google sheet.
  2. Select the custom menu option.
  3. Upon selecting the custom menu option, the data in the highlighted cell range is copied to the TARGET google sheet.

Currently, the the "Target" is hardcoded.

What I want to do is to build a Radio Button list. One Radio Button for every Sheet that exists in the TARGET google sheet. Allow the user to choose one, then move the data to that specific Sheet, when they press the "OK" button.

I've searched for a while, but I'm unable to find anything about even putting radio buttons in a google sheets dialog, let alone build the radio button list based on sheets that exist in another google sheet document.


回答1:


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



回答2:


Here's a simple example of a radio dialog:

function radiosOnADialog() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var html='<html><head><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"><script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script></head>';
  for(var i=0;i<10;i++) {
    html+=Utilities.formatString('<br /><input type="radio" name="rgroup" id="%s" value="%s" onChange="getSelected();" />%s','id'+ Number(i+1),'radio' + Number(i+1),' radio' + Number(i+1));
  }
  html+='<body><br /><input type="button" value="close" onClick="google.script.host.close()" /><div id="msgdiv"></div>';
  html+='<script>function getSelected(){ var selected=document.querySelector(\'input[name="rgroup"]:checked\').value;document.getElementById("msgdiv").innerHTML="<br />You selected " + selected  + ".<br />";}</script></body></html>'; 
  var ui=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModelessDialog(ui, 'A Radio Dialog');
}
  • Radio Buttons

The Dialog:




回答3:


Closing this question, as I now know HTML needs to be used.



来源:https://stackoverflow.com/questions/55679010/is-there-a-way-to-use-radio-buttons-in-a-google-sheets-dialog-and-save-the-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!