google apps script - How do I delete individual responses in FormApp -
shouldn't formresponse have remove or delete response method? https://developers.google.com/apps-script/reference/forms/form-response
is there , i'm missing in docs?
i'm talking responses here not items.
nope, not there. , no external api fill gap.
so here's crazy idea.
you write script gets responses, calls deleteallresponses()
, writes one(s) want deleted. you'd have summary info reflects responses care about, you'd have lost submission dates (...which add non-form data in spreadsheet), , submitter userid (in apps domains only, , again retain outside form).
whether or not compromises acceptable depend on intended use of form data is.
code
this forms-contained script simple menu & ui, delete indicated responses. content of deleted responses logged.
/** * adds custom menu active form, containing single menu item * invoking deleteresponsesui() specified below. */ function onopen() { formapp.getui() .createmenu('my menu') .additem('delete response(s)', 'deleteresponsesui') .addtoui(); } /** * ui forms function, deleteresponses(). */ function deleteresponsesui() { var ui = formapp.getui(); var response = ui.prompt("delete response(s)", "list of resonse #s delete, separated 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 indicated response(s) form. * caveat: timestamps remaining responses changed. * deleted responses logged, cannot recovered. * * @parameter {number or number[]} reponse(s) deleted, 0-indexed. * * @returns {number} number of responses deleted. */ function deleteresponses(trash) { if (!trash) throw new error( "missing parameter(s)" ); logger.log(json.stringify(trash)) if (!array.isarray(trash)) trash = [trash]; // if didn't array, fix var form = formapp.getactiveform(); var responses = form.getresponses(); // feels should ask "are really, sure?" form.deleteallresponses(); var numdeleted = 0; (var = 0; < responses.length; i++) { if ( trash.indexof(i.tostring()) !== -1 ) { // response deleted logger.log( "deleted response: " + json.stringify(itemizeresponse(responses[i] )) ) numdeleted++ } else { // response kept var newresponse = form.createresponse(); var itemresponses = responses[i].getitemresponses(); (var j = 0; j < itemresponses.length; j++) { newresponse.withitemresponse(itemresponses[j]); } newresponse.submit(); } } return numdeleted } /** * returns item responses javascript object (name/value pairs). * * @param {response} form response object * * @returns simple object item responses + timestamp */ function itemizeresponse(response) { if (!response) throw new error( "missing parameter(s)" ); var itemresponses = response.getitemresponses(); var itemizedresponse = {"timestamp":response.gettimestamp()}; (var j = 0; j < itemresponses.length; j++) { itemizedresponse[itemresponses[j].getitem().gettitle()] = itemresponses[j].getresponse(); } return itemizedresponse; }
Comments
Post a Comment