Reputation: 53
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. My boss has also asked me if there is a way that employees can see what their last 3 registrations were, in case they may have forgotten to record an entry or exit time. So what I need to do is look for each employee's specific name/id in the sheet where the data is being received, and print that data on the web app under a "Last Registrations" section.
Here is a minimal reproducible example, that just asks for a name, looks for that name in a data base and then allows you to send your submission while bringing the timestamp of the submission. All i would need to see is how to look for the person's name in the data base of "ReceivedData" (which comes from earlier registrations on the the web app) and how to bring the timestamp from that submission to the web app under the "Last Registrations section". Here is also a spreadsheet with some data where you can work. (https://docs.google.com/spreadsheets/d/1CJoPuq3sHE5L31GwlvS4Zygm1sL3M0HGC7MgW3rCq3g/edit#gid=0)
Sorry for the long description and thank you.
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) {
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 getLastRow = webAppSheet2.getLastRow();
var found_record = '';
for(var i = 1; i <= getLastRow; i++)
{
if(webAppSheet2.getRange(i, 1).getValue() == Name)
{
found_record = 'TRUE';
}
}
if(found_record == '')
{
found_record = 'FALSE';
}
return found_record;
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<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;
google.script.run.withSuccessHandler(function(output)
{
if(output == 'TRUE')
{
document.getElementById("loginDisplay").style.display = "none";
document.getElementById("dataDisplay").style.display = "block";
}
else if(output == 'FALSE')
{
document.getElementById("errorMessage").innerHTML = "Name not found";
}
}).checkLogin(Name);
}
</script>
</head>
<body>
<div id="loginDisplay">
<div>
<label>Name</label><br>
<input type="text" id="Name" />
</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 Registrations</label>
<br><br>
<button type="button" value="Add" onclick="AddRow()">Send</button>
</div>
</div>
</body>
</html>
Upvotes: 0
Views: 301
Reputation: 201388
I believe your goal as follows.
From So what I need to do is look for each employee's specific name/id in the sheet where the data is being received, and print that data on the web app under a "Last Registrations" section.
and All i would need to see is how to look for the person's name in the data base of "ReceivedData" (which comes from earlier registrations on the the web app) and how to bring the timestamp from that submission to the web app under the "Last Registrations section".
, I understood your question as follows.
ReceivedData
sheet in the Spreadsheet.checkLogin
in GAS side, it might be suitable.<div id="lastTimestamp"></div>
is added to HTML and LoginUser
in Javascript is modified.When above points are reflected to your script, it becomes as follows.
Please modify checkLogin
as follows.
function checkLogin(Name) {
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 check = webAppSheet2.getRange(2, 1, webAppSheet2.getLastRow(), 1).createTextFinder(Name).matchEntireCell(true).findNext();
var obj = {check: check ? '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;
}
Please modify LoginUser
as follows.
function LoginUser() {
var Name = document.getElementById("Name").value;
google.script.run.withSuccessHandler(function({check, lastTimestamp}) {
if(check == 'TRUE') {
document.getElementById("loginDisplay").style.display = "none";
document.getElementById("dataDisplay").style.display = "block";
document.getElementById("lastTimestamp").innerHTML = lastTimestamp;
} else if(check == 'FALSE') {
document.getElementById("errorMessage").innerHTML = "Name not found";
}
}).checkLogin(Name);
}
And, please add HTML as follows.
From:<label>Last Registrations</label>
<br><br>
<button type="button" value="Add" onclick="AddRow()">Send</button>
To:
<label>Last Registrations</label>
<br><br>
<div id="lastTimestamp"></div>
<button type="button" value="Add" onclick="AddRow()">Send</button>
Upvotes: 1