doGet with pages split into files
index.html pulls in css/app.html and js/app.html with include(); works on phones and inside an iframe.
File Code.gs
function doGet(e) {
const page = HtmlService.createTemplateFromFile('index');
page.appUrl = ScriptApp.getService().getUrl();
page.params = (e && e.parameter) || {};
return page.evaluate()
.setTitle('My app')
.addMetaTag('viewport', 'width=device-width, initial-scale=1')
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
/** <?!= include('css/app') ?> in a page puts that file there (its own <? ?> run too). */
function include(name) {
return HtmlService.createTemplateFromFile(name).evaluate().getContent();
}
File index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<?!= include('css/app') ?>
</head>
<body>
<main id="app">Loading…</main>
<?!= include('js/app') ?>
</body>
</html>
Good to know
- setXFrameOptionsMode(ALLOWALL) lets the app open inside another page (a public link, Google Sites).
- Never write the /dev or /exec address in the code: ScriptApp.getService().getUrl() gives the right one.
Let the AI put it in your appGAS AI Builder knows every recipe here. Describe your app and it uses this code where it fits, in a Google Apps Script project in your own Drive.
Use it in a projectMore in Web app
Who is using the app, and their roleReads the visitor's email and looks up their role in a Users tab; server functions check it before doing anything.Call the server from the page with async/awaitA small helper turns google.script.run into a promise, with errors shown instead of lost.Return data the page can readTurns dates into text and drops values the page can't receive, so a result never arrives as null.Upload a file from the page to DriveThe page reads the chosen file as base64 and sends it; the server saves it in a folder and returns its link.