Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

google apps script - Get the current response onSubmit

Is there any way to find out the current response of the Google Form (programmatically)?

In the onSubmit function.

Looked over their documentation but I have to provide the respondId.

How do I find the respondId?

A snipped code from them about this is:

// Open a form by ID and log the responses to each question.
 var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
 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());
   }
 }

UPDATE:

function onSubmit(e) {
  /* Get values entered by filling the form */
  var itemResponses = e.response.getItemResponses();
  var myWantedValue = itemResponses[COLUMN_NUMBER].getResponse();
}

Note: By passing the e(vent) as parameter the values can be accessed by submitting the live form, NOT by the script editor!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This code gives you the values for the last form response, is that what you're looking for ?

function myFunction() {
  var formResponses = FormApp.getActiveForm().getResponses();
  Logger.log(formResponses.length);
  var formResponse = formResponses[formResponses.length-1];
  var itemResponses = formResponse.getItemResponses();
  for (var j = 0; j < itemResponses.length; j++) {
    var itemResponse = itemResponses[j];
    Logger.log('Last response to the question "%s" was "%s"',
               itemResponse.getItem().getTitle(),
               itemResponse.getResponse());
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...