您遇到的问题(除了您编写的代码也有点混乱)是,当事件由“编辑时”或“表单提交”触发时,事件对象对它们来说是不同的,因此它们会没有相同的属性,您可以应用与代码中相同的逻辑。
这是编辑对象的样子,也是表单提交对象的样子。
知道。我通过以下方式修改了您的代码:
在您的主要功能中,我检查触发的原因是什么,如果它是编辑或提交事件,然后根据事件以及它们的条件是否为真,我调用onEditChange
oronSubmitChange
函数。
function BatteryInspection(e) {
var range = e.range;
var row = range.getRow();
var col = range.getColumn();
var sheet = range.getSheet();
var colToCapitalize = 2;
var sheetName = "FormResponses1";
// Let's see if it is a form or edit object, checking the namedValues properties
if(!e.namedValues){
// Edit event
// Check the right conditions for an edit event
if (sheet.getName() !== sheetName || row < 2 || col !== colToCapitalize || typeof e.value == "object") return;
else onEditChange(e, sheet, row);
} else {
// Submit Event
// Check the right conditions for a submit event
if (sheet.getName() !== sheetName || row < 2 | typeof e.values != "object") return;
else onSubmitChange(e, sheet, row);
}
}
在onEditChange
andonSubmitChange
函数中,我将右侧单元格中的值设置为大写,然后调用该setFormulas
函数。
// Call when there is an edit event
function onEditChange(evt, sheet, row){
// Uppercase the value where the triggered occured
evt.range.setValue(evt.value.toUpperCase());
setFormulas(sheet, row);
}
// Call when there is an submit event
function onSubmitChange(evt, sheet, row){
// Uppercase the value just in the value of the second col
sheet.getRange(row, 2).setValue(evt.values[1].toUpperCase());
setFormulas(sheet, row);
}
如名称所示,在 setFormulas
函数中,我只是设置了您之前在 main 函数中设置的公式BatteryInspection
。
// Function for setting the formulas
// Call whether an edit or submit event happens
function setFormulas(sheet, row){
sheet.getRange(row, 10).setFormula("=SUBSTITUTE(B" + row + ",\" \",\"\")");
var cellcopy = sheet.getRange(row, 10);
var cellvalues = cellcopy.getValues();
sheet.getRange(row, 2).setValues(cellvalues);
cellcopy.clearContent();
sheet.getRange(row, 8).setFormula("=VLOOKUP(B" + row + ",LookUpTable!A2:C1001,2,0)");
sheet.getRange(row, 9).setFormula("=VLOOKUP(B" + row + ",LookUpTable!A2:C1001,3,0)");
}
整个代码现在看起来像这样:
function BatteryInspection(e) {
var range = e.range;
var row = range.getRow();
var col = range.getColumn();
var sheet = range.getSheet();
var colToCapitalize = 2;
var sheetName = "FormResponses1";
// Let's see if it is a form or edit object, checking the namedValues properties
if(!e.namedValues){
// Edit event
// Check the right conditions for an edit event
if (sheet.getName() !== sheetName || row < 2 || col !== colToCapitalize || typeof e.value == "object") return;
else onEditChange(e, sheet, row);
} else {
// Submit Event
// Check the right conditions for a submit event
if (sheet.getName() !== sheetName || row < 2 | typeof e.values != "object") return;
else onSubmitChange(e, sheet, row);
}
}
// Call when there is an edit event
function onEditChange(evt, sheet, row){
// Uppercase the value where the triggered occured
evt.range.setValue(evt.value.toUpperCase());
setFormulas(sheet, row);
}
// Call when there is an submit event
function onSubmitChange(evt, sheet, row){
// Uppercase the value just in the value of the second col
sheet.getRange(row, 2).setValue(evt.values[1].toUpperCase());
setFormulas(sheet, row);
}
// Function for setting the formulas
// Call whether an edit or submit event happens
function setFormulas(sheet, row){
sheet.getRange(row, 10).setFormula("=SUBSTITUTE(B" + row + ",\" \",\"\")");
var cellcopy = sheet.getRange(row, 10);
var cellvalues = cellcopy.getValues();
sheet.getRange(row, 2).setValues(cellvalues);
cellcopy.clearContent();
sheet.getRange(row, 8).setFormula("=VLOOKUP(B" + row + ",LookUpTable!A2:C1001,2,0)");
sheet.getRange(row, 9).setFormula("=VLOOKUP(B" + row + ",LookUpTable!A2:C1001,3,0)");
}
文档
有关更多信息,您还可以查看: