How to create a template for AppsScript project

A Template with login system for your AppsScript web apps

         Google Apps Script is a cloud-based scripting language that allows developers to create custom applications, add-ons, and macros for various Google services, such as Google Sheets, Google Docs, and Google Forms. One of the most powerful features of Google Apps Script is its ability to create web applications that can be hosted on Google servers.

LIVE DEMO - QWERTY use as UserId & password


        The code snippet provided in the link contains a Google Apps Script code that creates a web application for user login and logout functionality. This web application is built using HTML and Google Apps Script. The HTML code is used to create the user interface for the web application, while the Google Apps Script code handles the backend functionality.

Let's take a closer look at the code snippet to understand how it works.

function doGet(e) {
  var html =  HtmlService.createTemplateFromFile('login');
  html.message = '';
  return html.evaluate();
}
function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename).getContent();
}
function getUrl() {
 var url = ScriptApp.getService().getUrl();
 return url;
}
var z = (num, places) => String(num).padStart(places, '0');
var wss = SpreadsheetApp.getActiveSpreadsheet();
var us = wss.getSheetByName("Users");
var sa = wss.getSheetByName("active");
var sv = wss.getSheetByName("view");
var s1 = wss.getSheetByName("Sheet1");
var s2 = wss.getSheetByName("Sheet2");
var s3 = wss.getSheetByName("Sheet3");

var pgi;
function doPost(e) {
    Logger.log(JSON.stringify(e));
    if(e.parameter.LoginButton == 'Login'){
    var username = e.parameter.username;
    var password = e.parameter.password;
    pgi = username;
    var vdate = vlogin(username, password);
    if(vdate == 'TRUE'){
    var html =  HtmlService.createTemplateFromFile('index');
    var nm = us.getDataRange().getValues();
    for(var i=1;i<nm.length;i++){if(username === nm[i][4]){var accv = nm[i][6];var nme = nm[i][7];}}
    html.username = username;
    html.message = nme;
    html.acc = accv;
    return html.evaluate();   
    }else{
    var html =  HtmlService.createTemplateFromFile('login');
    html.message = 'Failed to Login';
    return html.evaluate();     
    }}else if(e.parameter.LogoutButton == 'Logout'){
    louNow(e.parameter.username);
    var html =  HtmlService.createTemplateFromFile('login');
    html.message = 'Logged Out';
    return html.evaluate(); 
    }else if(e.parameter.Pcv == 'Pcv'){
    louNow(e.parameter.username);
    var html =  HtmlService.createTemplateFromFile('login');
    html.message = 'Access denied.';
    return html.evaluate(); 
    }else if(e.parameter.null == null){
    louNow(e.parameter.username);
    var html =  HtmlService.createTemplateFromFile('login');
    html.message = 'Login to continue...';
    return html.evaluate(); 
    }}
    function vlogin(username, password){
    var time = Utilities.formatDate(new Date(),"Asia/Kolkata", 'yyyy-MM-dd\'T\'HH:mm:ss');
    var clr =  sa.getLastRow();
    var vlr =  us.getLastRow();
    var flag ='';
    for(var x =1;x<=clr; x++){
    if(sa.getRange(x,3).getValue()==username&&username!=""){
    flag = 'TRUE';
    sa.getRange(x,5).setValue(time);
    }}if( flag == ''){
    for(var i = 1;i<=vlr; i++){
    if(us.getRange(i,2).getValue()=="Yes"&&us.getRange(i,5).getValue()==username&&us.getRange(i,6).getValue()==password&& username!=""){
    flag = 'TRUE';
    var v2 = us.getRange(i,4).getValue();
    var v3 = us.getRange(i,8).getValue();
    sa.insertRowAfter(clr).appendRow([time,v2,username,v3]);
    }}}if(flag == ''){
    flag = 'FALSE'; 
    }return flag;};

  function louNow(username){
  var clr =  sa.getLastRow();
  for(var b = 1; b <=clr; b++){
  if(sa.getRange(b, 3).getValue()==username){
  sa.deleteRow(b);}}};

 function autoLout(){
  var clr =  sa.getLastRow();
  for(var t = 1; t <= clr; t++){
  if(sa.getRange(t, 1).getValue() < new Date(Date.now() - sa.getRange(t,2).getValue()*60*1000)){
  sa.deleteRow(t);}}};

The doGet() function is the entry point for the web application. It is called when the user navigates to the web application URL. The function creates an HTML template for the login page and returns it to the user.

The include() function is used to include HTML code from other files in the web application. This function is called from within the HTML code to load other HTML files, such as CSS or JavaScript files.

The getUrl() function is used to get the URL of the web application. This function is called from within the HTML code to set the action attribute of the HTML form element. This ensures that the form data is submitted to the correct URL when the user clicks the login button.

The z() function is a helper function that is used to format numbers with leading zeros. This function is used to format the user ID numbers in the web application.

The remaining code defines several variables and functions that handle the login and logout functionality of the web application.

The doPost() function is called when the user submits the login form. It retrieves the user credentials from the form and validates them against the user data stored in the Google Sheet. If the credentials are valid, the user is redirected to the home page. Otherwise, an error message is displayed on the login page.

The vlogin() function is used to validate the user credentials. It retrieves the user data from the Google Sheet and checks if the provided credentials match any of the user records. If a match is found, the function logs the user login time and returns "TRUE". Otherwise, it returns "FALSE".

The louNow() function is used to log out the user from the web application. It is called when the user clicks the logout button. The function removes the user login record from the Google Sheet.

The autoLout() function is a helper function that automatically logs out users who have been inactive for a specified period of time. This function is not used in the web application but can be used to enhance the security of the web application.

In conclusion, the code snippet provided in the link demonstrates how to create a web application for user login and logout functionality using Google Apps Script. The code can be customized to suit different use cases and integrated with other Google services to create powerful web applications.

Please help me by donate small amount.

UPI: md.imranbkj@okicici

Stripe: https://donate.stripe.com/4gwdTrdzKbuE2Aw288

 For code     -    visit
-------Have a nice day!-----

1 comment

  1. Hello, the link for code is broken. Please, can you upload again?
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.