为了让表单创建者能够更好地控制哪些人可以回复,我们将为回复者引入精细的控制功能。2026 年 1 月 31 日之后使用该 API 创建的表单将默认处于未发布状态。如需了解详情,请参阅
Google 表单的 API 变更。
Apps 脚本示例 Web 应用
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本页面简要介绍了 Google Forms API 的 Apps Script Web 应用示例。
前提条件
- 在您的 Google Cloud 项目中启用 Google 表单 API。
- 按照GitHub README 文件中的说明为示例 Web 应用设置一个新的 Apps 脚本项目。
概览
借助 Google 表单 API,任何经过身份验证的客户端都可以直接使用 REST 访问该 API。
由于 Apps Script 提供了一种简单的方法来获取有效的 OAuth 访问令牌并进行 REST 调用,因此可以轻松地将 Apps Script 直接与 Google 表单 API 集成。Apps 脚本已提供现有的 Google 表单服务,可让脚本创建、访问和修改 Google 表单。不过,如果您想直接从 Apps 脚本使用 Google 表单 API,请按如下方式设置 OAuth 访问权限:
获取经过身份验证且具有范围限定的 OAuth 访问令牌。
ScriptApp.getOAuthToken();
在对该 API 的任何 REST 调用中提供的 options 参数中添加 OAuth 令牌。
UrlFetchApp.fetch(Url, options);
如需查看此操作的示例,请参阅设置 Apps 脚本项目。设置指南详细介绍了调用 API 所需的基本管道,不过,为了向最终用户提供对 API 的直观访问方式,您还可以试用和修改我们的示例 Web 应用。
Web 应用示例用法
GitHub 上提供了一个完整的示例 Web 应用,用于演示直接调用所有 Forms API 方法。
运行 Web 应用
设置项目并将其部署为 Web 应用后,您可以尝试执行一些基本操作。
点击创建表单按钮,创建新表单。
新表单现在会显示在表单 ID 字段中,您可以点击在 Google 表单中打开来打开该表单。
在新窗口中打开新表单后,尝试手动添加几个问题,然后点击按 ID 获取表单,您会在右侧的 JSON 输出中看到新问题。
您可以随意探索该应用中提供的其他方法,并观察右侧每项操作的输出。
使用 Google 表单监视器
另请注意表单监视器的按钮。这些脚本使用 Google 表单 API 的“监控”功能,执行创建、列出、删除和续订监控的基本操作。您需要设置一个包含 Cloud Pub/Sub 主题的项目,才能使用这些主题。
如需详细了解 Forms API 监视器和 Cloud Pub/Sub,请参阅设置和接收推送通知。
后续步骤
尝试自定义示例应用以满足您的需求。以下是几个例子:
- 在 Google 表格中创建题库,然后使用 Apps 脚本 SpreadsheetApp 服务从表格中提取题目以填充新表单。
- 或者,您的题库也可以位于 SQL 数据库中,您可以在其中使用 Apps 脚本 JDBC 服务将数据库中的题目填充到表单中。
- 生成测验并将成绩保存到 Google 表格成绩册中。
- 使用 Cloud Pub/Sub 设置您的项目,并使用监控功能生成实时学生排行榜来显示知识问答结果。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-04-09。
[null,null,["最后更新时间 (UTC):2025-04-09。"],[],["This content details using Apps Script with the Google Forms API. Key actions include enabling the Forms API and setting up an Apps Script project. It demonstrates obtaining an OAuth access token via `ScriptApp.getOAuthToken()` and making REST calls with `UrlFetchApp.fetch()`. A web app example allows users to create forms, view them by ID, and interact with API methods. Additionally, it highlights the use of Forms API watches with Cloud Pub/Sub for creating, listing, deleting, and renewing watches. The app can be customized to suit specific needs.\n"],null,["# Apps Script sample web app\n\nThis page provides an overview of the Apps Script web app example for the\nGoogle Forms API.\n\nPrerequisites\n-------------\n\n- Enable the Forms API in your Google Cloud project.\n- Set up a new Apps Script project as described in the [GitHub README file](https://github.com/googleworkspace/apps-script-samples/blob/main/forms-api/demos/AppsScriptFormsAPIWebApp/README.md) for the sample web app.\n\nOverview\n--------\n\nThe Forms API allows any authenticated client to access the API\ndirectly using [REST](https://www.codecademy.com/article/what-is-rest).\n\nBecause Apps Script provides an easy way to obtain both a valid\n[OAuth Access Token](https://oauth.net/2/access-tokens/) and make REST calls,\nit's straightforward to integrate Apps Script directly with the Forms API.\nApps Script already provides an existing\n[Forms Service](/apps-script/reference/forms), which allows scripts to create,\naccess, and modify Google Forms. However, if you prefer to use the\nForms API directly from Apps Script, set up OAuth access as follows:\n\n1. Get a scoped and authenticated OAuth access token.\n\n `ScriptApp.getOAuthToken();`\n2. Include the OAuth token in the *options* argument provided in any REST\n calls to the API.\n\n `UrlFetchApp.fetch(Url, options);`\n\nSee\n[Set up an Apps Script project](/workspace/forms/api/guides/forms-apps-script)\nfor an example of this in action. The setup guide details the basic plumbing\nneeded to call the API---however, to provide intuitive access to the API for\nend users, you can also try out and modify our sample web app.\n\nWeb app example usage\n---------------------\n\nA complete example web app that calls all of the Forms API methods directly\nis available on\n[GitHub](https://github.com/googleworkspace/apps-script-samples/tree/main/forms-api/demos/AppsScriptFormsAPIWebApp)\nfor demonstration purposes.\n\n### Running the web app\n\nOnce you've set up your project and deployed it as a Web app, you can try some\nbasic operations.\n\n1. Create a new form by clicking the **Create Form** button.\n\n The new form is now visible in the **Form id:** field and you can open it by\n clicking **Open in Forms**.\n2. After opening the new form in a separate window, try adding a few questions\n manually, then click **Get Form by Id** and you'll see your new questions\n in the JSON output on the right.\n\nFeel free to explore the other methods available in the app and observe the\noutput on the right for each operation.\n\n### Working with Forms watches\n\nNotice also the buttons for forms\n[watches](/workspace/forms/api/reference/rest/v1/forms.watches). These use the\nForms API watches feature and perform basic operations to\ncreate, list, delete and renew watches. You need to set up a project with\n[Cloud Pub/Sub](https://cloud.google.com/pubsub) topics to use them.\n\nFor more information on Forms API watches and Cloud Pub/Sub, see\n[Set up and receive push notifications](/workspace/forms/api/guides/push-notifications).\n\nNext steps\n----------\n\nTry customizing the example app to suit your needs. Here are a few examples:\n\n- Create a *question bank* in a Google sheet, and then use the Apps Script [SpreadsheetApp](/apps-script/reference/spreadsheet/spreadsheet-app) service to fetch questions from a sheet to populate new forms.\n- Alternatively, your question bank can reside in a SQL database where you can use the Apps Script [JDBC Service](/apps-script/reference/jdbc) to populate forms with questions from the database.\n- Generate a quiz and save the scores in a Google sheet grade book.\n- Set up your project with Cloud Pub/Sub and use the [watches](/workspace/forms/api/guides/push-notifications) feature to generate a real-time student leaderboard to display quiz results."]]