user2425
user2425

Reputation: 113

Passing answers from a Dialog to a JavaScript

I'm making a spreadsheet. I want it to have a button (I have already done this). Clicking this button should open a dialog with an editable numeric field, two checkboxes, and OK, Cancel buttons.

Of course, when the OK button is pressed, some things should happen in the spreadsheet, that is, I want pass the information that the user entered in the dialog to the script that opened the dialog.

I have been looking in GAS guides and reference and I found nothing (the example in the guide has only a button to close the dialog: no information is passed). A complete example would be most helpful.

EDIT: Sorry, I'm new here, and I thought that this StackOverFlow page were more specific. My question is about Google Apps Script, and the spreadsheet I'm making is a Google Spreadsheet.

Upvotes: 1

Views: 48

Answers (1)

Piyin
Piyin

Reputation: 1834

Ok, so I assume you already have at least the Code.gs and Index.html from the example. What you should do is define your custom handlers in your Code.gs file to handle the data you send from the HTML Service

To make it easy, we'll suppose all you want to do is insert a new row with some text. So we'll start by creating the handler:

// Code.gs
function createNewRow(text) {
  SpreadsheetApp.getActiveSheet().appendRow(['New row', text]);
}

Then, put some custom HTML elements and the Javascript code to make the link:

<!-- Index.html -->
<input type="text" id="text" />
<input type="button" value="Insert new row" onclick="insertNewRow()" />
<script>
function insertNewRow(){
  // Calls our handler with the value of the input with id "text"
  google.script.run.createNewRow(document.querySelector('#text').value);
}
</script>

That would be all. I hope it's an easy enough example for you to follow

Upvotes: 1

Related Questions