1

I am trying to get the selected radio button and push it into another script via html service. However I do not know how to push the selected radio button in google script.

Index.html

<div>
<?
var attrs = getAllDrawings();
for(i=0; i<attrs.length;i++){
?>
<INPUT TYPE="Radio" Name="Drawing" Value= "<?= attrs[i] ?> "> <?= attrs[i] ?> <br>
<? } ?>   

<input type="submit" value="submit"
onclick="google.script.run.getDrawing(result)" />   

<input type="button" value="Close"
onclick="google.script.host.close()" />
</div>

What do I use instead of result in google.script.run.getDrawing(result) so I can run a function based on it

function getDrawing(e) {
  Logger.log(e);
}

I am creating an html template with a list of files in a folder. The user then selects the needed file, and I need to return the URL for that file to the user. However I do not know how to return the file that the user has selected. So I need to get the selected radio button value. For example if radios are Spreadsheet1, Spreadsheet2, Spreadsheet3 and Spreadsheet4 and 3rd one was selected which is Spreadsheet3 how can I run getDrawing("Spreadsheet3").

Mu problem is similar to this SO problem, but I am using HTML instead of UIApp.

Edit:

This is how I call Index.html

function demoHtmlServices() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var html = HtmlService.createTemplateFromFile('Index').evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
  ss.show(html);
}
Community
  • 1
  • 1
Akshin Jalilov
  • 1,658
  • 1
  • 12
  • 12

2 Answers2

1

In order to get the form(html template) responses, you can use the withSuccessHandler(function) and pass the form values this way:

<input type="button" value="Submit" 
 onClick="google.script.run.withSuccessHandler(DataSaved).processForm(this.form)"/>

DataSaved is a script function to display a success message. Also processForm is a Apps script function to work with the form responses. You can check this page and this page for more details on how you can achieve this. Hope that helps!

Community
  • 1
  • 1
KRR
  • 4,647
  • 2
  • 14
  • 14
1

You can access the result of your radio buttons (or any other input element) via HTML, without needing to write any additional Google Apps Script code. You need to have the elements inside a named element, and can then navigate to them beginning at document. For example, document.drawings.Drawing.value will show the value of the Drawing radio buttons of the drawings form.

You can leave the submit button outside of the form element that contains the input elements you want to access.

<form id="drawings" name="drawings">
<?
var attrs = getAllDrawings();
for(i=0; i<attrs.length;i++){
?>
<input type="radio" name="Drawing" value= "<?= attrs[i] ?>"> <?= attrs[i] ?> <br>
<? } ?>   
</form>

<input type="submit" value="submit"
onclick="google.script.run.getDrawing(document.drawings.Drawing.value)" />   

<input type="button" value="Close"
onclick="google.script.host.close()" />
Mogsdad
  • 44,709
  • 21
  • 151
  • 275