You'll find most of your answer by reading Html Service - Google Apps Script. The way you run it is to create a published script-as-web-app.
The Html Service is one option, but you could also use the UiApp Service - take a look at Call a Google Apps Script function in HTML for an example that uses that approach instead.
With the following code in myFunction()
, you are modifying the content (innerHTML
) of an element named result
. That's how you will dynamically change the displayed page.
var b = document.getElementById('result');
b.innerHTML = 'You pressed the button.';
Problem is, you don't have such an element. Let's add one, a <div>
. Just to make the effect of myFunction()
really obvious, we'll start with some content, but that's optional. The important bit is that we provide an id for the element, "result" in this case, to match the id already in the function.
<div id="result">You haven't pressed the button yet.</div>
Here's the final code. In addition to the "result" change, the <script>
block moved inside the <html>
tags, and an id was added to your button allowing it to be disabled after clicking. As shown in the screenshot, you will have two files in the editor.
Code.gs
Not much to this, it's exactly as described in Creating HTML Files
function doGet() {
return HtmlService.createHtmlOutputFromFile('myPage');
}
myPage.html
Again, start with the examples from Html Service. Note that .createHtmlOutputFromFile('myPage')
in the doGet()
contains the name of the html file in your script. (If I had a dollar for every time I screwed that up because of cut & paste, I'd have two dollars.)
<html>
<script>
function myFunction() {
var b = document.getElementById('result');
b.innerHTML = 'You pressed the button.';
document.getElementById('button1').disabled = 'disabled';
//alert('The output has been modified');
return;
}
</script>
<body>
<button onclick='myFunction()' id='button1'>Press this</button>
<div id="result">You haven't pressed the button yet.</div>
</body>
</html>