Formulare und Antworten abrufen

Mit der Google Forms API können Sie Formularinhalte, Einstellungen und Metadaten sowie die Antworten der Endnutzer auf das Formular abrufen. Auf dieser Seite wird beschrieben, wie Sie diese Aufgaben ausführen.

Hinweis

Führen Sie die folgenden Aufgaben aus, bevor Sie mit den Aufgaben auf dieser Seite fortfahren:

  • Folgen Sie der Anleitung für das Early Adopter-Programm, um die Autorisierung/Authentifizierung und die Einrichtung der Anmeldedaten abzuschließen.

Formularinhalte und ‑metadaten abrufen

Wenn Sie den Inhalt, die Einstellungen und die Metadaten eines Formulars abrufen möchten, rufen Sie die Methode forms.get() mit der Formular-ID auf.

Python

forms/snippets/retrieve_contents.py
from apiclient import discovery
from httplib2 import Http
from oauth2client import client, file, tools

SCOPES = "https://www.googleapis.com/auth/forms.body.readonly"
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1"

store = file.Storage("token.json")
creds = None
if not creds or creds.invalid:
  flow = client.flow_from_clientsecrets("client_secrets.json", SCOPES)
  creds = tools.run_flow(flow, store)
service = discovery.build(
    "forms",
    "v1",
    http=creds.authorize(Http()),
    discoveryServiceUrl=DISCOVERY_DOC,
    static_discovery=False,
)

# Prints the title of the sample form:
form_id = "<YOUR_FORM_ID>"
result = service.forms().get(formId=form_id).execute()
print(result)

Node.js

forms/snippets/get_form.js
import path from 'path';
import {forms} from '@googleapis/forms';
import {authenticate} from '@google-cloud/local-auth';

const formID = '<YOUR_FORM_ID>';

async function getForm(query) {
  const auth = await authenticate({
    keyfilePath: path.join(__dirname, 'credentials.json'),
    scopes: 'https://www.googleapis.com/auth/forms.body.readonly',
  });
  const formsClient = forms({
    version: 'v1',
    auth: auth,
  });
  const res = await formsClient.forms.get({formId: formID});
  console.log(res.data);
  return res.data;
}

Alle Formularantworten abrufen

Wenn Sie alle Antworten aus einem Formular abrufen möchten, rufen Sie die Methode forms.responses.list() mit der Formular-ID auf.

Python

forms/snippets/retrieve_all_responses.py
from apiclient import discovery
from httplib2 import Http
from oauth2client import client, file, tools

SCOPES = "https://www.googleapis.com/auth/forms.responses.readonly"
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1"

store = file.Storage("token.json")
creds = None
if not creds or creds.invalid:
  flow = client.flow_from_clientsecrets("client_secrets.json", SCOPES)
  creds = tools.run_flow(flow, store)
service = discovery.build(
    "forms",
    "v1",
    http=creds.authorize(Http()),
    discoveryServiceUrl=DISCOVERY_DOC,
    static_discovery=False,
)

# Prints the responses of your specified form:
form_id = "<YOUR_FORM_ID>"
result = service.forms().responses().list(formId=form_id).execute()
print(result)

Node.js

forms/snippets/get_all_responses.js
import path from 'path';
import {forms} from '@googleapis/forms';
import {authenticate} from '@google-cloud/local-auth';

const formID = '<YOUR_FORM_ID>';

async function getAllResponses() {
  const auth = await authenticate({
    keyfilePath: path.join(__dirname, 'credentials.json'),
    scopes: 'https://www.googleapis.com/auth/forms.responses.readonly',
  });
  const formsClient = forms({
    version: 'v1',
    auth: auth,
  });
  const res = await formsClient.forms.responses.list({
    formId: formID,
  });
  console.log(res.data);
  return res.data;
}

Einzelne Formularantwort abrufen

Wenn Sie eine bestimmte Antwort aus einem Formular abrufen möchten, rufen Sie die Methode forms.responses.get() mit der Formular-ID und der Antwort-ID auf.

Python

forms/snippets/retrieve_single_response.py
from apiclient import discovery
from httplib2 import Http
from oauth2client import client, file, tools

SCOPES = "https://www.googleapis.com/auth/forms.responses.readonly"
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1"

store = file.Storage("token.json")
creds = None
if not creds or creds.invalid:
  flow = client.flow_from_clientsecrets("client_secrets.json", SCOPES)
  creds = tools.run_flow(flow, store)
service = discovery.build(
    "forms",
    "v1",
    http=creds.authorize(Http()),
    discoveryServiceUrl=DISCOVERY_DOC,
    static_discovery=False,
)

# Prints the specified response from your form:
form_id = "<YOUR_FORM_ID>"
response_id = "<YOUR_RESPONSE_ID>"
result = (
    service.forms()
    .responses()
    .get(formId=form_id, responseId=response_id)
    .execute()
)
print(result)

Node.js

forms/snippets/get_single_response.js
import path from 'path';
import {forms} from '@googleapis/forms';
import {authenticate} from '@google-cloud/local-auth';

const formID = '<YOUR_FORM_ID>';
const responseID = '<YOUR_RESPONSE_ID>';

async function getSingleResponse() {
  const auth = await authenticate({
    keyfilePath: path.join(__dirname, 'credentials.json'),
    scopes: 'https://www.googleapis.com/auth/forms.responses.readonly',
  });
  const formsClient = forms({
    version: 'v1',
    auth: auth,
  });
  const res = await formsClient.forms.responses.get({
    formId: formID,
    responseId: responseID,
  });
  console.log(res.data);
  return res.data;
}