错误,而试图使用onFormSubmit在谷歌应用程序脚本的谷歌表单



我正在尝试设置一些脚本,以防止在青少年中心登记的谷歌表单上出现喷子回复。

我们遇到了一些问题,人们用了不恰当的名字和回答。

在尝试调试和运行代码时抛出调用堆栈"onFormSubmit @ code:6">

原文如下。

function onFormSubmit(e) {
// Define the words to blacklist
var blacklistedWords = [""];
// Get the response from the form
var response = e.response.getItemResponses()[0].getResponse();
// Check if the response contains any blacklisted words
for (var i = 0; i < blacklistedWords.length; i++) {
if (response.indexOf(blacklistedWords[i]) !== -1) {
// If the response contains a blacklisted word, delete the response
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var lastResponse = formResponses[formResponses.length - 1];
var itemResponses = lastResponse.getItemResponses();
itemResponses[0].getResponse().deleteItemResponse();
break;
}
}
}
function blacklistSingleLetter(e) {
// Get the response from the form
var response = e.response.getItemResponses()[0].getResponse();

// Check if the response is a single letter
if (response.length === 1) {
// If the response is a single letter, delete the response
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var lastResponse = formResponses[formResponses.length - 1];
var itemResponses = lastResponse.getItemResponses();
itemResponses[0].getResponse().deleteItemResponse();
}
}
function validatePhoneNumber(e) {
// Get the response from the form
var response = e.response.getItemResponses()[0].getResponse();

// Check if the response is a phone number and if it has exactly 10 characters
if (/^d{10}$/.test(response)) {
// If the response is a phone number with exactly 10 characters, do nothing
} else {
// If the response is not a phone number with exactly 10 characters, delete the response
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var lastResponse = formResponses[formResponses.length - 1];
var itemResponses = lastResponse.getItemResponses();
itemResponses[0].getResponse().deleteItemResponse();

}
}

如果我运行代码,这个错误会被抛出

TypeError: Cannot read properties of undefined (reading 'response')onFormSubmit @ Code.gs:6

我试过让chatGPT写一些,用代码解决问题,但没有运气。我还看了一下Apps Script的文档,还是没找到。

ChatGPT说错误可能来自于这个…

您看到的错误很可能是因为e参数没有传递给onFormSubmit()函数。下面是脚本的更新版本,应该可以正常工作:

我已经为脚本设置了触发器和所有东西。

任何建议和帮助,感谢

它在许多情况下都有帮助-然而,这不是最终的解决方案-即使在5分钟后,事件数据仍然不可用

const itemResponses = callWithRetry(getItemResponses, event, 10)
function getItemResponses(event) {
return event.response.getItemResponses()
}

function callWithRetry(fn, params, maxDepth=7, depth = 0){
try {
return fn(params);
} catch(e) {
if (depth > maxDepth) {
throw e;
}
Utilities.sleep(2 ** depth * 500);
console.info("Retrying....")
return callWithRetry(fn, params, maxDepth, depth + 1);
}

相关内容

  • 没有找到相关文章

最新更新