My problem here is that when the user who has Viewer access in the google sheet tested the web app they are just able to access the web app but when making changes in the records there, they are getting an error and the changes are not applied
I want the user who has Viewer access in the google sheets can add and edit records in the Web App. I also don’t want to change my deployment settings like this: Execute as: User accessing the web app Who has access: ** Anyone with Google account
Please help me with my codes to achieve my goal, because right now the users who has Editor access in the google sheets can add and edit records in the Web App
Code.gs
function doGet(e) {
return handleRequest(e);
}
function loadIndex(e) {
return handleRequest(e);
}
function handleRequest(e) {
// Check if the event object e is defined and has parameters
var page = (e && e.parameter) ? e.parameter.page || 'index' : 'index'; // Default to 'index' if no page is specified or e is undefined
try {
var userEmail = Session.getEffectiveUser().getEmail(); // Use getEffectiveUser to get the email of the user accessing the app
Logger.log("User Email from Session: " + userEmail); // Debugging line
if (!userEmail) {
return HtmlService.createHtmlOutput("Could not retrieve user email. Please ensure you are logged in.");
}
if (!isUserAuthorized(userEmail)) {
return HtmlService.createHtmlOutput("You are not authorized to access this application.");
}
// Directly return the page since users are managed by Google Sheets permissions
Logger.log("User is authorized: " + userEmail); // Debugging line
return HtmlService.createTemplateFromFile(page).evaluate();
} catch (err) {
Logger.log("Error in handleRequest: " + err.toString()); // Log the error
return HtmlService.createHtmlOutput("You don't have permission to access the page");
}
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent();
}
// Check if the user is authorized by verifying their email against the list of viewers
function isUserAuthorized(email) {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var viewers = sheet.getViewers().map(function(user) {
return user.getEmail();
});
Logger.log("Authorized viewers: " + viewers.join(", ")); // Debugging line
return viewers.includes(email);
}
YouGuess is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.