[英]Why doesn't my sheet add new rows of data, after adding formulas to the spreadsheet?
我創建了一個 web 應用程序來跟蹤我公司員工的工作時間,web 應用程序很簡單,它只是要求員工輸入上班時間和離開時間。 我讓他們先輸入他們的 ID 號和密碼,然后他們只需填寫日期、他們到達/離開的時間,然后我為他們想要添加的任何其他詳細信息添加了一個字符串。 將這些數據帶到谷歌電子表格中,如圖所示。 我想要做的是在“姓名”列中添加一個 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.
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>
問題是您正在使用一個arrayformula
,它會擴展到工作表中的最后一個可用行。
但是,您的目標是 append 每次在包含內容的最后一行之后都有一個新行。
因此,通過使用getLastRow
你得到了錯誤的行。
而不是使用arrayformula
使用單個vlookup
公式並利用模板文字來動態更改vlookup
值:
=IFERROR(VLOOKUP(B${Lrow+1};DataBase:A2;B250;2;0);"")
這樣您就不需要兩個不同的 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]);
我更改了您的公式以匹配從BBDD
到DataBase
的DataBase
表。 如果您使用其他工作表名稱,請將其更改回(在腳本中)。
只修改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]);
}
當心:
也不要忘記保存腳本更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.