In a Google Apps Script web app, the index.html
page is publicly viewable. The database sheet can be shared privately with a few individuals, but the webapp is accessible accessible according to the publishing permissions set.
Mogsdad posted a method of displaying multiple HTML pages in an Apps Script web app using a URL query.
These pages appear to be viewable only by editors of the sheet. So, I'm trying to find a way to display different HTML pages to users who are not editors on the Google Sheet. Any ideas?
Here's an example:
First, check if the user is an administrator from an array in the Sheet.
// Client-side call (index.html)
google.script.run.withSuccessHandler(showAdmin).checkAdmin();
// server-side GAS code to evaluate
function checkAdmin() {
var user = Session.getActiveUser().getEmail();
var adminSheet = ss.getSheetByName('admins');
var adminData = adminSheet.getRange(1,5,adminSheet.getLastRow(),1).getValues();
if(adminData.toString().indexOf(user) > -1) {
Logger.log(true);
return true;
}
}
// success callback client side (index.html)
function showAdmin(user) {
if(user) {
$(".admin").removeClass("hide");
}
}
The script evaluates which page to display when the user clicks the link once they're authenticated as an administrator.
function doGet(e) {
Logger.log( Utilities.jsonStringify(e) );
if (!e.parameter.page) {
// When no specific page requested, return "home page"
return HtmlService.createTemplateFromFile("index").evaluate().setTitle("Elkhart PD");
}
// else, use page parameter to pick an html file from the script
return HtmlService.createTemplateFromFile(e.parameter['page']).evaluate();
}
This serves the appropriate HTML page. Since I'm an editor on the Sheet, I can see all pages. When I log in under a testing account, which is not an editor, I can see index.html
to register for courses, but I'm denied access even if I authenticate as an administrator. With testing, if I'm an editor on the sheet, I can get to the supplemental HTML pages.
I think the true answer is to build a true web page, but I wanted to see if I was missing anything before heading down that path in the near term.