I tried to use app script to let people to fill google spreadsheet with an html page. But after I enter data on html page, the data did not show up on my target spreadsheet.
There is no problem when I execute function on code.gs. The log show up normally. And there is no problem when I use alert to test script on html. But while I tried to pass parameter from html to code.gs through js. It not work.
code.gs:
function doGet(event) {
// Logger.log(event);
return HtmlService.createHtmlOutputFromFile("page");
}
function GG(name, email, link) {
var url = "spreadsheet url";
var ss = SpreadsheetApp.openByUrl(url);
var ws = ss.getSheetByName("Data");
ws.appendRow([new Date(), name, email, link]);
Logger.log("GG function is work")
}
function WTF() {
Logger.log("WTF !!!");
}
page.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<base target="_top">
</head>
<body>
<div class="container">
<form>
<div class="form-group">
<label for="email">Name:</label>
<input type="text" class="form-control" id="username">
</div>
<br>
<div class="form-group">
<label for="email">Email:</label>
<input type="text" class="form-control" id="email">
</div>
<div class="form-group">
<label for="link">Link:</label>
<input type="url" class="form-control" id="link">
</div>
<button class="btn btn-primary" id="submit">Submit</button>
</form>
</div>
<script>
document.getElementById("submit").addEventListener("click",fn);
function lol() {
google.script.run.WTF();
}
function fn() {
var uname = document.getElementById("username").value;
var email = document.getElementById("email").value;
var link = document.getElementById("link").value;
google.script.run.GG(uname,email,link);
}
</script>
</body>
</html>
It works fine when I change document.getElementById("submit").addEventListener("click",fn);
to document.getElementById("submit").addEventListener("click",lol);
The log shows up as expected, but GG not work. The sheet did not add any data as expected unless I execute it through "Run > Run function > GG" on app script page, it will show "undefined" on each cell for data.
resaonofdocument.getElementById("resaon").value. Whenfn()is run under this condition,document.getElementById("resaon")becomesnull, and an error occurs. By this,google.script.run.GG(uname,reason,link)doesn't work. For example, as a test case, how about modifying todocument.getElementById("email").value? I thought that this issue might be have been modified in your actual script. So I posted this as a comment. If I misunderstood your question, I apologize. - TanaikeGG()is run bygoogle.script.run.GG(uname,reason,link), ifurlandDataof the sheet name are correct, the values ofnew Date(), name, email, linkare appended to the sheet. How about this? If it doesn't work, can I ask you about the settings of Web Apps you deployed? - Tanaike