Google Sheets API를 사용하여 스프레드시트 내의 셀과 범위의 서식을 업데이트할 수 있습니다. 이 페이지의 예에서는 Sheets API를 사용하여 일반적인 서식 지정 작업을 수행하는 방법을 보여줍니다. 조건부 서식 레시피 페이지에서 조건부 서식의 예를 더 확인할 수 있습니다.
스프레드시트를 업데이트할 때 일부 요청은 응답을 반환할 수 있습니다.
이러한 값은 배열로 반환되며 각 응답은 해당 요청과 동일한 색인을 차지합니다. 일부 요청에는 응답이 없으며 이러한 요청의 응답은 비어 있습니다. 이러한 예의 응답 구조는 spreadsheets.batchUpdate에서 확인할 수 있습니다.
이러한 예는 언어에 구애받지 않도록 HTTP 요청의 형태로 제공됩니다. Google API 클라이언트 라이브러리를 사용하여 여러 언어로 일괄 업데이트를 구현하는 방법을 알아보려면 스프레드시트 업데이트를 참고하세요.
이 예에서 자리표시자 SPREADSHEET_ID 및 SHEET_ID는 이러한 ID를 제공할 위치를 나타냅니다. 스프레드시트 URL에서 스프레드시트 ID를 확인할 수 있습니다.
spreadsheets.get 메서드를 사용하여 시트 ID를 가져올 수 있습니다. 범위는 A1 표기법을 사용하여 지정됩니다. 범위의 예는 Sheet1!A1:D5입니다.
위 동영상에서는 고정 행 만들기, 셀 굵게 표시, 통화 서식 구현, 셀 유효성 검사 실행, 셀 값 제한 등 다양한 방법으로 스프레드시트 셀의 서식을 지정하는 방법을 알아봅니다.
셀 테두리 수정
다음 spreadsheets.batchUpdate 코드 샘플은 UpdateBordersRequest를 사용하여 A1:F10 범위의 각 셀에 파란색 점선 상단 및 하단 테두리를 지정하는 방법을 보여줍니다. innerHorizontal 필드는 범위 내부에 가로 테두리를 만듭니다.
필드를 생략하면 전체 범위의 상단과 하단에만 테두리가 추가됩니다.
그림 1. 시트에서 각 셀을 파란색 점선 상단 및 하단 테두리로 서식을 지정합니다.
요청 프로토콜은 다음과 같습니다.
POST https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID:batchUpdate
다음 spreadsheets.batchUpdate 코드 샘플은 RepeatCellRequest를 사용하여 시트의 헤더 행을 서식 지정하는 방법을 보여줍니다. 첫 번째 요청은 텍스트 색상, 배경색, 텍스트 글꼴 크기, 텍스트 정렬을 업데이트하고 텍스트를 굵게 만듭니다. range 필드에서 열 색인을 생략하면 전체 행의 형식이 지정됩니다. 두 번째 요청은 헤더 행이 고정되도록 시트 속성을 조정합니다.
그림 2. 시트에서 헤더 행의 형식을 지정합니다.
요청 프로토콜은 다음과 같습니다.
POST https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID:batchUpdate
다음 spreadsheets.batchUpdate 코드 샘플은 RepeatCellRequest를 사용하여 맞춤 날짜/시간 및 숫자 형식을 갖도록 셀을 업데이트하는 방법을 보여줍니다. 첫 번째 요청은 범위 A1:A10의 셀에 맞춤 날짜/시간 형식 hh:mm:ss am/pm,
ddd mmm dd yyyy를 부여합니다. 이 형식의 날짜 시간 예는 '02:05:07 PM, Sun Apr
03 2016'입니다.
두 번째 요청은 B1:B10의 셀에 맞춤 숫자 형식 #,##0.0000을 부여합니다. 이는 숫자가 쉼표 구분 기호로 그룹화되어야 하고, 소수점 뒤에 4자리가 있어야 하며, 선행 0 하나를 제외한 모든 선행 0이 삭제되어야 함을 나타냅니다. 예를 들어 숫자 '3.14'는 '3.1400'으로 렌더링되고 '12345.12345'는 '12,345.1235'로 렌더링됩니다.
[[["이해하기 쉬움","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(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```"]]