gpt4 book ai didi

google-apps-script - 获取 Google 表单的最后回复

转载 作者:行者123 更新时间:2023-12-01 18:22:41 24 4
gpt4 key购买 nike

我正在编写一个脚本来获取最后一次表单提交。我在网上找到了原始示例...

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的值?

最佳答案

首先您需要使用 getItemResponses() 方法。 getItemResponses() 方法获取一份表单提交的所有“Items”。大多数项目都是问题。但有些项目不是问题,例如 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;
};

关于google-apps-script - 获取 Google 表单的最后回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40496046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com