您的触发器函数正在传递一个事件对象,但除非您在参数列表中命名该对象,否则您必须通过arguments
.
尝试这个:
function onFormSubmit(e) {
这就是使您的功能正常工作所需要的一切。
或者,如果你想使用arguments
,你必须改变你的代码来做这样的事情:
var CheckString=arguments[0].values[5];
不过,您还有其他问题。Range.clearContents()
将删除您之前的所有回复,但也会删除问题/标题行,并且不会重置回复范围。
我已经重写了您的脚本以使用此答案tidy()
中的函数变体。这将留下您的标题,并完全删除包含所有先前响应的行。
function onFormSubmit(e) {
var CheckString=e.values[5];
if (CheckString == ['purge']) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("Form Responses");
var numResponses = first.getDataRange().getNumRows() - 1;
tidy(first,numResponses);
}
}
/**
* Prompt user for the number of form response rows to remove, then delete them.
* Assumes that the form responses are in the active sheet, that there is one
* row of 'headers' followed by responses (in row 2).
*
* @param {Sheet} sheet Sheet to be tidied.
* @param {number} thisMany (Optional) The number of rows to remove. If not
* specified, user will be prompted for input.
*/
function tidy(sheet,thisMany) {
if (tidy.arguments.length == 0) {
// Exception if no sheet provided
throw new Error( 'Parameter "sheet" not defined' );
}
// else we assume we have a Sheet object for first argument
if (tidy.arguments.length == 1)
thisMany = Browser.inputBox("How many responses should be tidied?");
sheet.deleteRows(2, thisMany);
}