简体   繁体   English

需要从 Google Apps 脚本查询 Google 表格以获取特定数据

[英]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. ,我理解你的问题如下。

    1. You want to search the inputted value from ReceivedData sheet in the Spreadsheet.您想要从电子表格中的ReceivedData表中搜索输入的值。
    2. You want to put the last timestamp retrieved by searching the inputted value from the sheet to below of "Last Registrations section".您希望将通过搜索输入的值从工作表中检索到的最后一个时间戳放在“最后注册部分”的下方。

Modification points:修改点:

  • From your script, I thought that when the script for searching the inputted value is included in the function checkLogin in GAS side, it might be suitable.从您的脚本中,我认为当用于搜索输入值的脚本包含在 GAS 端的 function checkLogin中时,它可能是合适的。
  • And, in order to show the last timestamp, <div id="lastTimestamp"></div> is added to HTML and LoginUser in Javascript is modified.并且,为了显示最后一个时间戳,在 HTML 中添加了<div id="lastTimestamp"></div>并修改了 Javascript 中的LoginUser

When above points are reflected to your script, it becomes as follows.当以上几点反映到您的脚本中时,它会变成如下。

Modified script:修改脚本:

Google Apps Script side: Google Apps 脚本端:

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;
}

HTML & Javascript side: HTML & Javascript 端:

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。

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>

Note:笔记:

  • In this modified script, your shared Spreadsheet and script are used.在此修改后的脚本中,使用了您共享的电子表格和脚本。 So when the structure of Spreadsheet is changed, the script might not be able to be used.因此,当 Spreadsheet 的结构发生变化时,脚本可能无法使用。 Please be careful this.请注意这一点。

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Google Apps脚本查询Google表格 - Query a Google Sheet from Google Apps Script 如何使用 Google Apps 脚本将数组的数据打印到 Google 表格中 - How to print the data of an array into a Google sheet with Google Apps Script 如何在使用 Bootstrap 和 HTML5 从 Google Sheet 填充数据的 Google Sheet Apps 脚本中添加“数据列表” - How to Add 'Data List' in Google Sheet Apps Script that Populates Data from from Google Sheet with Bootstrap and HTML5 将谷歌工作表中的单元格数据放入 html 输入框。 谷歌应用程序脚本 - put cell data from google sheet into html input box. google apps script Google Apps脚本-从另一个工作表导入工作表值 - Google apps script - Importing sheet values from another sheet 通过从 Google Sheet 生成的数组进行交互 | Google Apps 脚本 - Interact through an array generated from a Google Sheet | Google Apps Script Google Sheet Script - 删除包含特定数据的行 - Google Sheet Script - remove row with specific data 通过 Google Apps 脚本中的 GID 获取特定工作表 - Get specific sheet by its GID in Google Apps Script 如何在 Google Apps 脚本中通过 id 获取特定工作表? - How to get specific sheet by id in Google Apps script? Google Apps 脚本将特定范围从任何选项卡复制到主工作表 - Google Apps Script Copying Specific Range from any tab to a Master Sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM