Trong bài này, giaoan.link chia sẻ tiếp đến bạn chủ đề về Google sheet app script webapp Login form ver 2024. Trên kênh youtube/netmediacctv đã chia sẻ nhiều về login form, tuy nhiên với mỗi loại là một giao diện và chức năng khác nhau. Sau đây là code app script và video hướng dẫn.
Các project về google sheet và app script khác:
- Tạo hóa đơn trên Google sheet – Gửi email đính kèm hóa đơn pdf – Quản lý url hóa đơn trên danh sách
- Google sheet Ứng dụng theo dõi đơn hàng – Danh sách sản phẩm mua, trạng thái đơn hàng
- Tip on Ms Word Hướng dẫn tạo ngắt trang, xóa ngắt trang đơn lẽ hoặc toàn bộ ngắt trang trên file doc
- Google sheet | Generate QR Codes – Mã hóa nội dung Cell – Thêm hình ở giữa mã QR code
- Google sheet App Script | Tạo custom menu – Xóa tất cả dòng Rỗng trên vùng dữ liệu Nhanh chống
- Google Script Web | From upload file – Kiểm tra trùng lặp – Ghi đè hoặc Tạo phiên bản mới cho file
- Google Script Web | Trang Tìm kiếm nhiều điều kiên riêng lẽ hoặc hết hợp – Print, Xuất file PDF
- Hướng dẫn kiểm tra Bàn phím, Tháo và thay bàn phím mới cho Dell Inspirion 15 3000 series
- Google sheet webapp | Quản lý bán hàng – Chức nằn trả góp, In thông tin
- Ổ C đầy Di chuyển dữ liệu ZALO sang Ổ D hoặc ổ khác một cách dễ dàng Không lo zalo báo đầy ổ đĩa
Code trang “Code.gs”
let MySheets = SpreadsheetApp.getActiveSpreadsheet();
let LoginSheet = MySheets.getSheetByName("login");
function doGet(e){
var output = HtmlService.createTemplateFromFile('login');
var sess = getSession();
if (sess.loggedIn){
let page = e.parameter.page;
if (page == null) page = "main";
var output = HtmlService.createTemplateFromFile(page);
return output.evaluate();
// output = HtmlService.createTemplateFromFile('main');
}
return output.evaluate();
}
function myURL(){
return ScriptApp.getService().getUrl();
}
function setSession(session) {
var sId = Session.getTemporaryActiveUserKey();
var uProp = PropertiesService.getUserProperties();
uProp.setProperty(sId, JSON.stringify(session));
}
function getSession(){
var sId = Session.getTemporaryActiveUserKey();
var uProp = PropertiesService.getUserProperties();
var sData = uProp.getProperty(sId);
return sData ? JSON.parse(sData): {loggedIn: false};
}
function loginUser(pUID, pPassword) {
if (loginCheck (pUID, pPassword)) {
var sess= getSession();
sess.loggedIn = true;
setSession(sess);
return 'success';
}else {
return 'failure';
}
}
function logoutUser() {
var sess = getSession();
sess.loggedIn = false;
setSession(sess);
}
function loginCheck (pUID, pPassword){
let LoginPass = false;
let ReturnData = LoginSheet.getRange("A:A").createTextFinder(pUID).matchEntireCell(true).findAll();
ReturnData.forEach(function (range) {
let StartRow = range.getRow();
let TmpPass = LoginSheet.getRange (StartRow, 2).getValue();
if (TmpPass == pPassword){
LoginPass = true;
}
});
return LoginPass;
}
function OpenPage(PageName){
return HtmlService.createHtmlOutputFromFile(PageName).getContent();
}
//Header for main page
function includeHeader(){
return HtmlService.createTemplateFromFile("header").evaluate().getContent();
}
Code trang “login.html”
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Security Login form ver 2024</title>
<style>
/* POPPINS FONT */
@import url('https://fonts.googleapis.com/css2?family=Tahoma:wght@400;500;600;700&display=swap');
*{
font-family: 'Poppins', sans-serif;
}
.wrapper{
background: #ececec;
padding: 0 20px 0 20px;
}
.main{
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.side-image{
background-image: url("https://gkg.com.vn/wp-content/uploads/2020/12/thiet-ke-van-phong-lam-viec.jpg");
background-position: center;
background-size: cover;
background-repeat: no-repeat;
border-radius: 10px 0 0 10px;
position: relative;
}
.row{
width: 900px;
height:550px;
border-radius: 10px;
background: #fff;
padding: 0px;
box-shadow: 5px 5px 10px 1px rgba(0,0,0,0.2);
}
.text{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.text p{
color: #fff;
font-size: 20px;
}
i{
font-weight: 400;
font-size: 15px;
}
.right{
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.input-box{
width: 330px;
box-sizing: border-box;
}
img{
width: 35px;
position: absolute;
top: 30px;
left: 30px;
}
.input-box header{
font-weight: 700;
text-align: center;
margin-bottom: 45px;
}
.input-field{
display: flex;
flex-direction: column;
position: relative;
padding: 0 10px 0 10px;
}
.input{
height: 45px;
width: 100%;
background: transparent;
border: none;
border-bottom: 1px solid rgba(0, 0, 0, 0.2);
outline: none;
margin-bottom: 20px;
color: #40414a;
}
.input-box .input-field label{
position: absolute;
top: 10px;
left: 10px;
pointer-events: none;
transition: .5s;
}
.input-field input:focus ~ label
{
top: -10px;
font-size: 13px;
}
.input-field input:valid ~ label
{
top: -10px;
font-size: 13px;
color: #5d5076;
}
.input-field .input:focus, .input-field .input:valid{
border-bottom: 1px solid #743ae1;
}
.submit{
border: none;
outline: none;
height: 45px;
background: #ececec;
border-radius: 5px;
transition: .4s;
}
.submit:hover{
background: rgba(37, 95, 156, 0.937);
color: #fff;
}
.signin{
text-align: center;
font-size: small;
margin-top: 25px;
}
span a{
text-decoration: none;
font-weight: 700;
color: #000;
transition: .5s;
}
span a:hover{
text-decoration: underline;
color: #000;
}
@media only screen and (max-width: 768px){
.side-image{
border-radius: 10px 10px 0 0;
}
img{
width: 35px;
position: absolute;
top: 20px;
left: 47%;
}
.text{
position: absolute;
top: 70%;
text-align: center;
}
.text p, i{
font-size: 16px;
}
.row{
max-width:420px;
width: 100%;
}
}
</style>
<script src="https://code.jquery.com/jquery-3.7.0.min.js"
integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=" crossorigin="anonymous"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script>
function login() {
var username = document.getElementById("uid").value;
var password = document.getElementById("pass").value;
google.script.run.withSuccessHandler(function(response) {
if (response === "success") {
document.getElementById("myid").click();
} else {
$("#RetMsg").removeClass("alert-danger").removeClass("alert-success").addClass("alert-danger");
$("#RetMsg").html("Invalid User ID or Password");
$("#RetMsg").show();
}
}).loginUser(username, password);
}
function ClearText(){
$('#RetMsg').html("");
$('#RetMsg').hide();
$('#RetMsgReg').html("");
$('#RetMsgReg').hide();
}
function OpenRegisterPage(){
google.script.run.withSuccessHandler(ShowRegister).OpenPage("register");
}
function ShowRegister(data){
$('#DivLogin').hide();
$('#DivRegister').html(data);
}
</script>
</head>
<body>
<div class="wrapper">
<div class="container main">
<a style="display:none" id="myid" href="<?= myURL(); ?>" target="_top">Link</a>
<div class="row">
<div class="col-md-6 side-image">
<!------------- image ------------->
<img src="https://yt3.googleusercontent.com/ytc/AIf8zZTvSn3mtqZh9lj1Igis8ZzX_cH-QZ44yRuN4RO7=s176-c-k-c0x00ffffff-no-rj-mo" alt="">
<div class="text">
<p>NỘI DUNG THÔNG TIN <br><i>Youtube.com/Netmediacctv</i></p>
</div>
</div>
<div class="col-md-6 right">
<div class="input-box">
<h2 style="text-align: center">ĐĂNG NHẬP</h2>
<div class="input-field">
<input type="text" class="input" id="uid" onchange="ClearText()" required>
<label for="email">User</label>
</div>
<div class="input-field">
<input type="password" class="input" id="pass" onchange="ClearText()" required>
<label for="pass">Password</label>
</div>
<div class="input-field">
<button type="button" onclick="login()" class="submit" >Login</button>
</div>
<br>
<div id="RetMsg" class="alert alert-danger " style="display:none" role="alert"> </div>
<div class="signin">
<span>Bạn chưa có tài khoảng? <a href="#">Sigup</a></span>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Code trang “header.html”
<head>
<base target="_top">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.7.0.min.js"
integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=" crossorigin="anonymous"></script>
<style>
body {
background-color: #ffffff; //Background color
}
.mycolor {
background-color: #6d3b89;//Color header
}
.color {
color:red; //#6d3b89;
}
.customlink{
color: white;
vertical-align: middle;
padding: 5px;
text-decoration: none;
}
.customlink:hover, customlink:active,customlink:visited {
font-weight: bold;
color: white;
vertical-align: middle;
text-decoration: none;
}
</style>
<script>
function logout()
{
google.script.run.withSuccessHandler(AfterLogOut).logoutUser();
}
function AfterLogOut()
{
document.getElementById("myid").click();
}
</script>
<nav class="navbar navbar-expand-lg mycolor px-2" >
<h3 class=" text-white"><a href="https://giaoan.link" target="_blank"><img src="https://giaoan.link/wp-content/uploads/2021/10/logo-giaoan-2021v2.gif" width="100 px"/></a></h3>
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="customlink active " href="<?= myURL(); ?>">Main</a>
</li>
<li class="nav-item">
<a class="customlink" href="<?= myURL(); ?>?page=page1">Page 1</a>
</li>
</ul>
<button class="btn float-end px-5 text-white" type="button" onclick="logout()">Log Out</button>
<a style="display:none" id="myid" href="<?= myURL(); ?>" target="_top">Link</a>
</nav>
</head>
Code trang “main.html”
<!DOCTYPE html>
<html>
<?!=includeHeader();?>
<body >
<center>
<h1>Welcome to Home Page</h1>
</center>
</body>
</html>
Code trang “page1.html”
<!DOCTYPE html>
<html>
<?!=includeHeader();?>
<body>
<div class="h4" style="text-align:right; color: red;">Welcome to Page 1</div>
<iframe src="https://script.google.com/macros/s/AKfycby2NG9eWiOMFgJcChEq1IJ6zsG-wxrcBYJ1JVkaoZsKoleUTgvGF8mTuxY2I4cLJzTB/exec" style="border:0px solid blue; height: 600px; width: 100%;" title="Danh sách Project Excel và Google sheet"></iframe>
<a style="display:none" id="myid" href="<?= myURL(); ?>" target="_top">Link</a>
</body>
</html>