拒绝提交 Google 表单或移除回复



我使用的是当前"新"版本的Google Forms。我有一个表单,表单 A 和关联的脚本 A。该脚本包含一个函数 onFormSubmit,与表单 A 的表单提交触发器相关联。该函数接收一个参数,即一个事件,该参数分别在"源"和"响应"字段中包含表单和提交的表单响应。

在此函数的正文中,如何防止事件/拒绝表单提交?

或者,如果无法做到这一点,我怎样才能悄悄地阻止存储 FormResponse,或者以静默方式将其从响应列表中删除?

我看到有一个Form.deleteAllResponses方法。我是否必须删除所有响应,然后重新添加除当前响应之外的所有响应?还是有更好的方法?

尝试使用事件触发器并使用:

setAcceptingResponses(enabled)

设置表单当前是否接受响应。新表单的默认值为 true。

以下是相关 SO 帖子中的代码示例:

function onFormSubmit(){
  var af = FormApp.getActiveForm();
  var defaultClosedFor = af.getCustomClosedFormMessage();
  af.setCustomClosedFormMessage("The form is currently processing a submission, please refresh the page.");
  af.setAcceptingResponses(false);

<put your script stuff here>

   af.setAcceptingResponses(true);
  af.setCustomClosedFormMessage(defaultClosedFor);
}

希望这有帮助。

我处理这个问题的方法是使用第二张表格(同一 Google 表格中的选项卡(,将表单响应复制到其中。onFormSubmit(( 函数中发生的第一件事是将响应表中的最新行复制到重复工作表中。您可以实现一个选择语句,该语句根据您的条件选择是否制作副本。

这意味着表单中始终有原始回复的副本(对于审核目的很重要(,但如果提交者出错,也是一种更正/修改回复的方法。

如果它有用,这是我执行复制的函数(请注意,我的设置对象在其他地方被抽象,但希望有足够的内容来明确发生了什么(。

/**
 * Copies form submissions from the responses sheet to another sheet.
 *
 * @param {event object} e the event object received from a form submit trigger
 * @param {Settings} settings an object containing the settings this function expects to use
 * @return {integer} the position of the new row in the destination sheet
 */
function copyFormSubmissionToSheet(e, settings) {
  var sourceSheet = SpreadsheetApp.getActive().getSheetByName(settings.nameOfFormResponsesSheet);
  var destinationSheet = SpreadsheetApp.getActive().getSheetByName(settings.name OfApprovalsSheet);
  var newRow = e.range["rowStart"];
  var columnCount = sourceSheet.getLastColumn();
  var newResponseRange = sourceSheet.getRange(newRow, 1, 1, columnCount);
  var newResponseDestinationRange = destinationSheet.getRange(destinationSheet.getLastRow()+1, 1, 1, columnCount);
  newResponseRange.copyTo(newResponseDestinationRange);
  var newDataSheetRow = destinationSheet.getLastRow();
  return newDataSheetRow;
}

最新更新