範例
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本節提供codelab,可協助您熟悉 Google Sheets API。此外,我們也提供一系列「配方」範例,說明如何將預期的 Google 試算表動作轉換為 API 要求。
通常,您可以使用 API 完成特定工作的方式不只一種。決定如何處理工作時,請注意下列事項:
食譜
本節列出的範例會示範如何以 Sheets API v4 要求表示 Google 試算表中的常見動作。
這些範例會以 HTTP 要求的形式呈現,不限於特定語言。如要瞭解如何使用 Google API 用戶端程式庫,以特定語言實作 Google Sheets API 要求通訊協定,請參閱「讀取及寫入儲存格值」和「更新試算表」指南。
本節的食譜分為下列類別:
- 基本讀取:顯示如何從試算表讀取值的食譜。
- 基本寫入:顯示如何將值寫入試算表的食譜。
- 基本格式設定:配方,說明如何變更工作表和儲存格的外觀。
- 圖表:顯示如何在試算表中建立及變更圖表的食譜。
- 條件式格式設定:
顯示如何根據條件變更儲存格外觀的食譜。
- 資料作業:配方,說明如何在試算表中建立、移動及操控資料。
- 已命名和受保護的範圍:這些食譜說明如何在試算表中建立、更新及移除已命名和受保護的範圍。
- 資料透視表:顯示如何在試算表中建立資料透視表的食譜。
- 資料列和資料欄作業:食譜說明如何新增、移除及移動資料列和資料欄,以及更新其屬性。
- 工作表作業:這些範例說明如何建立、清除、複製及刪除工作表,以及如何控管工作表屬性。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-29 (世界標準時間)。
[null,null,["上次更新時間: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."]]