您的触发器函数正在传递一个事件对象,但除非您在参数列表中命名该对象,否则必须通过arguments
访问它。
试试这个:
function onFormSubmit(e) {
这是所有的需要,使您的工作功能。
另外,如果你想使用arguments
,你必须改变你的代码,做这样的事情:
var CheckString=arguments[0].values[5];
您还有其他问题,但。 Range.clearContents()
将清除您之前的所有回复,但也会清除问题/标题行,并且不会重置回复范围。
我已将您的脚本重写为使用this answer的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);
}
完美。现在想出http post来从excel发送清除命令!当我达到它时,我会跳过那座桥! – user2448450
只是一个愚蠢的一面注意:在JavaScript(和应用程序脚本)中,即使没有命名它也可以访问一个函数参数。只要检查'arguments'数组。 –
@HenriqueAbreu - 同意了,尽管对于自认的新手来说不那么友好。我用一个使用'arguments'的例子更新了答案。 (已经在'tidy()'中使用了它。) – Mogsdad