Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save apivat60/626f2bae1b3e7e05215647886c4f241e to your computer and use it in GitHub Desktop.
Save apivat60/626f2bae1b3e7e05215647886c4f241e to your computer and use it in GitHub Desktop.
------------Code.gs----------------
var ss= SpreadsheetApp.openByUrl('aaa');
function doGet(e) {
var htmlOutput = HtmlService.createTemplateFromFile('Login');
htmlOutput.message = '';
return htmlOutput.evaluate();
}
function doPost(e) {
Logger.log(JSON.stringify(e));
if(e.parameter.LoginButton == 'Login')
{
var username = e.parameter.username;
var password = e.parameter.password;
var checkanswer = checkLogin(username, password);
if(checkanswer == 'TRUE')
{
var htmlOutput = HtmlService.createTemplateFromFile('Welcome');
htmlOutput.username = username.toUpperCase();
htmlOutput.message = '';
return htmlOutput.evaluate();
}
else
{
var htmlOutput = HtmlService.createTemplateFromFile('Login');
htmlOutput.message = 'กรอกชื่อผู้ใช้หรือรหัสผ่านผิด';
return htmlOutput.evaluate();
}
}
else if(e.parameter.LogoutButton == 'Logout')
{
LogOutUserNow(e.parameter.username);
var htmlOutput = HtmlService.createTemplateFromFile('Login');
htmlOutput.message = 'คุณออกจากระบบแล้ว';
return htmlOutput.evaluate();
}
}
function getUrl() {
var url = ScriptApp.getService().getUrl();
return url;
}
function checkLogin(username, password) {
var usernamesheet = ss.getSheets()[0]
var currentsheet = ss.getSheets()[1]
var usernameLastRow = usernamesheet.getLastRow();
var currentLastRow = currentsheet.getLastRow();
var found_record = '';
for(var y = 2; y <= currentLastRow; y++)
{
if(currentsheet.getRange(y, 1).getValue().toUpperCase() == username.toUpperCase())
{
found_record = 'TRUE';
var d = new Date();
currentsheet.getRange(y, 2).setValue(d);
}
}
if(found_record == '')
{
for(var i = 2; i <= usernameLastRow; i++)
{
if(usernamesheet.getRange(i, 1).getValue().toUpperCase() == username.toUpperCase() &&
usernamesheet.getRange(i, 2).getValue().toUpperCase() == password.toUpperCase())
{
found_record = 'TRUE';
currentsheet.appendRow([username.toUpperCase(), new Date()]);
}
}
}
if(found_record == '')
{
found_record = 'FALSE';
}
return found_record;
}
function LogOutUserNow(username)
{
var currentsheet = ss.getSheets()[1]
var currentLastRow = currentsheet.getLastRow();
for(var y = 2; y <= currentLastRow; y++)
{
if(currentsheet.getRange(y, 1).getValue() == username.toUpperCase())
{
currentsheet.getRange(y, 3).setValue('X');
}
}
for(var y = 2; y <= currentLastRow; y++)
{
if(currentsheet.getRange(y, 3).getValue() == 'X')
{
currentsheet.deleteRow(y);
}
}
}
function LogOutUser()
{
var currentsheet = ss.getSheets()[1]
var currentLastRow = currentsheet.getLastRow();
var ThirtyMinutesAgo = new Date( Date.now() - 30000 * 60 );
for(var y = 2; y <= currentLastRow; y++)
{
if(currentsheet.getRange(y, 2).getValue() < ThirtyMinutesAgo)
{
currentsheet.getRange(y, 3).setValue('X');
}
}
for(var y = 2; y <= currentLastRow; y++)
{
if(currentsheet.getRange(y, 3).getValue() == 'X')
{
currentsheet.deleteRow(y);
}
}
}
----------------Login-------------------------------
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<script type="text/javascript" src="js/materialize.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Prompt">
<style>
body {
font-family: "Prompt";
font-size: 16px;
}
</style>
</head>
<body>
<div class="row">
<div class="col s12 m4 offset-m4">
<div class="card">
<div class="card-action teal lighten-1 white-text">
<h3>ลงชื่อเข้าสู่ระบบ</h3>
</div>
<?var url = getUrl();?>
<form method="post" action="<?= url ?>" >
<div class="card-content">
<div class="form-field">
<label>UserName</label>
<input type="text" name="username" />
</div><br>
<div class="form-field">
<label>Password</label>
<input type="password" name="password" />
</div><br>
<div class="form-field">
<button class = "btn-large wave-effect wave-dark" style = "width:100%" type="submit" value="Login" name="LoginButton">Login</button>
<span><?= message ?></span>
</div><br>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
---------------Welcome.html--------------------------
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<script type="text/javascript" src="js/materialize.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Prompt">
<style>
body {
font-family: "Prompt";
font-size: 16px;
}
</style>
</head>
<body>
<div class="row">
<div class="col s12 m4 offset-m4">
<div class="card">
<div class="card-action teal lighten-1 white-text">
<h3>ลงชื่อเข้าใช้เรียบร้อย</h3>
</div>
<?var url = getUrl();?>
<form method="post" action="<?= url ?>" >
<br><center>
<span style="font-weight: bold">ยินดีต้อนรับคุณ <?= username ?></span> </center>
<input type="hidden" value="<?= username ?>" name="username" />
<span><?= message ?></span>
<br><center>
<button class="waves-effect waves-red btn yellow black-text" style = "width:80%" type="button" onclick="linkweb()">คลิกเข้าดูผลการเรียนออนไลน์</button>
</center><br><br>
<button class = "btn-large wave-effect wave-dark" style = "width:100%" type="submit" value="Logout" name="LogoutButton">Logout</button>
<br>
</form>
</div>
</div>
</div>
<script>
function linkweb(){
window.open("aaa","_top");
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment