FormResponse不应该有删除或删除响应方法吗? https://developers.google.com/apps-script/reference/forms/form-response
它存在吗,我只是在文档中遗漏了它?
我在谈论的回应不是物品。
答案 0 :(得分:4)
不,不在那里。而且没有外部API来填补空白。
所以这是一个疯狂的想法。
您可以编写一个获取所有响应的脚本,调用deleteAllResponses()
,然后写回除了您要删除的所有响应之外的所有响应。然后,您将获得反映您关注的回复的摘要信息,但您已丢失提交日期(...您可以在电子表格中添加为非表单数据)和提交者UserID(仅限应用程序域) ,你可以将它保留在表格之外)。
妥协是否可接受取决于您对表单数据的预期用途。
这是一个包含表单的脚本,带有一个简单的菜单和UI,将删除指示的响应。记录已删除回复的内容。
/**
* Adds a custom menu to the active form, containing a single menu item for
* invoking deleteResponsesUI() specified below.
*/
function onOpen() {
FormApp.getUi()
.createMenu('My Menu')
.addItem('Delete response(s)', 'deleteResponsesUI')
.addToUi();
}
/**
* UI for Forms function, deleteResponses().
*/
function deleteResponsesUI() {
var ui = FormApp.getUi();
var response = ui.prompt("Delete Response(s)",
"List of resonse #s to delete, separated by commas",
ui.ButtonSet.OK_CANCEL);
if (response.getSelectedButton() == ui.Button.OK) {
var deleteCSV = response.getResponseText();
var numDeleted = deleteResponses(deleteCSV.split(/ *, */));
ui.alert("Deleted "+numDeleted+" responses.", ui.ButtonSet.OK);
}
}
/**
* Deletes the indicated response(s) from the form.
* CAVEAT: Timestamps for all remaining responses will be changed.
* Deleted responses are logged, but cannot be recovered.
*
* @parameter {Number or Number[]} Reponse(s) to be deleted, 0-indexed.
*
* @returns {Number} Number of responses that were deleted.
*/
function deleteResponses(trash) {
if (!trash) throw new Error( "Missing parameter(s)" );
Logger.log(JSON.stringify(trash))
if (!Array.isArray(trash)) trash = [trash]; // If we didn't get an array, fix it
var form = FormApp.getActiveForm();
var responses = form.getResponses();
// Really feels like we should ask "ARE YOU REALLY, REALLY SURE?"
form.deleteAllResponses();
var numDeleted = 0;
for (var i = 0; i < responses.length; i++) {
if ( trash.indexOf(i.toString()) !== -1 ) {
// This response to be deleted
Logger.log( "Deleted response: " + JSON.stringify(itemizeResponse(responses[i] )) )
numDeleted++
}
else {
// This response to be kept
var newResponse = form.createResponse();
var itemResponses = responses[i].getItemResponses();
for (var j = 0; j < itemResponses.length; j++) {
newResponse.withItemResponse(itemResponses[j]);
}
newResponse.submit();
}
}
return numDeleted
}
/**
* Returns item responses as a javascript object (name/value pairs).
*
* @param {Response} Form Response object
*
* @returns Simple object with all item responses + timestamp
*/
function itemizeResponse(response) {
if (!response) throw new Error( "Missing parameter(s)" );
var itemResponses = response.getItemResponses();
var itemizedResponse = {"Timestamp":response.getTimestamp()};
for (var j = 0; j < itemResponses.length; j++) {
itemizedResponse[itemResponses[j].getItem().getTitle()] = itemResponses[j].getResponse();
}
return itemizedResponse;
}
答案 1 :(得分:3)
现在可以通过脚本执行
要删除回复,您需要您想要删除的回复的ID:
FormApp.getActiveForm().deleteResponse(responseId)
注意:您也可以使用openById获取表单
FormApp.openById(form_id).deleteResponse(responseId)
参考: https://developers.google.com/apps-script/reference/forms/form#deleteresponseresponseid
请注意:该答复将从摘要答复和个人答复中永久删除。
答案 2 :(得分:1)
您可以使用deleteAllResponses()从表单中删除所有回复,但不能删除个别回复。你不能手动delete individual responses。如果您将表单回复定向到电子表格,则可以使用Spreadsheet Service选择并删除其中的个别回复。
答案 3 :(得分:1)
@詹姆斯费雷拉
新版Google表单允许您在不需要脚本的情况下从Google表单中删除个别回复。
@mogsdad和@john无法做到这一点的答案直到最近都是如此。
- 现在可以在新版Google表单
上使用Google于2016年2月10日发布公告。(New Google Forms is now the default option)
要删除个别回复,请单击“回复”标签,然后选择“个人”。找到要删除的记录,然后单击垃圾桶图标以删除该个人响应 但请注意,不会从连接到表单电子表格中删除响应。