# 5.การสร้างฟอร์ม Login Wepapp ด้วย Google Sheet

{% embed url="<https://youtu.be/e7oJH0SsKU0>" %}

1.code.gs

```
var ss= SpreadsheetApp.openByUrl('url google sheet');
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('Cpanel');
     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);
    }      
  }
}
```

2.Login.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="../../Documents/webapp/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><center>welcome to Admin </center></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  waves-light " style = "width:100%" type="submit" value="Login" name="LoginButton">Login </button>
                <span><?= message ?></span> 
           </div><br>
           </div>
         </form>
       </div>
     </div>
    </div>
  </body>
</html>
```

3.Cpanel.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 blue 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("http://www.narongwit.com");
      }
      </script>
   </body>
</html>

```

4.url สำหรับปรับแต่ง CSS form

[https://materializecss.com/about.html <br>](<https://materializecss.com/about.html&#xD;&#xA;>)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://itreviewapp.gitbook.io/book/1.ebook-google-script/1.1-google-script/5.-login-wepapp-google-sheet.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
