Google api - 如何在表单中获取文本?

Google api - how do you get the text in Forms?

当我在表单文件上调用 getContent() 时,它 returns 为空/空。

如何获取 Google Drive 中表单文件的文本内容?

你是对的,你需要使用 Forms Service 来提取你的 Google 表单的内容。

如果要获取表单的内容(带选项的问题),需要使用getItems()方法获取表单中所有项目的数组。

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

对于 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,您可以使用(asCheckboxItem()asMultipleChoiceItem()asTextItem() 等)。

如果你想获取表单的内容(问题与回复),你需要使用FormResponse来获取整个表单的回复。

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

您也可以参考这些 SO 线程:

  • Extract Checkbox or Multiple-choice Choices from a Google Form that Already Exists
  • How to extract response items from a Google Form

希望对您有所帮助!