问题
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
NameandPasswordfromDataBasesheet in the Spreadsheet. - You want to show the last timestamp retrieved from
ReceivedDatasheet by searching the inputted values fromDataBasesheet.
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
Namevalue is searched from the 1st column, and then,Passwordis checked from the 2nd column with the same row of the searched 1st column. And, when the both values are same,TRUEis returned.
Reference:
- offset(rowOffset, columnOffset)
来源:https://stackoverflow.com/questions/65773045/need-to-check-for-login-username-and-password-and-then-bring-data-from-previous