0

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.

Community
  • 1
  • 1
Brian
  • 4,274
  • 2
  • 27
  • 55
  • So if I understand correctly, the web app runs under the authorization of the user accessing the web app. But fails when the web app tries to open a google sheet they don't have access to? And instead of displaying them the usual failed message page, you want to serve a different page? Is this your question or am I missing something? – Jack Brown May 12 '17 at 16:07
  • It's a registration website that has ballooned significantly. It reads/writes registrations from a spreadsheet. I have two other HTML pages, one for administrators and one for presenters. They are not all shared on the Spreadsheet. `HtmlService` seems to serve `index.html` publicly, anything else requires edit rights on the sheet. The answer may very well be to not do this through apps script. – Brian May 12 '17 at 17:30
  • So you are serving these pages in google spreadsheet using either a sidebar or a dialog box. You want to serve a different page to users who don't have edit access to the sheet. Either way, I would include few details in the question to explain your intent better. An example code or flowchart would be useful. – Jack Brown May 12 '17 at 17:40
  • No, hosting a web app with a direct link to templated HTML, not in a sidebar. The sheet is serving as a database, really. Just updated the question with the relevant scripts in use. It's definitely a strange case. – Brian May 12 '17 at 18:02
  • The reason editor can access the website is because they are only one who can open the spreadsheet. So when they new user logins who doesn't have access to the sheet, the program running under his authentication will not be able to access the sheet and confirm if he/she is registered not. This is interesting project if you are willing to bounce your idea of someone, we can set up a chat :) – Jack Brown May 12 '17 at 18:52
  • Leaving school for the day right now, but maybe next week sometime. The entire project is a GitHub repo you can jump to from my profile. Would love some outside eyes on it. – Brian May 12 '17 at 18:58

0 Answers0