Need to check for login username and password, and then bring data from previous entries

穿精又带淫゛_ 提交于 2021-01-25 08:07:20

问题


Following this question I had asked previously (Need to Query Google Sheet for specific data from Google Apps Script).

I have created a web app to track the working hours of employees at my company, the web app is simple, it first asks them to provide their username and a password, and then allows them to register their time of entry o their exit time. Now, I need to find a way to check for a match between username and password (in a data base), and if this is true, bring information about that employee's last submission to the web app, this last submission data is found on another sheet that receives the data from the web app.

Here is a minimal reproducible example, where its just asks for a name and a password, and if correct, show another display, where it brings the timestamp of that user's last submission to the web app.

var name="";

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('Form');
}
function AddRecord(Name) {
  
  // get spreadsheet details
  var url = 'https://docs.google.com/spreadsheets/d/1CJoPuq3sHE5L31GwlvS4Zygm1sL3M0HGC7MgW3rCq3g/edit#gid=0';
  //Paste URL of GOOGLE SHEET
  var ss1= SpreadsheetApp.openByUrl(url);
  var webAppSheet1 = ss1.getActiveSheet();
  const Lrow = webAppSheet1.getLastRow();
  const data = [Name, new Date ()];

  webAppSheet1.getRange(Lrow+1,1, 1, data.length).setValues([data])       
}

function checklogin(Name,Password) {
  var url = 'https://docs.google.com/spreadsheets/d/1CJoPuq3sHE5L31GwlvS4Zygm1sL3M0HGC7MgW3rCq3g/edit#gid=0'; //Paste URL of GOOGLE SHEET
  var ss2= SpreadsheetApp.openByUrl(url);
  var webAppSheet2 = ss2.getSheetByName("DataBase");
  var checkuser = webAppSheet2.getRange(2, 1, webAppSheet2.getLastRow(), 1).createTextFinder(Name).matchEntireCell(true).findNext();
  var obj = {checkuser: checkuser ? 'TRUE' : 'FALSE'};
  var sheet = ss2.getSheetByName("ReceivedData");
  var ranges = sheet.getRange(2, 1, sheet.getLastRow(), 1).createTextFinder(Name).matchEntireCell(true).findAll();
  if (ranges.length > 0) {
    obj.lastTimestamp = ranges.pop().offset(0, 1, 1, 1).getDisplayValue();
    return obj;
  }
  return obj;
}
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <style>
    #LastR{
    display: inline-block;
    }
    </style>
    <script>
    function AddRow()
    {
      var Name = document.getElementById("Name").value;  
      google.script.run.AddRecord(Name);
      document.getElementById("Name").value = '';
      }    
function LoginUser() {
  var Name = document.getElementById("Name").value;
  var Password = document.getElementById("Password").value;
  google.script.run.withSuccessHandler(function({checkuser, lastTimestamp}) {
    if(checkuser == 'TRUE') {
      document.getElementById("loginDisplay").style.display = "none";
      document.getElementById("dataDisplay").style.display = "block";
      document.getElementById("lastTimestamp").innerHTML = lastTimestamp;
    } else if(checkuser == 'FALSE') {
      document.getElementById("errorMessage").innerHTML = "Name not found";
    }
  }).checklogin(Name,Password);
}
    </script>
</head>
<body>
<div id="loginDisplay">
    <div> 
    <label>Name</label><br>
    <input type="text" id="Name" />
    </div>
        <div> 
    <label>Password</label><br>
    <input type="text" id="Password" />
    </div>
    <div class="btn">
<input value="Login" onclick="LoginUser()"
 type="button">
 <span id="errorMessage"></span>
</div> 

    </div>
    <div style="display:none"  id="dataDisplay">
    <div>
<label>Last Registration</label>
<br><br>
<div class="LastR" id="lastTimestamp"></div>
<button type="button" value="Add" onclick="AddRow()">Send</button>
</div>
</div>
  </body>
</html>

And here is a sheet where you can work from or copy the information. https://docs.google.com/spreadsheets/d/1CJoPuq3sHE5L31GwlvS4Zygm1sL3M0HGC7MgW3rCq3g/edit#gid=0


回答1:


I believe your goal as follows.

  • You want to search the inputted values of Name and Password from DataBase sheet in the Spreadsheet.
  • You want to show the last timestamp retrieved from ReceivedData sheet by searching the inputted values from DataBase sheet.

In this case, I would like to propose to modify var obj = {checkuser: checkuser ? 'TRUE' : 'FALSE'}; of the function checklogin for achieving your goal as follows.

From:

var obj = {checkuser: checkuser ? 'TRUE' : 'FALSE'};

To:

var obj = {checkuser: checkuser && checkuser.offset(0, 1).getValue() == Password ? 'TRUE' : 'FALSE'};
  • In this modification, the inputted Name value is searched from the 1st column, and then, Password is checked from the 2nd column with the same row of the searched 1st column. And, when the both values are same, TRUE is returned.

Reference:

  • offset(rowOffset, columnOffset)


来源:https://stackoverflow.com/questions/65773045/need-to-check-for-login-username-and-password-and-then-bring-data-from-previous

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!