2 回答

TA贡献1790条经验 获得超9个赞
用于Array.flat在检查值而不是 for 循环之前先展平数据:
if(values.flat().includes(value)){
return /*confirmation page*/
} else {
return /*error page*/
}
for 循环不起作用,因为逻辑错误:您的代码指出:
for each value in values
if this value(in loop) is equal to value, I'm checking
end this function and return confirmation html
else
end this function and return error html
无论如何,该函数将在检查第一个值后结束。执行这种 for 循环的正确方法是:
var isValueInColL = false;
for (var i = 0; i < values.length; i++) {
if (values[i][0] == value) {//added [0]
isValueInColL = true;
break;// no need to check rest of col L
}
}
var html = isValueInColL ? 'Confirmation' : 'Error';
return HtmlService.createTemplateFromFile(html).evaluate()
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

TA贡献1836条经验 获得超3个赞
试试这种方式:
function doGet() {
return HtmlService.createHtmlOutputFromFile('Form.html');
}
function doPost (e) {
Logger.log(JSON.stringify(e))
if(!e || !e.parameter) {
Browser.msgBox('No Parameters');
return;
}
var doc=SpreadsheetApp.getActive();
var sheet=doc.getSheetByName('OTP');
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];});
sheet.getRange(nextRow,1,1,newRow.length).setValues([newRow]);
var sheet1=doc.getSheetByName("Sheet1");
var values=sheet1.getRange(2,12,sheet1.getLastRow()-1,1).getValues();
var value=newRow[1];
for (var i=0;i<values.length; i++) {
if (values[i][0]==value) {
return HtmlService.createHtmlOutputFromFile('Confirmation');
}else {
return HtmlService.createHtmlOutputFromFile('Error');
}
}
}
以这种方式尝试您的html:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>body {font-family: "Lato", sans-serif}</style>
</head>
<body>
<form action="script URL" target="_self" method="POST">
<input type="number" placeholder="Enter OTP" required name="OTP">
<button type="submit">Send</button>
</form>
</body>
</html>
您的一些 html 标签不合适,例如 body 标签不属于
添加回答
举报