简体   繁体   English

为什么在向电子表格添加公式后,我的工作表没有添加新的数据行?

[英]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>

Explanation / Issue:说明/问题:

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 ( data1data2 ),因为数据可以直接粘贴到工作表中:

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 .我更改了您的公式以匹配从BBDDDataBaseDataBase表。 Change that back (in the script) if you are using the other sheet name.如果您使用其他工作表名称,请将其更改回(在脚本中)。

Solution:解决方案:

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:当心:

  • In order for the changes to take effect you need to re-deploy your webApp (either new deployment in the new script editor) or if you are using the older editor, deploy the web app again by selecting project version new and click on update.为了使更改生效,您需要重新部署您的 webApp(在新脚本编辑器中进行新部署),或者如果您使用的是旧编辑器,请通过选择新项目版本并单击更新再次部署 web 应用程序。

Also don't forget to save the script changes.也不要忘记保存脚本更改。

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

相关问题 谷歌工作表脚本自动添加带有复制公式的新行 - Google sheet script auto add new rows with copied formulas 为什么我的角度提交功能不添加空数据 - Why doesn't my angular submit function adding empty data 为什么将此无限循环添加到我的 javascript 代码后我的页面无法加载? - Why my page doesn't load after adding this infinite loop to my javascript code? 将 if 语句脚本添加到电子表格的新行 - Add if statement script to new rows of spreadsheet 为什么我的jquery代码没有在运行时向html表添加行? - Why doesn't my jquery code add rows to html table on fly? 为什么我在 React-Native 中的 FlatList 没有更新,即使添加了额外数据? - Why my FlatList in React-Native doesn't update, even after adding extradata? 将工作表复制到新电子表格,将单元格值添加到新电子表格名称的末尾 - Copy sheet to new spreadsheet, Add cell value to end of new spreadsheet name 为什么单击“新产品”按钮后,我的“保存”按钮和“取消”按钮没有显示? - Why my Save button and Cancel Button doesn't show after I clicked the New Product button? easyLocator 不加载我的电子表格 - easyLocator doesn't load my spreadsheet 添加新控件后,传单地图不起作用 - Leaflet map doesn’t work after adding a new control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM