示例
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本部分提供了一个 codelab,您可以通过它熟悉 Google Sheets API。此外,本页还提供了一组“配方”示例,演示了如何将预期的 Google 表格操作转换为 API 请求。
通常,您可以通过多种方式使用该 API 完成给定的任务。在决定如何处理任务时,请注意以下事项:
食谱
本部分列出的示例演示了如何将 Google 表格中的常见操作表示为 Sheets API v4 请求。
这些示例以 HTTP 请求的形式呈现,不涉及具体语言。如需了解如何使用 Google API 客户端库以特定语言实现 Sheets API 请求协议,请参阅读取和写入单元格值和更新电子表格指南。
本部分中的食谱分为以下几类:
- 基本读取 - 显示如何从工作表中读取值的配方。
- 基本写入 - 显示如何将值写入工作表的配方。
- 基本格式设置 - 展示如何更改工作表和单元格外观的方案。
- 图表 - 显示如何在工作表中创建和更改图表的配方。
- 条件格式 - 显示如何根据条件更改单元格外观的配方。
- 数据操作 - 显示如何在电子表格中创建、移动和操控数据的配方。
- 命名范围和受保护的范围 - 演示如何在电子表格中创建、更新和移除命名范围和受保护的范围的配方。
- 数据透视表 - 演示如何在工作表中创建数据透视表的配方。
- 行和列操作 - 显示如何添加、移除和移动行和列,以及如何更新其属性的配方。
- 工作表操作 - 显示如何创建、清除、复制和删除工作表以及如何控制其属性的配方。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[],[],null,["# Samples\n\nThis section presents a\n[codelab](https://codelabs.developers.google.com/codelabs/sheets-api/) you can\nuse to become familiar with the Google Sheets API. Also provided are a set of\n\"recipe\" examples that demonstrate how to translate an intended Google Sheets\naction into an API request.\n\nOften there's more than one way to complete a given task with the API. When\nyou're deciding on how to approach a task, keep the following in mind:\n\n- If you need to read or write cell values, the [`spreadsheets.values`](/workspace/sheets/api/reference/rest/v4/spreadsheets.values) collection is a better choice than the [`spreadsheets`](/workspace/sheets/api/reference/rest/v4/spreadsheets) collection. The former's interface is easier to use for simple read/write operations.\n- Wherever possible, use the batch methods\n ([`spreadsheet.batchUpdate`](/workspace/sheets/api/reference/rest/v4/spreadsheets/batchUpdate),\n [`spreadsheet.values.batchGet`](/workspace/sheets/api/reference/rest/v4/spreadsheets.values/batchGet),\n and\n [`spreadsheet.values.batchUpdate`](/workspace/sheets/api/reference/rest/v4/spreadsheets.values/batchUpdate))\n to bundle multiple requests into a single method call. Using these batch\n methods improves efficiency as they:\n\n - Reduce client HTTP overhead.\n - Reduce the number of queries made.\n - Reduce the number of revisions on the doc.\n - Ensure atomicity of all the changes in the batch.\n\nRecipes\n-------\n\nThe examples listed in this section demonstrate how to express common actions in\nSheets as Sheets API v4 requests.\n\nThese examples are presented in the form of HTTP requests to be language\nneutral. To learn how to implement Sheets API request protocols in a\nspecific language using Google API client libraries, see the [Read \\& write cell\nvalues](/workspace/sheets/api/guides/values) and [Update\nspreadsheets](/workspace/sheets/api/guides/batchupdate) guides.\n\nRecipes in this section are divided into the following categories:\n\n- [Basic reading](/workspace/sheets/api/samples/reading)---Recipes that show how to read values from a sheet.\n- [Basic writing](/workspace/sheets/api/samples/writing)---Recipes that show how to write values to a sheet.\n- [Basic formatting](/workspace/sheets/api/samples/formatting)---Recipes that show how to change the appearance of sheets and cells.\n- [Charts](/workspace/sheets/api/samples/charts)---Recipes that show how to create and alter charts in a sheet.\n- [Conditional formatting](/workspace/sheets/api/samples/conditional-formatting)--- Recipes that show how to alter cell appearance based on conditions.\n- [Data operations](/workspace/sheets/api/samples/data)---Recipes that show how to create, move, and manipulate data in a spreadsheet.\n- [Named \\& protected\n ranges](/workspace/sheets/api/samples/ranges)---Recipes that show how to create, update, and remove named and protected ranges in a spreadsheet.\n- [Pivot tables](/workspace/sheets/api/samples/pivot-tables)---Recipes that show how to create pivot tables in a sheet.\n- [Row \\& column\n operations](/workspace/sheets/api/samples/rowcolumn)---Recipes that show how to add, remove, and move rows and columns, and update their properties.\n- [Sheet operations](/workspace/sheets/api/samples/sheet)---Recipes that show how to create, clear, copy, and delete sheets, and also control their properties."]]