我有一个HTML表单,允许用户从Google表中填充表单,并成功地将新行添加到工作表中。我根据
我正在尝试扩展应用程序脚本的功能,以根据两列的数据搜索重复项的条目行,如果有匹配,请覆盖现有行。
我知道我需要一个循环,该循环通过Google表中的每一行进行迭代才能与所讨论的表单数据字段进行比较,但是我不知道如何访问每个字段。
这是表单的缩短示例(实际的更长):
<form id="form">
<form id="form">
<input name="firstName" placeholder="firstName" />
<input name="lastName" placeholder="lastName" />
<input name="someOtherField" placeholder="someOtherField" />
<input name="someFourthField" placeholder="someOtherField" />
<div class="btn-success btn" onclick="SaveData()">Save Data</div>
</form>
</form>
<script>
function SaveData() {
var formData = new FormData(document.getElementById("form"));
fetch('https://script.google.com/macros/s/AKfycbwQFSXfeOKBHzf41MF6Nh5XIOjaPvr159-blUxsg5smD3BDH8qB4RUZRRo8q9nCJLb18w/exec',
{
method: 'post',
body: formData,
})
}
</script>
我的应用程序脚本在添加新行时可以很好地工作,但是要么我的for循环未正确编写,因此找不到匹配项,或者设置nextrow index notex不是工作:
var sheetName = 'Entries'
var scriptProp = PropertiesService.getScriptProperties()
function intialSetup () {
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet()
scriptProp.setProperty('key', activeSpreadsheet.getId())
}
function doPost (e) {
var lock = LockService.getScriptLock()
lock.tryLock(10000)
try {
var doc = SpreadsheetApp.openById(scriptProp.getProperty('key'))
var sheet = doc.getSheetByName(sheetName)
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]
var nextRow = sheet.getLastRow() + 1
var newRow = headers.map(function(header) {
return header === 'timestamp' ? new Date() : e.parameter[header]
})
var range = sheet.getDataRange();
var rangeData = range.getValues();
// Here is where the script is failing to find a match in the spreadsheet
for(i = 1; i > rangeData.length; i++) {
if(rangeData[i][0] == e.firstName && rangeData[i][1] == e.lastName)
{
nextRow = i;
}
}
sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])
return ContentService
.createTextOutput(JSON.stringify({ 'result': 'success', 'row': nextRow }))
.setMimeType(ContentService.MimeType.JSON)
}
catch (e) {
return ContentService
.createTextOutput(JSON.stringify({ 'result': 'error', 'error': e }))
.setMimeType(ContentService.MimeType.JSON)
}
finally {
lock.releaseLock()
}
}
使用上面的脚本,每个表单条目都会创建一个新的行,匹配是永远不正确的。因此,我认为我在匹配调查中的逻辑或我用来查找表单值和电子表格数据的语法有问题。那我想念什么?
谢谢!
编辑:
为了帮助详细说明此示例,我附上了样品电子表格的屏幕截图。如下,如果用户提交带有名称“ Bob”和LastName“ Belcher”的表单,则我希望将其余的表单数据覆盖现有的行中。但是,如果FirstName和LastName字段在电子表格中没有匹配,请添加新的行。
上面的脚本始终即使存在现有行,也会添加新行。我尝试使用的逻辑。我期望的结果,但这导致脚本完全失败(没有更新的行,也没有添加行)。我还尝试使用此这个 这个视频,使用“ indexof”表单数据,而不是使用IF语句查找匹配的循环。该解决方案也无法正常工作
电子表格屏幕截图
编辑编辑:
我已经使整个脚本可用,并且记录了一个简短的视频当前脚本行为与所需的行为。希望这阐明了我的问题。谢谢!
I have an HTML form that allows users to populate the form from a Google Sheet, and successfully add new rows to the Sheet. I created the Apps Script according to this tutorial.
I am trying to expand the function of my Apps Script to search the rows of entries for duplicates based on data from two columns and if there is a match, overwrite the existing row.
I know I need a for loop that iterates through each row in the Google Sheet to compare to the form data fields in question, but I don't know how to access each.
This is a shortened example of the the form (the actual is much longer):
<form id="form">
<form id="form">
<input name="firstName" placeholder="firstName" />
<input name="lastName" placeholder="lastName" />
<input name="someOtherField" placeholder="someOtherField" />
<input name="someFourthField" placeholder="someOtherField" />
<div class="btn-success btn" onclick="SaveData()">Save Data</div>
</form>
</form>
<script>
function SaveData() {
var formData = new FormData(document.getElementById("form"));
fetch('https://script.google.com/macros/s/AKfycbwQFSXfeOKBHzf41MF6Nh5XIOjaPvr159-blUxsg5smD3BDH8qB4RUZRRo8q9nCJLb18w/exec',
{
method: 'post',
body: formData,
})
}
</script>
My Apps Script works perfectly when adding new rows, but either my for loop is not written correctly and thus not finding matches or otherwise setting the nextRow index isn't working:
var sheetName = 'Entries'
var scriptProp = PropertiesService.getScriptProperties()
function intialSetup () {
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet()
scriptProp.setProperty('key', activeSpreadsheet.getId())
}
function doPost (e) {
var lock = LockService.getScriptLock()
lock.tryLock(10000)
try {
var doc = SpreadsheetApp.openById(scriptProp.getProperty('key'))
var sheet = doc.getSheetByName(sheetName)
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]
var nextRow = sheet.getLastRow() + 1
var newRow = headers.map(function(header) {
return header === 'timestamp' ? new Date() : e.parameter[header]
})
var range = sheet.getDataRange();
var rangeData = range.getValues();
// Here is where the script is failing to find a match in the spreadsheet
for(i = 1; i > rangeData.length; i++) {
if(rangeData[i][0] == e.firstName && rangeData[i][1] == e.lastName)
{
nextRow = i;
}
}
sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])
return ContentService
.createTextOutput(JSON.stringify({ 'result': 'success', 'row': nextRow }))
.setMimeType(ContentService.MimeType.JSON)
}
catch (e) {
return ContentService
.createTextOutput(JSON.stringify({ 'result': 'error', 'error': e }))
.setMimeType(ContentService.MimeType.JSON)
}
finally {
lock.releaseLock()
}
}
With the above script, every form entry creates a new row, the match is never true. So I think there is something wrong either with my logic in matchfinding or the syntax I'm using to find the form values and spreadsheet data. So what am I missing?
Thanks!
EDIT:
To help elaborate this example, I have attached a screenshot of a sample spreadsheet. As is, if a user submits a form with firstName "Bob" and lastName "Belcher," I would like to have the remaining form data overwrite the existing row with those names. But if the firstName and lastName fields have no match in the spreadsheet, add a new row.
The above script always adds new rows even if there is an existing row. I tried using the logic from this question to achieve my desired result but this caused the script to fail entirely (no updated rows and no added rows). I also tried using the logic steps outlined in this video which uses "indexOf" form data instead of a for loop with if statement to find matches. This solution also did not work
Spreadsheet Screenshot
EDIT EDIT:
I have made the whole script available and recorded a short video of the current script behavior versus the desired behavior. Hopefully this clarifies my question. Thanks!
发布评论
评论(1)
我相信您的目标如下。
firstName
和LastName
的值。而且,当这些值存在于电子表格中时,您要更新行。当这些值不存在于电子表格中时,您希望将数据作为新行附加。当您的脚本被修改以实现此目的时,以下修改如何?
来自:
to:
e.lastName
始终undefined
。而且,在您的脚本中,需要在找到值时完成循环。注意:
修改了Google Apps脚本时,请将部署修改为新版本。这样,修改后的脚本反映在Web应用程序中。请小心。
I believe your goal is as follows.
firstName
andlastName
from the Spreadsheet. And, when those values are existing in the Spreadsheet, you want to update the row. When those values are not existing in the Spreadsheet, you want to append the data as a new row.When your script is modified for achieving this, how about the following modification?
From:
To:
for(i = 1; i > rangeData.length; i++)
, no loop is done. And,e.firstName
ande.lastName
are alwaysundefined
. And also, in your script, it is required to finish the loop when the values were found.Note:
When you modified the Google Apps Script, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful this.
You can see the detail of this in the report of "Redeploying Web Apps without Changing URL of Web Apps for new IDE".