I'm not a developer, so please be kind. I have used Google Apps Script to create a Web App for the charity I work for.
At the top of the page, there is a set of menu options.
Rather than add these manually to each page, I have created a Header.html file and then used an include()
function on each page to include the header.
function include(filename)
{
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
This is fine, except that the links in the Header.html are all relative links to load other html pages within the web app using the doGet() function to serve a new HTML page, e.g.
<a href="https://script.google.com/macros/s/{deployment ID}/exec?logout=1">Logout </a>
As a result, every time I deploy the app, I have to then manually update the links in the Header.html file and redeploy.
I tried using ScriptApp.getService().getUrl()
, but this will not work via the include()
function as it is just inserted as text.
What is the best way to have a header that gives the same set of links on each page but where the link is generated programmatically to have the current deployment ID in the URL?
EDIT: Code requested by @Carlos M
Code.gs:
function doGet(e){
var cache = CacheService.getUserCache()
.
.
.
//============================
// LOGOUT
//============================
if(e.parameter.logout)
{
cache.removeAll(["email", "name", "code", "loggedIn", "month"])
}
.
.
.
}
function include(filename)
{
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
function getUrl()
{
var url = ScriptApp.getService().getUrl();
return url;
}
Index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<?!= include('Stylesheet'); ?>
</head>
<body>
<?var url = getUrl() ?>
<?!= include('Header'); ?>
<div>
<h1>Membership Duty Booking</h1>
<span id="message"><?=message ?></span>
<p class="bold">Select a Month</p>
.
.
.
</body>
</html>
Header.html
<div>
[MULTIPLE SIMILAR ENTRIES FOR "MENU" OPTIONS]
<span class="uHeader">
<a href="https://script.google.com/macros/s/{Deployment ID}/exec?logout=1">Logout </a>
</span>
</div>
It is the {Deployment ID} in the Header.html file that I need to update. As you can see, I am already using the method suggested by @MetaMan below, but it will not work for an HTML file that is added using the include()
function. It works elsewhere, but not for this scenario. This is what I am trying to resolve :)