This sample data entry web app works as expected and appends new data to the spreadsheet using the id. With minimal changes, how can the script from the HTML file be placed into a different file while keeping the same functionality?
The code.js file:
const id = "#####";
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('WebApp');
}
function AddRecord(firstname, lastname) {
var sheet = SpreadsheetApp.openById(id).getSheetByName("alpha");
sheet.appendRow([firstname, lastname, new Date()]);
}
existing WebApp.html which works:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function AddRow()
{
var firstname = document.getElementById("firstname").value;
var lastname = document.getElementById("lastname").value;
google.script.run.AddRecord(firstname, lastname);
document.getElementById("firstname").value = '';
document.getElementById("lastname").value = '';
}
</script>
</head>
<body>
First Name:<input type="text" id="firstname" />
Second Name:<input type="text" id="lastname" />
<input type="button" value="Add" onclick="AddRow()" />
</body>
</html>
Pulling out just the script and putting in a different file:
<script>
function AddRow()
{
var firstname = document.getElementById("firstname").value;
var lastname = document.getElementById("lastname").value;
google.script.run.AddRecord(firstname, lastname);
document.getElementById("firstname").value = '';
document.getElementById("lastname").value = '';
}
</script>
How can the above script be inserted, appended, or included back into the WebApps.html file?
Undoubtedly there’s a term to succinctly describe this process.
2
Answers
Put this:
into somename.html
Then change WebApp.html to:
Add this function to one of your first gs script files
You can just move the javascript code to another file (a .js file) without the script tags. You only need to add a source property to the script tag in the html file.
The seperate file will be: (without the <script> tags)
(Call it another_file.js for now)
And the html file will become: