2016-11-08 3 views
1

私は最後のフォーム提出を得るためにスクリプトを作成しています。私はundefinedを得る上で、私はスクリプトのログを表示するとGoogleフォームからの最後の返信を取得

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()); 
    } 
} 
} 

私は

function onSubmit() { 
    // Open a form by ID and log the responses to each question. 
var form = FormApp.openById('1G3e4FTYlsJUl5mKX2v1pYWauG1FO5IUPDIOI6P4hUoA'); 
var formResponses = form.getResponses(); 

var last_response = formResponses[formResponses.length - 1]; 
    Logger.log(last_response.val) 
} 

...にこれを変更した...オンラインバニラ例を見つけました。 last_responseの値を出力するにはどうすればよいですか?

答えて

1

最初にgetItemResponses()メソッドを使用する必要があります。 getItemResponses()メソッドは、1つのフォーム送信のすべての「アイテム」を取得します。ほとんどの項目が質問です。しかし、PAGE_BREAKのように質問ではない項目があります。だからあなたは改ページのようなものを除外する必要があります。私はこのコードで項目を呼びました質問ですが、項目は常に質問ではありません。

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; 
}; 
0

ちょうど後世のために、私はフォームから提出された最後の応答を私に与える上記の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; 
}; 
関連する問題