问题
When i call getContent()
on a Form file, it returns empty / null.
How do I get the textual content of a Form file in Google Drive?
回答1:
You're right that you need to use Forms Service to extract the contents of your Google Form.
If you want to get the contents of the form (questions with choices), you need to use getItems() method to get an array of all items in the form.
// Open a form by ID and log the responses to each question.
var form = FormApp.openById('Form_ID');
// Logs the questions
var items = form.getItems()
for (var i = 0; i < items.length; i++){
Logger.log(items[i].getTitle());
// Logs the choices
if (items[i].getType() == 'CHECKBOX') {
var itemChoices = items[i].asCheckboxItem().getChoices()
for (var j = 0; j < itemChoices.length; j++) {
var choicesValue = itemChoices[j].getValue();
Logger.log(choicesValue)
}
}
};
For getType, you need to specify what type of choices the form has. In the sample code above, I used asCheckboxItem()
to return checkbox items. Here are the list of methods that you can use (asCheckboxItem()
, asMultipleChoiceItem()
, asTextItem()
, etc.).
If you want to get the contents of the form (questions with responses), you need to use FormResponse to get the responses to the form as a whole.
// Open a form by ID and log the responses to each question.
var form = FormApp.openById('Form_ID');
// Logs the questions with responses
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());
}
}
You may also refer with these SO threads:
- Extract Checkbox or Multiple-choice Choices from a Google Form that Already Exists
- How to extract response items from a Google Form
Hope this helps!
来源:https://stackoverflow.com/questions/42122291/google-api-how-do-you-get-the-text-in-forms