0

I have 2 google forms that record form responses to a google Spreadsheet File. Each form delivers the answers to its respective sheet or "tab".

I have written a script for each form that sends me an email whenever the form is submitted. I have tested them separately and they work. However, when I try to put them together, neither the sorting or the sending of the email work.

I am having a hard time debugging because the main function is run by a trigger and I use form response data in my script and thus cannot execute the function manually.

Code:

//this function is called every time a form is submitted
function procesarVinculacionORetiro(e){

  //decide which of the two forms were submitted:
  if (e.namedValues["Ingresa por?"]){
    /////////
    //////// VINCULACIONES
    ///////

    //ordenar hoja de vinculaciones
    //enviar correo con info ultima vinculacion

    //sort data so new is always on top
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Vinculaciones');
    var range = sheet.getRange("A2:X");
    //column A = 1, B = 2, etc.
    range.sort( { column : 1, ascending: false } );


    //**********
    //PARA EL CORREO, CALCULEMOS DATOS IMPORTANTES:
    //**********

    //esto arregla el problema de emails duplicados
    Utilities.sleep(3000);            

    //e es un objeto que contiene la info del formato que acaban de submit
    var dataset = e.values;
    var timestamp=e.namedValues["Timestamp"];
    var ingresaPor=e.namedValues["Ingresa por?"];
    var cualTienda=e.namedValues["En cual tienda? o en oficina?"];
    var cargo=e.namedValues["Que cargo va a desempeñar?"];
    var nombreEmpleado=e.namedValues["Nombre de la persona a vincular? (OJO: sin apellidos)"];
    var apellido1Empleado=e.namedValues["Apellido #1 de la persona a vincular?"];
    var apellido2Empleado=e.namedValues["Apellido #2 de la persona a vincular?"];
    var quienSolicita=e.namedValues["Como te llamas tu?"];
    var quienAutoriza=e.namedValues["Quien lo autoriza?"];


    //*********
    //CONFIGUREMOS EL CUERPO DEL CORREO
    //*********
    var message='';
    message="Hola Equipo, hay una nueva vinculacion pendiente por crear. Los datos completos estan en la hoja de Vinculaciones.<br/><br/> RESUMEN: <br/><br/> fecha de reporte: "+timestamp+"<br/> nombre: "+nombreEmpleado+" "+apellido1Empleado+" "+apellido2Empleado+"<br/>ingresa por: "+ingresaPor+"<br/>reportado por: "+quienSolicita+"<br/> dice que lo autoriza: "+quienAutoriza+"<br/>" 

    //configuremos header correo:
    var destinatario='pamunoz@azucarcolombia.com';  
    //cuadremos cc 
    var carbonCopy='nomina@grupoimperio.com.co, jerodriguez@azucarcolombia.com, imalca@azucarcolombia.com';
    var subject="#Vincular para "+cualTienda+" (" +cargo+"): "+ nombreEmpleado+" "+apellido1Empleado+" x "+ingresaPor + " x " + quienSolicita
    MailApp.sendEmail({
      to: destinatario,
      cc: carbonCopy,
      subject: subject,
      htmlBody: message,
      replyTo: 'nadie@azucarcolombia.com'
    });
  }
  else {
    /////////
    //////// RETIROS
    ///////

    //sort data so new is always on top
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Retiros');
    var range = sheet.getRange("A2:I");
    //column A = 1, B = 2, etc.
    range.sort( { column : 1, ascending: false } );


    //**********
    //PARA EL CORREO, CALCULEMOS DATOS IMPORTANTES:
    //**********

    //esto arregla el problema de emails duplicados
    Utilities.sleep(3000);            


    //*********
    //CONFIGUREMOS EL CUERPO DEL CORREO
    //*********
    var message='';
    message="Hola Equipo, hay un nuevo retiro pendiente, lo pueden ver en la hoja RETIROS. Favor gestionar pago liquidacion en cuanto antes. Gracias" 

    //configuremos header correo:
    var destinatario='pamunoz@azucarcolombia.com';  
    //cuadremos cc 
    var carbonCopy='nomina@grupoimperio.com.co, jerodriguez@azucarcolombia.com, imalca@azucarcolombia.com';
    var subject='#Retiro Nuevo';
    MailApp.sendEmail({
      to: destinatario,
      cc: carbonCopy,
      subject: subject,
      htmlBody: message,
      replyTo: 'nadie@azucarcolombia.com'
    });
  }
}
quelquecosa
  • 890
  • 1
  • 10
  • 24
  • Have you tried logging values at critical points & then either inspecting the logs after entering test data or emailing the logs? – Dean Ransevycz Oct 19 '17 at 01:17
  • 1
    You can also set the notifications on the trigger to immediate and you'll get an error email with an error fairly quickly or you can see the errors on your google cloud platform dashboard. – Cooper Oct 19 '17 at 02:07
  • Possible duplicate of [How can I test a trigger function in GAS?](https://stackoverflow.com/questions/16089041/how-can-i-test-a-trigger-function-in-gas) – Rubén Oct 19 '17 at 15:45

0 Answers0