gpt4 book ai didi

google-apps-script - 如何删除 FormApp 中的个人回复

转载 作者:行者123 更新时间:2023-12-04 05:51:42 25 4
gpt4 key购买 nike

FormResponse 不应该有 remove 或 delete 响应方法吗? https://developers.google.com/apps-script/reference/forms/form-response

是否存在,我只是在文档中遗漏了它?

我在这里谈论的是响应,而不是项目。

最佳答案

不,不在那里。并且没有外部 API 来填补空白。

所以这是一个疯狂的想法。

您可以编写一个脚本来获取所有响应,调用 deleteAllResponses(),然后写回除您要删除的响应之外的所有响应。然后您将获得反射(reflect)您关心的回复的摘要信息,但您会丢失提交日期(......您可以将其作为电子表格中的非表单数据添加)和提交者用户 ID(仅在 Apps Domains 中, 并且您可以再次将其保留在表单之外)。

妥协是否可以接受取决于您对表单数据的预期用途。

代码

这是一个包含表单的脚本,具有简单的菜单和 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;
}

关于google-apps-script - 如何删除 FormApp 中的个人回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27241007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com