嘿,我正在尝试将更多的值(从提交表单)添加到指定的现有行中。但我收到控制台错误:
默默无闻的processPk1
这是我的.html代码:
onsubmit="handleFormSubmit(this) //got this onsubmit in my form
<script>
function handleFormSubmit(formObject) {
google.script.run.processPk1(formObject);
document.getElementById("myForm").reset();
}
</script>这是我的.gs代码:
function processPk1(formObject) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ws = ss.getSheetByName("Sheet1");
var data = ws.getRange(2, 1, ws.getLastRow()-1, 1).getValues();
var codeList = data.map(function(r){return r[0].toString(); });
var position = codeList.indexOf(formObject.code); //this is to find the specified existing row position
ws.getRange(position,13).setValue(formObject.pdca);
ws.getRange(position,15).setValue(formObject.teamwork);
ws.getRange(position,17).setValue(formObject.strive);
ws.getRange(position,19).setValue(formObject.cnc);
}你的答复会很感激的。提前谢谢你!
编辑
我想要做的是,用户可以提交一个存在于Google中的“代码”,如果它找到了,那么用户可以将"formObject.pdca,formObject.teamwork等“添加到与”代码“相同的行中。
这是我的电子表格示例:
https://docs.google.com/spreadsheets/d/15NboiL7aIkUFq267t3yhy9hLRGrdbHI2jAlrIq54SvM/edit?usp=sharing
发布于 2020-06-25 05:28:26
这个答案怎么样?
修改要点:
formObject.code的值时,position是-1。这样,就会发生错误。我想这可能是你出问题的原因。当以上要点反映到您的脚本中时,下面的修改如何?
修改脚本:
Google脚本端:
发自:
ws.getRange(position,13).setValue(formObject.pdca);
ws.getRange(position,15).setValue(formObject.teamwork);
ws.getRange(position,17).setValue(formObject.strive);
ws.getRange(position,19).setValue(formObject.cnc);至:
if (position > -1) {
position += 2;
} else {
position = ws.getLastRow() + 1;
ws.getRange(position,1).setValue(formObject.code);
}
ws.getRange(position,13).setValue(formObject.pdca);
ws.getRange(position,15).setValue(formObject.teamwork);
ws.getRange(position,17).setValue(formObject.strive);
ws.getRange(position,19).setValue(formObject.cnc);formObject.code时,值将追加到工作表中。HTML和Javascript方面:
google.script.run与异步进程一起运行。因此,下面的修改可能是合适的。
发自:
google.script.run.processPk1(formObject);
document.getElementById("myForm").reset();至:
google.script.run.withSuccessHandler(() => document.getElementById("myForm").reset()).processPk1(formObject);注意:
exec 的URL并修改脚本时,请将Web重新部署为新版本。这样,最新的脚本就会反映到Web应用程序中。请小心点。参考文献:
添加:
如果您只想在"A“列找到formObject.code时才更新行,那么下面的修改如何?
Google脚本端:
发自:
ws.getRange(position,13).setValue(formObject.pdca);
ws.getRange(position,15).setValue(formObject.teamwork);
ws.getRange(position,17).setValue(formObject.strive);
ws.getRange(position,19).setValue(formObject.cnc);至:
if (position > -1) {
position += 2;
ws.getRange(position,13).setValue(formObject.pdca);
ws.getRange(position,15).setValue(formObject.teamwork);
ws.getRange(position,17).setValue(formObject.strive);
ws.getRange(position,19).setValue(formObject.cnc);
}formObject.code时,脚本什么也不做。https://stackoverflow.com/questions/62568160
复制相似问题