[英]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.我创建了一个 web 应用程序来跟踪我公司员工的工作时间,web 应用程序很简单,它首先要求他们提供用户名和密码,然后允许他们注册他们的进入时间或离开时间。 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.
我的老板还问我是否有一种方法可以让员工查看他们最近 3 次的登记是什么,以防他们忘记记录进入或退出时间。 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.
所以我需要做的是在接收数据的工作表中查找每个员工的具体姓名/ID,然后在“最后注册”部分下的 web 应用程序上打印该数据。
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".
我需要看的是如何在“ReceivedData”(来自 web 应用程序的早期注册)的数据库中查找此人的姓名,以及如何将该提交的时间戳带到 web 应用程序下的“最后注册部分”。 Here is also a spreadsheet with some data where you can work.
这里还有一个包含一些数据的电子表格,您可以在其中工作。 ( https://docs.google.com/spreadsheets/d/1CJoPuq3sHE5L31GwlvS4Zygm1sL3M0HGC7MgW3rCq3g/edit#gid=0 )
( 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>
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.
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".
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.ReceivedData
表中搜索输入的值。checkLogin
in GAS side, it might be suitable.checkLogin
中时,它可能是合适的。<div id="lastTimestamp"></div>
is added to HTML and LoginUser
in Javascript is modified.<div id="lastTimestamp"></div>
并修改了 Javascript 中的LoginUser
。 When above points are reflected to your script, it becomes as follows.当以上几点反映到您的脚本中时,它会变成如下。
Please modify checkLogin
as follows.请修改
checkLogin
如下。
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.请按如下方式修改
LoginUser
。
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.并且,请按以下方式添加HTML。
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.