Truy xuất biểu mẫu và câu trả lời

Google Biểu mẫu API cho phép bạn truy xuất nội dung, chế độ cài đặt và siêu dữ liệu của biểu mẫu, cũng như câu trả lời của người dùng cuối cho biểu mẫu. Trang này mô tả cách thực hiện các tác vụ này.

Trước khi bắt đầu

Hãy thực hiện các việc sau trước khi tiếp tục thực hiện các việc trên trang này:

  • Hoàn tất quá trình uỷ quyền/xác thực và thiết lập thông tin đăng nhập theo hướng dẫn của Chương trình người dùng sớm.

Truy xuất nội dung và siêu dữ liệu của biểu mẫu

Để truy xuất nội dung, chế độ cài đặt và siêu dữ liệu của một biểu mẫu, hãy gọi phương thức forms.get() bằng mã nhận dạng biểu mẫu.

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 'node:path';
import {authenticate} from '@google-cloud/local-auth';
import {forms} from '@googleapis/forms';

// TODO: Replace with a valid form ID.
const formID = '<YOUR_FORM_ID>';

/**
 * Retrieves the content of a form.
 */
async function getForm() {
  // Authenticate with Google and get an authorized client.
  const auth = await authenticate({
    keyfilePath: path.join(__dirname, 'credentials.json'),
    scopes: 'https://www.googleapis.com/auth/forms.body.readonly',
  });

  // Create a new Forms API client.
  const formsClient = forms({
    version: 'v1',
    auth,
  });

  // Get the form content.
  const result = await formsClient.forms.get({formId: formID});

  console.log(result.data);
  return result.data;
}

Truy xuất tất cả câu trả lời của biểu mẫu

Để truy xuất tất cả các câu trả lời từ một biểu mẫu, hãy gọi phương thức forms.responses.list() bằng mã nhận dạng biểu mẫu.

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 'node:path';
import {authenticate} from '@google-cloud/local-auth';
import {forms} from '@googleapis/forms';

// TODO: Replace with a valid form ID.
const formID = '<YOUR_FORM_ID>';

/**
 * Retrieves all responses from a form.
 */
async function getAllResponses() {
  // Authenticate with Google and get an authorized client.
  const auth = await authenticate({
    keyfilePath: path.join(__dirname, 'credentials.json'),
    scopes: 'https://www.googleapis.com/auth/forms.responses.readonly',
  });

  // Create a new Forms API client.
  const formsClient = forms({
    version: 'v1',
    auth,
  });

  // Get the list of responses for the form.
  const result = await formsClient.forms.responses.list({
    formId: formID,
  });

  console.log(result.data);
  return result.data;
}

Truy xuất một câu trả lời duy nhất trong biểu mẫu

Để truy xuất một phản hồi cụ thể từ biểu mẫu, hãy gọi phương thức forms.responses.get() bằng mã biểu mẫu và mã phản hồi.

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 'node:path';
import {authenticate} from '@google-cloud/local-auth';
import {forms} from '@googleapis/forms';

// TODO: Replace with a valid form ID.
const formID = '<YOUR_FORM_ID>';
// TODO: Replace with a valid response ID.
const responseID = '<YOUR_RESPONSE_ID>';

/**
 * Retrieves a single response from a form.
 */
async function getSingleResponse() {
  // Authenticate with Google and get an authorized client.
  const auth = await authenticate({
    keyfilePath: path.join(__dirname, 'credentials.json'),
    scopes: 'https://www.googleapis.com/auth/forms.responses.readonly',
  });

  // Create a new Forms API client.
  const formsClient = forms({
    version: 'v1',
    auth,
  });

  // Get the specified response from the form.
  const result = await formsClient.forms.responses.get({
    formId: formID,
    responseId: responseID,
  });

  console.log(result.data);
  return result.data;
}