Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 从Google表单获取最后一个响应_Google Apps Script_Google Forms - Fatal编程技术网

Google apps script 从Google表单获取最后一个响应

Google apps script 从Google表单获取最后一个响应,google-apps-script,google-forms,Google Apps Script,Google Forms,我正在编写一个脚本,以获得最后一次表单提交。我在网上找到了香草的例子 function onSubmit() { // Open a form by ID and log the responses to each question. var form = FormApp.openById('1G3e4FTYlsJUl5mKX2v1pYWauG1FO5IUPDIOI6P4hUoA'); var formResponses = form.getResponses(); for (var

我正在编写一个脚本,以获得最后一次表单提交。我在网上找到了香草的例子

function onSubmit() {
  // Open a form by ID and log the responses to each question.
 var form = FormApp.openById('1G3e4FTYlsJUl5mKX2v1pYWauG1FO5IUPDIOI6P4hUoA');
 var formResponses = form.getResponses();
 for (var i = 0; i < formResponses.length; i++) {
   var formResponse = formResponses[i];
   var itemResponses = formResponse.getItemResponses();
   for (var j = 0; j < itemResponses.length; j++) {
     var itemResponse = itemResponses[j];
     Logger.log('Response #%s to the question "%s" was "%s"',
         (i + 1).toString(),
         itemResponse.getItem().getTitle(),
         itemResponse.getResponse());
   }
 }
}

当我查看上面脚本的日志时,会得到
未定义的
。如何输出上次\u响应的值?

首先需要使用
getItemResponses()
方法。
getItemResponses()
方法获取一个表单提交的所有“项”。大多数项目都是问题。但有些项目不是问题,比如分页符。因此,您需要排除诸如分页符之类的内容。我已经将代码中的项目称为问题,但项目并不总是问题

function getArrayOfOneSubmissionsAnswers() {
  var allQuestions,i,itemType,L,thisAnswer,thisQuestion,thisSubmissionsAnswers;
    //Define all variables without assigning a value - value at this point
    //is undefined

  allQuestions = FormApp.openById('your_Form_ID').getResponses()[0].getItemResponses();

  L = allQuestions.length;//How many questions are there in the Form
  thisSubmissionsAnswers = [];//Create an empty array for answers

  for (i=0;i<L;i++) {//Loop through all the questions in this Form submission
    thisQuestion = allQuestions[i];//Get this question

    itemType = thisQuestion.getItem().getType();

    if (itemType === FormApp.ItemType.PAGE_BREAK) {
      continue;//keep looping
    };

    thisAnswer = thisQuestion.getResponse();//Get the answer
    Logger.log(i + " - " + thisAnswer); 

    thisSubmissionsAnswers.push(thisAnswer);//add answer to the array
  };

  Logger.log("thisSubmissionsAnswers: " + thisSubmissionsAnswers); 

  return thisSubmissionsAnswers;
};
函数getArrayFonesSubmissionsAnswers(){
var allQuestions,i,itemType,L,thisAnswer,thisAnswer,thisSubmissionsAnswers;
//定义所有变量,但此时不指定值-值
//是未定义的
allQuestions=FormApp.openById('your_Form_ID')。getResponses()[0]。getItemResponses();
L=allQuestions.length;//表单中有多少个问题
thisSubmissionsAnswers=[];//为答案创建一个空数组

对于(i=0;i仅针对子孙后代)这里是对上面Sandy脚本的稍加修改的版本,它给出了从表单提交的最后一个响应

function getArrayOfLastSubmissionsAnswers() {
  var allQuestions,i,itemType,L,thisAnswer,thisQuestion,thisSubmissionsAnswers,number_of_submissions;
    //Define all variables without assigning a value - value at this point
    //is undefined

  number_of_submissions = FormApp.openById('your_Form_ID').getResponses().length;
  allQuestions = FormApp.openById('your_Form_ID').getResponses()[number_of_submissions - 1].getItemResponses();

  L = allQuestions.length;//How many questions are there in the Form
  thisSubmissionsAnswers = [];//Create an empty array for answers

  for (i=0;i<L;i++) {//Loop through all the questions in this Form submission
    thisQuestion = allQuestions[i];//Get this question

    itemType = thisQuestion.getItem().getType();

    if (itemType === FormApp.ItemType.PAGE_BREAK) {
      continue;//keep looping
    };

    thisAnswer = thisQuestion.getResponse();//Get the answer
    Logger.log(i + " - " + thisAnswer); 

    thisSubmissionsAnswers.push(thisAnswer);//add answer to the array
  };

  Logger.log("thisSubmissionsAnswers: " + thisSubmissionsAnswers); 

  return thisSubmissionsAnswers;
};
函数getArrayOfLastSubmissionsAnswers(){
var allQuestions,i,itemType,L,thisAnswer,thisAnswer,thisSubmissionsAnswers,提交数量;
//定义所有变量,但此时不指定值-值
//是未定义的
提交数量=FormApp.openById(“您的表单ID”).getResponses().length;
allQuestions=FormApp.openById('your_Form_ID').getResponses()[number_of_submissions-1].getItemResponses();
L=allQuestions.length;//表单中有多少个问题
thisSubmissionsAnswers=[];//为答案创建一个空数组
对于(i=0;i
function getArrayOfLastSubmissionsAnswers() {
  var allQuestions,i,itemType,L,thisAnswer,thisQuestion,thisSubmissionsAnswers,number_of_submissions;
    //Define all variables without assigning a value - value at this point
    //is undefined

  number_of_submissions = FormApp.openById('your_Form_ID').getResponses().length;
  allQuestions = FormApp.openById('your_Form_ID').getResponses()[number_of_submissions - 1].getItemResponses();

  L = allQuestions.length;//How many questions are there in the Form
  thisSubmissionsAnswers = [];//Create an empty array for answers

  for (i=0;i<L;i++) {//Loop through all the questions in this Form submission
    thisQuestion = allQuestions[i];//Get this question

    itemType = thisQuestion.getItem().getType();

    if (itemType === FormApp.ItemType.PAGE_BREAK) {
      continue;//keep looping
    };

    thisAnswer = thisQuestion.getResponse();//Get the answer
    Logger.log(i + " - " + thisAnswer); 

    thisSubmissionsAnswers.push(thisAnswer);//add answer to the array
  };

  Logger.log("thisSubmissionsAnswers: " + thisSubmissionsAnswers); 

  return thisSubmissionsAnswers;
};