The foo variable is sent from the client side HTML to Code.js as demonstrated by the logs. When this occurs, the bar key from PropertiesService is set and logged.
How is PropertiesServices accessed from the client side HTML and output as text?
logs:
Cloud logs
Oct 15, 2022, 5:27:14 PM
Info
logProperties..
Oct 15, 2022, 5:27:14 PM
Info
bar
Oct 15, 2022, 5:27:14 PM
Info
foo
Oct 15, 2022, 5:27:14 PM
Info
sending foo
html:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function sendFoo()
{
var foo = document.getElementById("foo").value;
google.script.run.setBar(foo);
document.getElementById("foo").value = '';
}
</script>
</head>
<body>
foo is:<input type="text" id="foo" />
<input type="button" value="sendFoo" onclick="sendFoo()" /> <br> <br> <br>
bar is:
</body>
</html>
I believe this can be accomplished with span tag.
code:
function setBar(foo) {
PropertiesService.getScriptProperties().setProperty("foo",foo);
PropertiesService.getScriptProperties().setProperty("bar","foo received");
logProperties();
}
function initProperties() {
PropertiesService.getScriptProperties().deleteAllProperties();
}
function logProperties() {
Logger.log("logProperties..");
var keys = PropertiesService.getScriptProperties().getKeys();
keys.forEach(key => {
Logger.log(key);
Logger.log(PropertiesService.getScriptProperties().getProperty(key));
});
}
function getUrl() {
var url = ScriptApp.getService().getUrl();
return url;
}
function doGet(e) {
initProperties();
var htmlOutput = HtmlService.createTemplateFromFile('WebApp');
htmlOutput.url = getUrl();
return htmlOutput.evaluate();
}
This is just within Google Apps Script and has been kept as simple as possible, but no simpler.
see also:
https://codewithcurt.com/how-to-call-google-apps-script-function-from-web-app/