[英]Why doesn't my sheet add new rows of data, after adding formulas to the spreadsheet?
I have created a web app to track the working hours of employees at my company, the web app is simple, it just asks employees to enter their entry time when they arrive to work and the time when they leave.我创建了一个 web 应用程序来跟踪我公司员工的工作时间,web 应用程序很简单,它只是要求员工输入上班时间和离开时间。 I have them enter first, their ID number and a password, then they just fill out the date, the time at which they arrived/left and I added a string for any other details they would like to add.我让他们先输入他们的 ID 号和密码,然后他们只需填写日期、他们到达/离开的时间,然后我为他们想要添加的任何其他详细信息添加了一个字符串。 Bringing this data to a google spreadsheet, as seen in the picture.将这些数据带到谷歌电子表格中,如图所示。 What I want to do is add a VLOOKUP function, in the column that says "Name", that will search for the employees name by looking up their ID number in a data base, the problem is once I add the formula to the column labeled "Name", the spreadsheet stops receiving new data from the web app.我想要做的是在“姓名”列中添加一个 VLOOKUP function,它将通过在数据库中查找员工的 ID 号来搜索员工姓名,问题是一旦我将公式添加到标记为的列“名称”,电子表格停止从 web 应用程序接收新数据。 Here's a link to a copy of the sheet ( https://docs.google.com/spreadsheets/d/1fjpKRi3k0VQ8MOoa5ruCZurXcz6vmVsQt2g3NbELSJA/edit#gid=1409321990 ) and an here is the simplified the JavaScript and HTML code. Here's a link to a copy of the sheet ( https://docs.google.com/spreadsheets/d/1fjpKRi3k0VQ8MOoa5ruCZurXcz6vmVsQt2g3NbELSJA/edit#gid=1409321990 ) and an here is the simplified the JavaScript and HTML code.
function doGet(e) { return HtmlService.createHtmlOutputFromFile('Form'); } function AddRecord(DateEntry, username, ArrivalTime, ExitTime ) { // get spreadsheet details var url = 'https://docs.google.com/spreadsheets/d/1fjpKRi3k0VQ8MOoa5ruCZurXcz6vmVsQt2g3NbELSJA/edit#gid='; //Paste URL of GOOGLE SHEET var ss1= SpreadsheetApp.openByUrl(url); var webAppSheet1 = ss1.getSheetByName('ReceivedData'); const Lrow = webAppSheet1.getLastRow(); const sep_col = 2; const data = [DateEntry, username, ArrivalTime, ExitTime, new Date()]; const data1 = data.slice(0,sep_col); const data2 = data.slice(sep_col,data.length); const start_col = 1; const space_col = 1; webAppSheet1.getRange(Lrow+1,start_col, 1, data1.length).setValues([data1]); webAppSheet1.getRange(Lrow+1,start_col+data1.length + space_col, 1, data2.length).setValues([data2]); }
<.DOCTYPE html> <html> <head> <base target="_top"> <title>Time Tracking</title> <script> function AddRow() { var DateEntry = document.getElementById("DateEntry");value. var username = document.getElementById("username");value. var ArrivalTime = document.getElementById("ArrivalTime");value. var ExitTime = document.getElementById("ExitTime");value. google.script.run,AddRecord(DateEntry, username, ArrivalTime; ExitTime). document.getElementById("DateEntry");value = ''. document.getElementById("username");value = ''. document.getElementById("ArrivalTime");value = ''. document.getElementById("ExitTime");value = ''; } </script> </head> <body> <div class="content"> <div class="header"> <h2>Time Tracking</h2> </div> <div> <label>ID</label><br> <input class="field" id="username" type="text"> </div> <div> <label>Date</label><br> <input class="field" type="date" id="DateEntry" /> </div> <div > <label>Arrival Time</label><br> <input class="time" type="time" id="ArrivalTime" /><br> </div> <div > <label>Exit Time</label><br> <input class="time" type="time" id="ExitTime" /><br> </div> <div class="btn"> <button type="button" value="Add" onclick="AddRow()">Send</button> </div> </div> </body> </html>
The issue is that you are using an arrayformula
which expands until the last available row in the sheet.问题是您正在使用一个arrayformula
,它会扩展到工作表中的最后一个可用行。
However, your goal is to append a new row everytime after the last row with content.但是,您的目标是 append 每次在包含内容的最后一行之后都有一个新行。
Therefore, by using getLastRow
you are getting the wrong row.因此,通过使用getLastRow
你得到了错误的行。
Instead of using arrayformula
use a single vlookup
formula and take advantage of template literals in order to dynamically change the vlookup
value:而不是使用arrayformula
使用单个vlookup
公式并利用模板文字来动态更改vlookup
值:
=IFERROR(VLOOKUP(B${Lrow+1};DataBase:A2;B250;2;0);"")
In this way you don't need two different arrays ( data1
and data2
) because the data can be pasted directly into the sheet:这样您就不需要两个不同的 arrays ( data1
和data2
),因为数据可以直接粘贴到工作表中:
const vlp_form = `=IFERROR(VLOOKUP(B${Lrow+1};DataBase!A2:B250;2;0);"")`;
const data = [DateEntry, username ,vlp_form, ArrivalTime, ExitTime, new Date()];
webAppSheet1.getRange(Lrow+1,1, 1, data.length).setValues([data]);
I changed your formula to match the DataBase
sheet from BBDD
to DataBase
.我更改了您的公式以匹配从BBDD
到DataBase
的DataBase
表。 Change that back (in the script) if you are using the other sheet name.如果您使用其他工作表名称,请将其更改回(在脚本中)。
Modify only AddRecord
as follows:只修改AddRecord
如下:
function AddRecord(DateEntry, username, ArrivalTime, ExitTime ) {
// get spreadsheet details
const url = 'https://docs.google.com/spreadsheets/d/1fjpKRi3k0VQ8MOoa5ruCZurXcz6vmVsQt2g3NbELSJA/edit#gid=';
//Paste URL of GOOGLE SHEET
const ss1= SpreadsheetApp.openByUrl(url);
const webAppSheet1 = ss1.getSheetByName('ReceivedData');
const Lrow = webAppSheet1.getLastRow();
const vlp_form = `=IFERROR(VLOOKUP(B${Lrow+1};DataBase!A2:B250;2;0);"")`;
const data = [DateEntry, username ,vlp_form, ArrivalTime, ExitTime, new Date()];
webAppSheet1.getRange(Lrow+1,1, 1, data.length).setValues([data]);
}
Be careful:当心:
Also don't forget to save the script changes.也不要忘记保存脚本更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.