[英]My "From spreadsheet - On form submit" trigger is getting disabled every time I try to run it - how do I find out why?
I have a Google form that collects reddit usernames in a Google sheet.我有一个 Google 表单,可以在 Google 表格中收集 reddit 用户名。 I have a script to check whether the username doesn't exist (ie
https://www.reddit.com/user/<username>
gives a 404) or is banned (username looked up against a list var banned_users = [...]
).我有一个脚本来检查用户名是否不存在(即
https://www.reddit.com/user/<username>
给出 404)或被禁止(用户名在列表中查找var banned_users = [...]
)。 If it is non-existent or banned, I mark the row orange.如果它不存在或被禁止,我将行标记为橙色。 Here's the script:
这是脚本:
function validateUser() {
var spreadsheet = SpreadsheetApp.openById(sheet_id);
var sheet = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
var last_row_number = sheet.getLastRow();
var reddit_username = sheet.getRange(last_row_number, 2).getValue();
var reddit_username_cell = sheet.getRange(last_row_number, 2);
var reddit_username_row = sheet.getRange(last_row_number, 1, 1, 19);
if (banned_users.indexOf(reddit_username) != -1) {
reddit_username_cell.setNote('This user is banned.');
reddit_username_row.setBackground('#ff9900')
} else {
var url = 'https://www.reddit.com/user/' + reddit_username.slice(2) // Get rid of the leading 'u/'
var options = {
'muteHttpExceptions': true,
'followRedirects': true
};
var response = UrlFetchApp.fetch(url, options);
var http_code = response.getResponseCode();
if (http_code == '404') {
reddit_username_cell.setNote('This user does not exist.');
reddit_username_row.setBackground('#ff9900') // #ff9900 is default orange
}
}
}
This script works when I manually run the function.当我手动运行该函数时,此脚本有效。 It successfully marks banned or non-existent users when I make a form submission and trigger it from the script editor.
当我提交表单并从脚本编辑器触发它时,它成功地标记了禁止或不存在的用户。 However when I add a trigger to run it on form submit, and then make a form submission, it gets disabled:
但是,当我添加触发器以在表单提交时运行它,然后进行表单提交时,它会被禁用:
I don't understand why.我不明白为什么。 There is no email explaining the reason or logs showing that it ran at all.
没有电子邮件解释原因或日志显示它根本运行。 How do I find out why this trigger is getting disabled and how do I fix it?
如何找出此触发器被禁用的原因以及如何修复它? Thank you!
谢谢!
When running it from a standalone script (not Sheets-bound) I get the following error:从独立脚本(非 Sheets-bound)运行时,出现以下错误:
Please select an active spreadsheet first.
请先选择一个活动电子表格。 (line 11, file "Code")
(第 11 行,文件“代码”)
upon executing the line:在执行该行时:
var sheet = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
Triggers do run scripts in a standalone fashion too, so I suggest you avoid using active
sheets.触发器也以独立方式运行脚本,因此我建议您避免使用
active
表。 Instead, you can simply replace above line of code for the following:相反,您可以简单地将上面的代码行替换为以下内容:
var sheet = spreadsheet.getSheets()[0];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.