คุณอัปเดตรูปแบบของเซลล์และช่วงภายในสเปรดชีตได้โดยใช้ Google ชีต API ตัวอย่างในหน้านี้แสดงให้เห็นว่าการดำเนินการจัดรูปแบบที่พบบ่อยบางอย่างสามารถทำได้ด้วยชีต API อย่างไร ดูตัวอย่างการจัดรูปแบบตามเงื่อนไขเพิ่มเติมได้ในหน้าสูตรการจัดรูปแบบตามเงื่อนไข
ตัวอย่างเหล่านี้จะแสดงในรูปแบบคำขอ HTTP เพื่อให้เป็นกลางทางภาษา ดูวิธีใช้การอัปเดตแบบเป็นกลุ่มในภาษาต่างๆ โดยใช้ไลบรารีของไคลเอ็นต์ Google API ได้ที่อัปเดต สเปรดชีต
[[["เข้าใจง่าย","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-04 UTC"],[],[],null,["# Basic formatting\n\nYou can update the formatting of cells and ranges within spreadsheets using the\nGoogle Sheets API. The examples on this page illustrate how some common formatting\noperations can be achieved with the Sheets API. You can find more\nconditional formatting examples in the [Conditional\nformatting](/workspace/sheets/api/samples/conditional-formatting) recipe page.\n\nWhen updating a spreadsheet, some kinds of requests might return responses.\nThese are returned in an array, with each response occupying the same index as\nthe corresponding request. Some requests don't have responses and for those the\nresponse is empty. The response structure for these examples can be found under\n[`spreadsheets.batchUpdate`](/workspace/sheets/api/reference/rest/v4/spreadsheets/batchUpdate#response-body).\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.\nYou can get the [sheet ID](/workspace/sheets/api/guides/concepts#sheet) by using\nthe\n[`spreadsheets.get`](/workspace/sheets/api/reference/rest/v4/spreadsheets/get)\nmethod. The ranges are specified using [A1\nnotation](/workspace/sheets/api/guides/concepts#cell). An example range is\nSheet1!A1:D5.\n\nIn the above video, you learn how to format spreadsheet cells in various ways,\nincluding: creating frozen rows, bolding cells, implementing currency\nformatting, performing cell validation, and restricting cell values.\n\nEdit cell borders\n-----------------\n\nThe following\n[`spreadsheets.batchUpdate`](/workspace/sheets/api/reference/rest/v4/spreadsheets/batchUpdate)\ncode sample shows how to use the\n[`UpdateBordersRequest`](/workspace/sheets/api/reference/rest/v4/spreadsheets/request#updatebordersrequest)\nto give each cell in the A1:F10 range a dashed, blue top and bottom border. The\n`innerHorizontal` field creates horizontal borders on the interior of the range.\nOmitting the field would result in borders only being added to the top and\nbottom of the entire range.\n**Figure 1.** Format each cell with a dashed, blue top and bottom border in a sheet.\n\nThe request protocol is shown below. \n\n```\nPOST https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID:batchUpdate\n``` \n\n```scdoc\n{\n \"requests\": [\n {\n \"updateBorders\": {\n \"range\": {\n \"sheetId\": SHEET_ID,\n \"startRowIndex\": 0,\n \"endRowIndex\": 10,\n \"startColumnIndex\": 0,\n \"endColumnIndex\": 6\n },\n \"top\": {\n \"style\": \"DASHED\",\n \"width\": 1,\n \"color\": {\n \"blue\": 1.0\n },\n },\n \"bottom\": {\n \"style\": \"DASHED\",\n \"width\": 1,\n \"color\": {\n \"blue\": 1.0\n },\n },\n \"innerHorizontal\": {\n \"style\": \"DASHED\",\n \"width\": 1,\n \"color\": {\n \"blue\": 1.0\n },\n },\n }\n }\n ]\n}\n```\n\nFormat a header row\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 format a header row in a sheet. The first request updates the text color, the\nbackground color, the text font size, and the text justification and makes the\ntext bold. Omitting the column indices in the `range` field causes the entire\nrow to be formatted. The second request adjusts the sheet properties so that the\nheader row is frozen.\n**Figure 2.** Format the header row in a sheet.\n\nThe request protocol is shown below. \n\n```\nPOST https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID:batchUpdate\n``` \n\n```verilog\n{\n \"requests\": [\n {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets/request#repeatcellrequest\": {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#gridrange\": {\n \"sheetId\": SHEET_ID,\n \"startRowIndex\": 0,\n \"endRowIndex\": 1\n },\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#celldata\": {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#cellformat\": {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#color\": {\n \"red\": 0.0,\n \"green\": 0.0,\n \"blue\": 0.0\n },\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#horizontalalign\" : \"CENTER\",\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#textformat\": {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#color\": {\n \"red\": 1.0,\n \"green\": 1.0,\n \"blue\": 1.0\n },\n \"fontSize\": 12,\n \"bold\": true\n }\n }\n },\n \"fields\": \"userEnteredFormat(backgroundColor,textFormat,horizontalAlignment)\"\n }\n },\n {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets/request#updatesheetpropertiesrequest\": {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#sheetproperties\": {\n \"sheetId\": SHEET_ID,\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#gridproperties\": {\n \"frozenRowCount\": 1\n }\n },\n \"fields\": \"gridProperties.frozenRowCount\"\n }\n }\n ]\n}\n```\n\nMerge cells\n-----------\n\nThe following\n[`spreadsheets.batchUpdate`](/workspace/sheets/api/reference/rest/v4/spreadsheets/batchUpdate)\ncode sample shows how to use the\n[`MergeCellsRequest`](/workspace/sheets/api/reference/rest/v4/spreadsheets/request#mergecellsrequest)\nto merge cells. The first request merges the A1:B2 range into a single cell. The\nsecond request merges the columns in A3:B6, while leaving the rows separated.\n**Figure 3.** Merge cells in a sheet.\n\nThe request protocol is shown below. \n\n```\nPOST https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID:batchUpdate\n``` \n\n```scdoc\n{\n \"requests\": [\n {\n \"mergeCells\": {\n \"range\": {\n \"sheetId\": SHEET_ID,\n \"startRowIndex\": 0,\n \"endRowIndex\": 2,\n \"startColumnIndex\": 0,\n \"endColumnIndex\": 2\n },\n \"mergeType\": \"MERGE_ALL\"\n }\n },\n {\n \"mergeCells\": {\n \"range\": {\n \"sheetId\": SHEET_ID,\n \"startRowIndex\": 2,\n \"endRowIndex\": 6,\n \"startColumnIndex\": 0,\n \"endColumnIndex\": 2\n },\n \"mergeType\": \"MERGE_COLUMNS\"\n }\n },\n ]\n}\n```\n\nSet a custom datetime or decimal format for 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 update cells to have custom datetime and number formats. The first request\ngives the cells in the range A1:A10 the custom datetime format `hh:mm:ss am/pm,\nddd mmm dd yyyy`. An example datetime in this format is: \"02:05:07 PM, Sun Apr\n03 2016\".\n\nThe second request gives the cells in B1:B10 the custom number format\n`#,##0.0000`, which indicates that numbers should be grouped with comma\nseparators, that there should be 4 digits after the decimal, and that all but\none leading zero should be dropped. For example, the number \"3.14\" is rendered\nas \"3.1400\", while \"12345.12345\" is rendered as \"12,345.1235\".\n\nFor more information, see [Date and number\nformats](/workspace/sheets/api/guides/formats).\n**Figure 4.** Update cells to have custom datetime and number formats in a sheet.\n\nThe request protocol is shown below. \n\n```\nPOST https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID:batchUpdate\n``` \n\n```carbon\n{\n \"requests\": [\n {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets/request#repeatcellrequest\": {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#gridrange\": {\n \"sheetId\": SHEET_ID,\n \"startRowIndex\": 0,\n \"endRowIndex\": 10,\n \"startColumnIndex\": 0,\n \"endColumnIndex\": 1\n },\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#cellformat\": {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#cellformat\": {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#numberformat\": {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#numberformattype\": \"DATE\",\n \"pattern\": \"hh:mm:ss am/pm, ddd mmm dd yyyy\"\n }\n }\n },\n \"fields\": \"userEnteredFormat.numberFormat\"\n }\n },\n {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets/request#repeatcellrequest\": {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#gridrange\": {\n \"sheetId\": SHEET_ID,\n \"startRowIndex\": 0,\n \"endRowIndex\": 10,\n \"startColumnIndex\": 1,\n \"endColumnIndex\": 2\n },\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#cellformat\": {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#cellformat\": {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#numberformat\": {\n \"/workspace/sheets/api/reference/rest/v4/spreadsheets#numberformattype\": \"NUMBER\",\n \"pattern\": \"#,##0.0000\"\n }\n }\n },\n \"fields\": \"userEnteredFormat.numberFormat\"\n }\n }\n ]\n}\n```"]]