POST https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID:batchUpdate
{"requests":[{"setDataValidation":{"range":{"sheetId":SHEET_ID,"startRowIndex":0,"endRowIndex":10,"startColumnIndex":0,"endColumnIndex":4},"rule":{"condition":{"type":"NUMBER_GREATER","values":[{"userEnteredValue":"5"}]},"inputMessage":"Value must be > 5","strict":true}}}]}
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-29 (世界標準時間)。"],[],[],null,["# Data operations\n\nThe Google Sheets API lets you manipulate data within spreadsheets in various\nways. Most functionality that's available to users working with the\nSheets UI is also possible to do with the\nSheets API. The examples on this page illustrate how you can achieve\nsome common spreadsheet operations with the Sheets API.\n\nThese examples are presented in the form of HTTP requests to be language\nneutral. To learn how to implement a batch update in different languages using\nthe Google API client libraries, see [Update\nspreadsheets](/workspace/sheets/api/guides/batchupdate#example).\n\nIn these examples, the placeholders \u003cvar translate=\"no\"\u003eSPREADSHEET_ID\u003c/var\u003e and \u003cvar translate=\"no\"\u003eSHEET_ID\u003c/var\u003e\nindicates where you would provide those IDs. You can find the [spreadsheet\nID](/workspace/sheets/api/guides/concepts#spreadsheet) in the spreadsheet URL. You can get\nthe [sheet ID](/workspace/sheets/api/guides/concepts#sheet) by using the\n[`spreadsheets.get`](/workspace/sheets/api/reference/rest/v4/spreadsheets/get) method. The\nranges are specified using [A1 notation](/workspace/sheets/api/guides/concepts#cell). An\nexample range is Sheet1!A1:D5.\n\nApply data validation to a range\n--------------------------------\n\nThe following\n[`spreadsheets.batchUpdate`](/workspace/sheets/api/reference/rest/v4/spreadsheets/batchUpdate)\ncode sample shows how to use the\n[`SetDataValidationRequest`](/workspace/sheets/api/reference/rest/v4/spreadsheets/request#setdatavalidationrequest)\nto apply a data validation rule, where \"value \\\u003e 5\", to every cell in the range A1:D10.\n\nThe request protocol is shown below. \n\n POST https://sheets.googleapis.com/v4/spreadsheets/\u003cvar translate=\"no\"\u003eSPREADSHEET_ID\u003c/var\u003e:batchUpdate\n\n {\n \"requests\": [\n {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets/request#setdatavalidationrequest\": {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#gridrange\": {\n \"sheetId\": \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-nx\"\u003eSHEET_ID\u003c/span\u003e\u003c/var\u003e,\n \"startRowIndex\": 0,\n \"endRowIndex\": 10,\n \"startColumnIndex\": 0,\n \"endColumnIndex\": 4\n },\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#datavalidationrule\": {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#booleancondition\": {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#conditiontype\": \"NUMBER_GREATER\",\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#conditionvalue\": [\n {\n \"userEnteredValue\": \"5\"\n }\n ]\n },\n \"inputMessage\": \"Value must be \u003e 5\",\n \"strict\": true\n }\n }\n }\n ]\n }\n\nCopy \\& paste cell formatting\n-----------------------------\n\nThe following\n[`spreadsheets.batchUpdate`](/workspace/sheets/api/reference/rest/v4/spreadsheets/batchUpdate)\ncode sample shows how to use the\n[`CopyPasteRequest`](/workspace/sheets/api/reference/rest/v4/spreadsheets/request#copypasterequest)\nto copy the formatting only in the range A1:D10 and paste it to the F1:I10 range\nin the same sheet. The method uses the\n[`PasteType`](/workspace/sheets/api/reference/rest/v4/spreadsheets/request#pastetype) enum\nwith `PASTE_FORMAT` to paste the formatting and data validation only. The\noriginal values in A1:D10 remain unchanged.\n\nThe request protocol is shown below. \n\n POST https://sheets.googleapis.com/v4/spreadsheets/\u003cvar translate=\"no\"\u003eSPREADSHEET_ID\u003c/var\u003e:batchUpdate\n\n {\n \"requests\": [\n {\n \"copyPaste\": {\n \"source\": {\n \"sheetId\": \u003cvar translate=\"no\"\u003eSHEET_ID\u003c/var\u003e,\n \"startRowIndex\": 0,\n \"endRowIndex\": 10,\n \"startColumnIndex\": 0,\n \"endColumnIndex\": 4\n },\n \"destination\": {\n \"sheetId\": \u003cvar translate=\"no\"\u003eSHEET_ID\u003c/var\u003e,\n \"startRowIndex\": 0,\n \"endRowIndex\": 10,\n \"startColumnIndex\": 5,\n \"endColumnIndex\": 9\n },\n \"pasteType\": \"PASTE_FORMAT\",\n \"pasteOrientation\": \"NORMAL\"\n }\n }\n ]\n }\n\nCut \\& paste cells\n------------------\n\nThe following\n[`spreadsheets.batchUpdate`](/workspace/sheets/api/reference/rest/v4/spreadsheets/batchUpdate)\ncode sample shows how to use the\n[`CutPasteRequest`](/workspace/sheets/api/reference/rest/v4/spreadsheets/request#cutpasterequest).\nIt cuts the range A1:D10 and uses the\n[`PasteType`](/workspace/sheets/api/reference/rest/v4/spreadsheets/request#pastetype) enum\nwith `PASTE_NORMAL` to paste its values, formulas, formatting, and merges to the\nF1:I10 range in the same sheet. The original source range cell contents are\nremoved.\n\nThe request protocol is shown below. \n\n POST https://sheets.googleapis.com/v4/spreadsheets/\u003cvar translate=\"no\"\u003eSPREADSHEET_ID\u003c/var\u003e:batchUpdate\n\n {\n \"requests\": [\n {\n \"cutPaste\": {\n \"source\": {\n \"sheetId\": \u003cvar translate=\"no\"\u003eSHEET_ID\u003c/var\u003e,\n \"startRowIndex\": 0,\n \"endRowIndex\": 10,\n \"startColumnIndex\": 0,\n \"endColumnIndex\": 4\n },\n \"destination\": {\n \"sheetId\": \u003cvar translate=\"no\"\u003eSHEET_ID\u003c/var\u003e,\n \"rowIndex\": 0,\n \"columnIndex\": 5\n },\n \"pasteType\": \"PASTE_NORMAL\"\n }\n }\n ]\n }\n\nRepeat a formula over a range\n-----------------------------\n\nThe following\n[`spreadsheets.batchUpdate`](/workspace/sheets/api/reference/rest/v4/spreadsheets/batchUpdate)\ncode sample shows how to use the\n[`RepeatCellRequest`](/workspace/sheets/api/reference/rest/v4/spreadsheets/request#repeatcellrequest)\nto copy the formula `=FLOOR(A1*PI())` to the range B1:D10. The formula's range\nautomatically increments for each row and column in the range, starting with the\nupper left cell. For example, cell B1 has the formula `=FLOOR(A1*PI())`, while\ncell D6 has the formula `=FLOOR(C6*PI())`.\n\nThe request protocol is shown below. \n\n POST https://sheets.googleapis.com/v4/spreadsheets/\u003cvar translate=\"no\"\u003eSPREADSHEET_ID\u003c/var\u003e:batchUpdate\n\n {\n \"requests\": [\n {\n \"repeatCell\": {\n \"range\": {\n \"sheetId\": \u003cvar translate=\"no\"\u003eSHEET_ID\u003c/var\u003e,\n \"startRowIndex\": 0,\n \"endRowIndex\": 10,\n \"startColumnIndex\": 1,\n \"endColumnIndex\": 4\n },\n \"cell\": {\n \"userEnteredValue\": {\n \"formulaValue\": \"=FLOOR(A1*PI())\"\n }\n },\n \"fields\": \"userEnteredValue\"\n }\n }\n ]\n }\n\nSort a range with multiple sorting specifications\n-------------------------------------------------\n\nThe following\n[`spreadsheets.batchUpdate`](/workspace/sheets/api/reference/rest/v4/spreadsheets/batchUpdate)\ncode sample shows how to use the\n[`SortRangeRequest`](/workspace/sheets/api/reference/rest/v4/spreadsheets/request#sortrangerequest)\nto sort the range A1:D10, first by column B in ascending order, then by column C\nin descending order, then by column D in descending order.\n\nThe request protocol is shown below. \n\n POST https://sheets.googleapis.com/v4/spreadsheets/\u003cvar translate=\"no\"\u003eSPREADSHEET_ID\u003c/var\u003e:batchUpdate\n\n {\n \"requests\": [\n {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets/request#sortrangerequest\": {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#gridrange\": {\n \"sheetId\": \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eSHEET_ID\u003c/span\u003e\u003c/var\u003e,\n \"startRowIndex\": 0,\n \"endRowIndex\": 10,\n \"startColumnIndex\": 0,\n \"endColumnIndex\": 4\n },\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#sortspec\": [\n {\n \"dimensionIndex\": 1,\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#sortorder\": \"ASCENDING\"\n },\n {\n \"dimensionIndex\": 2,\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#sortorder\": \"DESCENDING\"\n },\n {\n \"dimensionIndex\": 3,\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#sortorder\": \"DESCENDING\"\n }\n ]\n }\n }\n ]\n }"]]