Using GAS, being able to update responses to a form?

Viewed 152

I want to use Google Apps Script to edit a response to a form that has already been submitted. I have the id of the answer and I can retrieve it, but I cannot send it again changing any item:

  var libro = SpreadsheetApp.getActiveSpreadsheet()
  var hoja = libro.getSheetByName('Respuestas de formulario 1')
  const answers = hoja.getRange(1,1,hoja.getLastRow(), hoja.getLastColumn()).getValues().slice(2)

  const form = FormApp.openById(FORM_ID);


var cuestionario = form.getResponse(RESPONSE_ID)
const items = form.getItems();
    cuestionario.withItemResponse(items[0].asListItem().createResponse(answers[1][1])) //competición
                .withItemResponse(items[1].asListItem().createResponse(answers[1][2])) //sección
                .withItemResponse(items[2].asTextItem().createResponse(answers[1][3])) //Equipo Sección
                .withItemResponse(items[3].asTextItem().createResponse(answers[1][4])) //Equipo A
                 .withItemResponse(items[4].asTextItem().createResponse(answers[1][5])) //Equipo B
                 .withItemResponse(items[5].asDateItem().createResponse(answers[1][6])) //Fecha Inicial
                 .withItemResponse(items[6].asDateItem().createResponse(answers[1][7])) //Fecha Juego
                 .withItemResponse(items[7].asDateItem().createResponse(answers[1][8])) //Hora
                 .withItemResponse(items[8].asTextItem().createResponse(answers[1][9])) //Campo
                 .withItemResponse(items[9].asTextItem().createResponse(answers[1][10])) //Entrenador
                 .withItemResponse(items[10].asTextItem().createResponse(answers[1][11])) //Categoría Fab
                 .withItemResponse(items[11].asListItem().createResponse(answers[1][12])) //Pista Colegio
                  cuestionario.submit()

Exception: Lo sentimos. Esta respuesta ya se ha enviado

Exception: Sorry. This reply has already been sent

1 Answers

An existing Form response can not be changed with FormResponse.submit() or withItemResponse(response).

The documentation for withItemResponse(response) states:

This method applies only to form responses that the script has created but not yet submitted; it cannot affect stored responses.

The documentation for FormResponse.submit() states:

Throws a scripting exception if the response has already been submitted.

There is no official, "built-in" way to edit an existing Google Form response. Only the person who submitted the Form originally should be able to edit the response in the Form. It's also not possible for the owner of the Form to manually edit a Form response. The owner of the Form can delete a response, but the owner can't edit a response.

The user of the Form should have at least some confidence and trust that their answers are not going to be changed, except by them.

There is the possibility to get an "Edit URL" with Apps Script code, but there is no way to programmatically use the Edit URL to edit the existing answers.

Related