Abhinav Jain
Abhinav Jain

Reputation: 33

Google Apps Script Client To Server

This code is not working properly when I can run this code as a web app, first, I deploy this program as a web app then when I was run the URL it works perfectly but when I submit some input my program log shows me "null".

[20-05-05 06:48:54:864 PDT] null

My program is :

code.gs

function doGet(e) {
 Logger.log(e.parameter);
 return HtmlService.createHtmlOutputFromFile("page");
}

function userClicked(name){
 Logger.log(name);
}

page.html

<!DOCTYPE html>
<html>
 <head>
  <base target="_top">
 </head>
 <body>
  <input type="text" id="username">
  <button id="btn">Submit</button>

 </body>

 <script>
  document.getElementById("btn").addEventListener("click", doStuff);
  function doStuff() {
   var uname = document.getElementById("username").value;
   google.script.run.userClicked(uname);
   document.getElementById("username").value="";
  }
 </script>
</html>

Upvotes: 0

Views: 40

Answers (1)

Cooper
Cooper

Reputation: 64082

This works:

The problem was that your javascript was in the wrong place. It came after the body rather than in the body. So I just moved the </body> tag and it works okay now.

function showMyDiaog() {
  const html='<!DOCTYPE html><html> <head>  <base target="_top"> </head> <body>  <input type="text" id="username">  <button id="btn">Submit</button> <script>  document.getElementById("btn").addEventListener("click",doStuff);  function doStuff() {   var uname = document.getElementById("username").value;   google.script.run.userClicked(uname);   document.getElementById("username").value="";  } </script></body></html>';
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), "Test")
}

function userClicked(name){
 Logger.log(name);
}

function doGet(e) {
  Logger.log(JSON.stringify(e));
  const html='<!DOCTYPE html><html> <head>  <base target="_top"> </head> <body>  <input type="text" id="username">  <button id="btn">Submit</button> <script>  document.getElementById("btn").addEventListener("click",doStuff);  function doStuff() {   var uname = document.getElementById("username").value;   google.script.run.userClicked(uname);   document.getElementById("username").value="";  } </script></body></html>';
  return HtmlService.createHtmlOutput(html);
}

Upvotes: 1

Related Questions