以编程方式制作的表单不会在触发器上返回响应

时间:2018-06-28 11:02:01

标签: javascript google-apps-script

在提交另一张表格(预制)后,我出于编程目的以编程方式制作了一张新表格。这是代码(此脚本已绑定到预制表单。此createVerificationForm()在由表单onSubmit 触发的函数内部调用):

/**
 * Create a Google Form to verify the submitted request
 * @param {Array<String>} The submitted data
 * @return {Form} The created verification form
 */
function createVerificationForm(submissionData) {
  // Create Form
  var form = FormApp.create("Request Verification");

  // Add Data to be verified as description
  form.setDescription("Show the details here");

  // Add options to verify or deny
  form.addMultipleChoiceItem()
      .setTitle("Verify the request")
      .setChoiceValues([REQUEST_VERIFIED, REQUEST_DENIED])
      .setRequired(true);

  // Set the form trigger
  ScriptApp.newTrigger('setRequestStatus')
           .forForm(form)
           .onFormSubmit()
           .create();

  return form;
}

我还为验证表单创建了一个触发器。 setRequestStatus将获得响应并相应地使用它。因为无法获取响应,所以我尝试stringify()事件对象来检查setRequestStatus()是否确实收到了响应。这是它的样子

/**
 * Set Request Status.
 * Triggered by Verification Form
 * @param e {Object} Event Object sent by the trigger
 */
function setRequestStatus(e) {
  var response = e.response;
  var itemResponses = response.getItemResponses();

// I use email because somehow Logger doesn't work
  MailApp.sendEmail(REVIEWER_EMAIL, "Verification Result", JSON.stringify(e));


}

结果如下:

{"response":{},"source":{},"authMode":{},"triggerUid":"xxxxxxxxxxxx"}

triggerUid外,所有内容均不存在。与创建表单和以编程方式设置触发器有什么关系?

仅供参考,查看“响应”选项卡(当您通过GDrive打开表单时),实际上已记录了响应。

编辑:

根据建议,我将setRequestStatus()调整为:

function setRequestStatus(e) {
  var response = e.response;
  var itemResponses = response.getItemResponses();

  console.log("FormResponse: " + itemResponses[0].getResponse());
}

日志的结果是这样的:

TypeError: Cannot call method "getResponse" of undefined. at setRequestStatus(FormResponder:139)

似乎ItemResponse没有返回getItemResponses()。考虑到直接在Google云端硬盘上打开表单检查收集到的响应确实显示响应确实存在,这很奇怪。

EDIT2:

我尝试log事件对象的内容以防万一。这是代码和日志屏幕截图。

/**
 * Set Request Status.
 * Triggered by Verification Form
 * @param e {Object} Event Object sent by the trigger
 */
function setRequestStatus(e) {
  var response = e.response;
  var itemResponses = response.getItemResponses();

  console.log(e);
  console.log(e.response);
  console.log(e.response.getItemResponses());
  console.log(e.response.getItemResponses()[0]);
}

Log Screencap

注意FormResponse对象在那里,但是当我尝试访问它时,它变成空的。我开始认为这可能是Google应该研究的问题。

EDIT3

看过stepvda's similar code之后,我认为这个问题可能与我的脚本绑定到某种形式(它已经有一个onSubmit触发器)或其他功能(例如例如发送电子邮件和编辑电子表格)。因此,我通过简化脚本在一个新的未绑定项目上进行了测试。这是代码(我对触发器的连接方式做了一些改动):

function testTrigger() {
//  Attach to a premade form (WORKED!)
//  var form = FormApp.openById('1lRKrqxuQm50EY4zMsf8b_myrN9KIaooFS7Elv48XD3U');
//  
//  ScriptApp.newTrigger('logDataFromForm')
//           .forForm(form)
//           .onFormSubmit()
//           .create();

// Attach to a programmatically made form (WORKED!)
//  var form = FormApp.create("Auto Trigger Test");
//  form.addTextItem()
//      .setTitle('How are you?');
//  
//  ScriptApp.newTrigger('logDataFromForm')
//           .forForm(form)
//           .onFormSubmit()
//           .create();

// Attach to a programmatically made form from a function (WORKED!)
  var submissionData = ['Blacky', 'blacky@dog.com'];
  createVerificationForm(submissionData);
}

function logDataFromForm(e) {
  console.log("I'm auto triggered");

  console.log(e);
  console.log(e.response);
  console.log(e.response.getItemResponses());
  console.log(e.response.getItemResponses()[0].getResponse());
}

function createVerificationForm(submissionData) {
  // Create Form
  var form = FormApp.create("Are you dumb? (test)");

  // Add Data to be verified as description
  form.setDescription(
    "Some random description" +
    "\nName\t\t\t\t: " + submissionData[0] +
    "\nEmail\t\t\t\t: " + submissionData[1] +
    "\n"
  );

  // Add options to verify or deny
  var choices = ['Yes', 'No'];
  form.addMultipleChoiceItem()
      .setTitle("Are you dumb?")
      .setChoiceValues(choices)
      .setRequired(true);

  // Set the form trigger
  ScriptApp.newTrigger('logDataFromForm')
           .forForm(form)
           .onFormSubmit()
           .create();
}

结果是:

Result

有效!

然后,我尝试在另一个项目上进行测试,就像第一次一样,它已绑定到预制表格。现在,testTrigger()由绑定表单的onSubmit触发器触发。 trigger snapshot 这是它的外观(其他功能相同):

function testTrigger(e) {  
// Attach to a programmatically made form from a function
  var itemResponses = e.response.getItemResponses();
  var submissionData = [itemResponses[0].getResponse(), itemResponses[1].getResponse()];
  createVerificationForm(submissionData);
}

这是结果:

Result

它不起作用!我无法真正确认这一点,但基于此测试,可能该事件对象未返回响应,因为它与绑定到已创建触发器的表单的脚本有关(在这种情况下,onSubmit )。

1 个答案:

答案 0 :(得分:1)

我最近开发了类似的功能,效果很好。您可能要与我编写的代码进行比较。看看createNewForm()和SubmitForm(e)函数here

您可能还希望仅查看response数组的内容。我注意到JSON.stringify并未在此处显示所有内容。在这种情况下,您将无法使用调试器,因此必须使用Logger.log()函数来查看响应中真正包含的内容。