由表单提交触发的Google脚本有时不起作用

时间:2015-02-10 14:10:53

标签: javascript google-apps-script google-sheets google-form eventtrigger

我有一个脚本,可以在发送时从Google表单中读取答案。该脚本绑定到电子表格,捕获所有答案。该脚本生成包含一些信息的PDF并通过GmailApp将其发送给用户,并将PDF存储在Drive文件夹中。但有时脚本不起作用。答案始终存储在电子表格中,但尽管如此,脚本仍然无法运行。我检查了"执行记录",但它包含有关上次成功执行的信息。

如何保证脚本始终运行?

有人可以解释为什么会这样吗?

代码

//ID del template
var docTemplate = "Template ID Here";
//Nombre de la copia
var docName = "biovenCIP";

//FUnción principal para generación y envío de constancia
function onFormSubmit(e){
  //Leyendo datos del formulario
  var d = new Date();
  var registration_date = e.values[0];
  var first_name = e.values[1];
  var second_name = e.values[2];
  var first_lastname = e.values[3];
  var second_lastname = e.values[4];
  var document_type = e.values[5];
  var document_number = e.values[6];
  var email_address = e.values[7];
  var occupation = e.values[8];
  var category = e.values[9];
  var payment_number = e.values[10];
  var payment_date = e.values[11];

  //Inicializando mensaje opcional
  var occupation_message = " ";

  //Generando el nombre completo
  if(second_name.localeCompare("") != 0){
    second_name = " " + second_name;
  }
  if(second_lastname.localeCompare("") != 0){
    second_lastname = " " + second_lastname;
  }
  var full_name = first_name + second_name + " " + first_lastname + second_lastname;
  full_name = full_name.replace(/\s+/g," ");

  //Generando compia del template  
  var copyID = DocsList.getFileById(docTemplate).makeCopy(docName + '_' + document_number).getId();
  var copyDoc = DocumentApp.openById(copyID);
  var copyBody = copyDoc.getActiveSection();

  //Reemplazando texto en el template
  copyBody.replaceText('keyDate', registration_date);
  copyBody.replaceText('keyFullName', full_name);
  copyBody.replaceText('keyIDType', document_type);
  copyBody.replaceText('keyIDNumber', document_number);
  copyBody.replaceText('keyEmail', email_address);
  copyBody.replaceText('keyOccupation', occupation);
  switch(occupation){
      case "Estudiante de pregrado":
      occupation_message = "(presente carnet el primer día del evento)";
      var payment_worth = 100.00;
      break
      case "Estudiante de postgrado":
      occupation_message = "(presente carnet el primer día del evento)";
      var payment_worth = 150.00;
      break
      case "Profesional":
      var payment_worth = 200.00;
      break
  }
  copyBody.replaceText('keyOMessage', occupation_message);
  copyBody.replaceText('keyCategory', category);
  copyBody.replaceText('keyPaymentNum', payment_number);
  copyBody.replaceText('keyPaymentDate', payment_date);
  copyBody.replaceText('keyPayment', payment_worth)
  copyDoc.saveAndClose();

  //Convertir temporalmente a PDF
  var pdf = DocsList.getFileById(copyID).getAs("application/pdf");

  //Adjuntando PDF y enviado correo electrónico
  var reply_email = "info@bioven.org.ve";
  var bcc_email = "congresobioven@gmail.com";
  var subject = "Constancia de inscripción - BIOVEN 2015";
  var body = "Estimado " + full_name + ",<br/><br/> A través de este correo electrónico le hacemos entrega de su constancia de inscripción en el V Congreso Venezolano de Bioingeniería - BIOVEN 2015, la cual deberá presentar el primer día del congreso durante la verificación de registro.<br/><br/> Agradecidos,<br/><br/> <b>Comité Organizador del BIOVEN 2015</b>";
  GmailApp.sendEmail(email_address, subject, body, {name: 'Congreso BIOVEN 2015', bcc: bcc_email, htmlBody: body, replyTo: reply_email, attachments: pdf});

  //Guardando PDF en Drive
  var folder_id = "Folder ID Here";
  DriveApp.getFolderById(folder_id).createFile(pdf);

  //Borrando archivo DOC temporal
  DocsList.getFileById(copyID).setTrashed(true);
}

1 个答案:

答案 0 :(得分:0)

尝试在代码中更高级别定义变量payment_worth

function onFormSubmit(e){
  //Leyendo datos del formulario
  var d = new Date();
  //Other var definitions
  var payment_worth = 0;

将初始值设为零。目前,如果您的案例/选择失败,变量payment_worth将是未定义的。

Reference Error

只是一个想法。不知道这是不是一个好主意。 ;)

案例选择将如下所示:

switch(occupation){
  case "Estudiante de pregrado":
  occupation_message = "(presente carnet el primer día del evento)";
  payment_worth = 100.00;
  break
  case "Estudiante de postgrado":
  occupation_message = "(presente carnet el primer día del evento)";
  payment_worth = 150.00;
  break
  case "Profesional":
  payment_worth = 200.00;
  break
}