Obtener varias respuestas de 1 forms en tiempo real

138 views
Skip to first unread message

Mario Alejandro Rodriguez Pachon

unread,
Jul 4, 2021, 11:55:19 PM7/4/21
to Google Apps Script Community
Hola buen día,

Necesito de su ayuda con el siguiente problema:

Tengo un activador que me permite ejecutar una función que obtiene la respuesta de un forms una vez esta se envía. Lo malo es que cuando se envían más de 1 respuesta al tiempo, el programa solo adquiere 1 y no rescata las demás. ¿Saben que función puedo utilizar para adquirir varias respuestas al tiempo?

Gracias!!

Clark Lind

unread,
Jul 5, 2021, 8:02:51 AM7/5/21
to Google Apps Script Community
Hola,
Esto debería ayudarlo a comenzar en la dirección correcta. Esto le muestra cómo obtener las marcas de tiempo de las dos últimas respuestas del formulario. A partir de ahí, debería poder derivar el tiempo (minutos y segundos) y hacer comparaciones y ejecutar código adicional. ¡Espero que esto ayude! (disculpe los errores del traductor de google):

var ss = SpreadsheetApp.getActiveSpreadsheet();

function onFormSubmit() {
var sheet = ss.getActiveSheet();
var fUrl = ss.getFormUrl();
var form = FormApp.openByUrl(fUrl);
var formResponses = form.getResponses();  //Devuelve una matriz (array)

var lastResponse = formResponses[formResponses.length - 1].getTimestamp(); //recuperar la última respuesta
var SecondToLastResponse = formResponses[formResponses.length - 2].getTimestamp();  //recuperar la penúltima respuesta

//escriba un código para recuperar los segundos de las marcas de tiempo y compare
  console.log(SecondToLastResponse,lastResponse);


}

Mario Alejandro Rodriguez Pachon

unread,
Jul 9, 2021, 11:25:12 PM7/9/21
to Google Apps Script Community

Hi thanks for your help,

but, the issue is get two responses summited at the same time with a trigger Form Submit, my code just run taking 1 response of 2.

 check the pictures below:

example1.png

so, I am trying to use LockService, but it is not working at the moment.

Thanks for your help.

Clark Lind

unread,
Jul 10, 2021, 7:34:40 AM7/10/21
to Google Apps Script Community
If you can share a mock sheet, it would be easier to help you solve this. Just make it "viewable", and I will make a copy.

Alan Wells

unread,
Jul 10, 2021, 9:52:09 AM7/10/21
to Google Apps Script Community
Use the event object to get either the answers or the row number of that submission.
Plus use LockService to prevent conflicts when two code instances are running at the same time.

function onFormSubmit(e) {
try{
    var releaseLock;

  releaseLock = function() {
    try{
      LockService.getDocumentLock().releaseLock();
    }catch(e){}
    }

  try{
    LockService.getDocumentLock().waitLock(10000);//Wait for up to 10 seconds - Dont check for whether the lock was obtained
   }catch(e){}

  var submittedValues = e.namedValues;
  var rowNumberOfSubmission = e.range.getRow();
  var sheetIdOfSubmission = e.range.getSheet().getSheetId();

   //Your code

   releaseLock();

}catch(e){
    try{
  LockService.getDocumentLock().releaseLock();
  }catch(e){}
  console.log("Error: " + e);
Reply all
Reply to author
Forward
0 new messages