From 0040e6985fabc0f869f705794b7735f437646eb3 Mon Sep 17 00:00:00 2001 From: Mudassir Chapra Date: Thu, 1 Aug 2024 15:16:24 +0500 Subject: [PATCH 1/8] Added a `batch_merge` method [issue #1473] --- gspread/worksheet.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/gspread/worksheet.py b/gspread/worksheet.py index 6a6783b8..cb4e048f 100644 --- a/gspread/worksheet.py +++ b/gspread/worksheet.py @@ -2606,6 +2606,23 @@ def unmerge_cells(self, name: str) -> JSONResponse: return self.client.batch_update(self.spreadsheet_id, body) + def batch_merge( + self, + merges: List[Dict[Literal["range", "mergeType"], Union[str | MergeType]]], + merge_type: MergeType = MergeType.all, + ) -> Any: + requests = [ + { + "merges": { + "range": a1_range_to_grid_range(i["range"], self.id), + "mergeType": i.get("mergeType", merge_type), + } + } + for i in merges + ] + + return self.client.batch_update(self.spreadsheet_id, {"requests": requests}) + def get_notes(self, default_empty_value: Optional[str] = "") -> List[List[str]]: """Returns a list of lists containing all notes in the sheet. From e53e3626823a9608490c0071f75a91060b869259 Mon Sep 17 00:00:00 2001 From: Mudassir Chapra Date: Thu, 1 Aug 2024 15:32:46 +0500 Subject: [PATCH 2/8] Added a docstring. --- gspread/worksheet.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/gspread/worksheet.py b/gspread/worksheet.py index cb4e048f..45e6b52b 100644 --- a/gspread/worksheet.py +++ b/gspread/worksheet.py @@ -2611,6 +2611,20 @@ def batch_merge( merges: List[Dict[Literal["range", "mergeType"], Union[str | MergeType]]], merge_type: MergeType = MergeType.all, ) -> Any: + """Merge multiple ranges at the same time. + + :param merges: list of dictionaries with the ranges(is A1-notation), and + an optional ``MergeType`` field. + See `MergeType`_ in the Sheets API reference. + :type merges: List[Dict[Literal["range", "mergeType"], Union[str | MergeType]]] + :params merge_type: (optional) default ``MergeType`` for all merges missing the merges. + defaults to ``MergeType.all``. + :type merge_type: ``MergeType`` + + :returns: The body of the request repsonse. + :rtype: dict + """ + requests = [ { "merges": { From 3e875da68bb53c828f91531acf14af8c9ee08849 Mon Sep 17 00:00:00 2001 From: Mudassir Chapra Date: Fri, 2 Aug 2024 11:25:44 +0500 Subject: [PATCH 3/8] fixed the values. --- gspread/worksheet.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gspread/worksheet.py b/gspread/worksheet.py index 45e6b52b..d6940db3 100644 --- a/gspread/worksheet.py +++ b/gspread/worksheet.py @@ -2609,7 +2609,7 @@ def unmerge_cells(self, name: str) -> JSONResponse: def batch_merge( self, merges: List[Dict[Literal["range", "mergeType"], Union[str | MergeType]]], - merge_type: MergeType = MergeType.all, + merge_type: MergeType = MergeType.merge_all, ) -> Any: """Merge multiple ranges at the same time. @@ -2618,7 +2618,7 @@ def batch_merge( See `MergeType`_ in the Sheets API reference. :type merges: List[Dict[Literal["range", "mergeType"], Union[str | MergeType]]] :params merge_type: (optional) default ``MergeType`` for all merges missing the merges. - defaults to ``MergeType.all``. + defaults to ``MergeType.merge_row``. :type merge_type: ``MergeType`` :returns: The body of the request repsonse. @@ -2627,7 +2627,7 @@ def batch_merge( requests = [ { - "merges": { + "mergeCells": { "range": a1_range_to_grid_range(i["range"], self.id), "mergeType": i.get("mergeType", merge_type), } From 93e64834cf72d8c81b3a2a7b9330e29ae9cdce8e Mon Sep 17 00:00:00 2001 From: Mudassir Chapra Date: Fri, 2 Aug 2024 11:26:01 +0500 Subject: [PATCH 4/8] Creted a test for batch_merge --- tests/worksheet_test.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/worksheet_test.py b/tests/worksheet_test.py index 61457567..369d9c92 100644 --- a/tests/worksheet_test.py +++ b/tests/worksheet_test.py @@ -212,6 +212,43 @@ def test_get_values_and_combine_merged_cells(self): values_with_merged = self.sheet.get_values("A1:D4", combine_merged_cells=True) self.assertEqual(values_with_merged, expected_merge) + @pytest.mark.vcr() + def test_batch_merged_cells(self): + self.sheet.resize(4, 4) + sheet_data = [ + ["1", "", "", ""], + ["", "", "title", ""], + ["", "", "2", ""], + ["num", "val", "", "0"], + ] + + self.sheet.update(sheet_data, "A1:D4") + + self.sheet.batch_merge( + [ + {"range": "A1:B2"}, + {"range": "C2:D2"}, + {"range": "C3:C4"}, + ] + ) + + expected_merge = [ + ["1", "1", "", ""], + ["1", "1", "title", "title"], + ["", "", "2", ""], + ["num", "val", "2", "0"], + ] + + values = self.sheet.get_values() + values_with_merged = self.sheet.get_values(combine_merged_cells=True) + + self.assertEqual(values, sheet_data) + self.assertEqual(values_with_merged, expected_merge) + + # test with cell address + values_with_merged = self.sheet.get_values("A1:D4", combine_merged_cells=True) + self.assertEqual(values_with_merged, expected_merge) + @pytest.mark.vcr() def test_get_values_with_args_or_kwargs(self): # test that get_values accepts args and kwargs From 53ee0296e3c180f3f92cb719954aad0fb6f1e640 Mon Sep 17 00:00:00 2001 From: alifeee Date: Tue, 24 Sep 2024 17:47:04 +0100 Subject: [PATCH 5/8] add cassette for test --- ...WorksheetTest.test_batch_merged_cells.json | 983 ++++++++++++++++++ 1 file changed, 983 insertions(+) create mode 100644 tests/cassettes/WorksheetTest.test_batch_merged_cells.json diff --git a/tests/cassettes/WorksheetTest.test_batch_merged_cells.json b/tests/cassettes/WorksheetTest.test_batch_merged_cells.json new file mode 100644 index 00000000..afb0c208 --- /dev/null +++ b/tests/cassettes/WorksheetTest.test_batch_merged_cells.json @@ -0,0 +1,983 @@ +{ + "version": 1, + "interactions": [ + { + "request": { + "method": "POST", + "uri": "https://www.googleapis.com/drive/v3/files?supportsAllDrives=True", + "body": "{\"name\": \"Test WorksheetTest test_batch_merged_cells\", \"mimeType\": \"application/vnd.google-apps.spreadsheet\"}", + "headers": { + "User-Agent": [ + "python-requests/2.32.3" + ], + "Accept-Encoding": [ + "gzip, deflate" + ], + "Accept": [ + "*/*" + ], + "Connection": [ + "keep-alive" + ], + "Content-Length": [ + "109" + ], + "Content-Type": [ + "application/json" + ], + "authorization": [ + "" + ] + } + }, + "response": { + "status": { + "code": 200, + "message": "OK" + }, + "headers": { + "Transfer-Encoding": [ + "chunked" + ], + "X-Frame-Options": [ + "SAMEORIGIN" + ], + "Content-Type": [ + "application/json; charset=UTF-8" + ], + "Alt-Svc": [ + "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000" + ], + "Expires": [ + "Mon, 01 Jan 1990 00:00:00 GMT" + ], + "Server": [ + "ESF" + ], + "Pragma": [ + "no-cache" + ], + "Date": [ + "Tue, 24 Sep 2024 16:46:38 GMT" + ], + "X-Content-Type-Options": [ + "nosniff" + ], + "X-XSS-Protection": [ + "0" + ], + "Cache-Control": [ + "no-cache, no-store, max-age=0, must-revalidate" + ], + "Vary": [ + "Origin, X-Origin" + ], + "content-length": [ + "196" + ] + }, + "body": { + "string": "{\n \"kind\": \"drive#file\",\n \"id\": \"1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY\",\n \"name\": \"Test WorksheetTest test_batch_merged_cells\",\n \"mimeType\": \"application/vnd.google-apps.spreadsheet\"\n}\n" + } + } + }, + { + "request": { + "method": "GET", + "uri": "https://sheets.googleapis.com/v4/spreadsheets/1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY?includeGridData=false", + "body": null, + "headers": { + "User-Agent": [ + "python-requests/2.32.3" + ], + "Accept-Encoding": [ + "gzip, deflate" + ], + "Accept": [ + "*/*" + ], + "Connection": [ + "keep-alive" + ], + "authorization": [ + "" + ] + } + }, + "response": { + "status": { + "code": 200, + "message": "OK" + }, + "headers": { + "Transfer-Encoding": [ + "chunked" + ], + "X-Frame-Options": [ + "SAMEORIGIN" + ], + "Content-Type": [ + "application/json; charset=UTF-8" + ], + "Alt-Svc": [ + "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000" + ], + "Server": [ + "ESF" + ], + "Date": [ + "Tue, 24 Sep 2024 16:46:39 GMT" + ], + "x-l2-request-path": [ + "l2-managed-6" + ], + "X-Content-Type-Options": [ + "nosniff" + ], + "X-XSS-Protection": [ + "0" + ], + "Cache-Control": [ + "private" + ], + "Vary": [ + "Origin", + "X-Origin", + "Referer" + ], + "content-length": [ + "3340" + ] + }, + "body": { + "string": "{\n \"spreadsheetId\": \"1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY\",\n \"properties\": {\n \"title\": \"Test WorksheetTest test_batch_merged_cells\",\n \"locale\": \"en_US\",\n \"autoRecalc\": \"ON_CHANGE\",\n \"timeZone\": \"Etc/GMT\",\n \"defaultFormat\": {\n \"backgroundColor\": {\n \"red\": 1,\n \"green\": 1,\n \"blue\": 1\n },\n \"padding\": {\n \"top\": 2,\n \"right\": 3,\n \"bottom\": 2,\n \"left\": 3\n },\n \"verticalAlignment\": \"BOTTOM\",\n \"wrapStrategy\": \"OVERFLOW_CELL\",\n \"textFormat\": {\n \"foregroundColor\": {},\n \"fontFamily\": \"arial,sans,sans-serif\",\n \"fontSize\": 10,\n \"bold\": false,\n \"italic\": false,\n \"strikethrough\": false,\n \"underline\": false,\n \"foregroundColorStyle\": {\n \"rgbColor\": {}\n }\n },\n \"backgroundColorStyle\": {\n \"rgbColor\": {\n \"red\": 1,\n \"green\": 1,\n \"blue\": 1\n }\n }\n },\n \"spreadsheetTheme\": {\n \"primaryFontFamily\": \"Arial\",\n \"themeColors\": [\n {\n \"colorType\": \"TEXT\",\n \"color\": {\n \"rgbColor\": {}\n }\n },\n {\n \"colorType\": \"BACKGROUND\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 1,\n \"green\": 1,\n \"blue\": 1\n }\n }\n },\n {\n \"colorType\": \"ACCENT1\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.25882354,\n \"green\": 0.52156866,\n \"blue\": 0.95686275\n }\n }\n },\n {\n \"colorType\": \"ACCENT2\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.91764706,\n \"green\": 0.2627451,\n \"blue\": 0.20784314\n }\n }\n },\n {\n \"colorType\": \"ACCENT3\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.9843137,\n \"green\": 0.7372549,\n \"blue\": 0.015686275\n }\n }\n },\n {\n \"colorType\": \"ACCENT4\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.20392157,\n \"green\": 0.65882355,\n \"blue\": 0.3254902\n }\n }\n },\n {\n \"colorType\": \"ACCENT5\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 1,\n \"green\": 0.42745098,\n \"blue\": 0.003921569\n }\n }\n },\n {\n \"colorType\": \"ACCENT6\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.27450982,\n \"green\": 0.7411765,\n \"blue\": 0.7764706\n }\n }\n },\n {\n \"colorType\": \"LINK\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.06666667,\n \"green\": 0.33333334,\n \"blue\": 0.8\n }\n }\n }\n ]\n }\n },\n \"sheets\": [\n {\n \"properties\": {\n \"sheetId\": 0,\n \"title\": \"Sheet1\",\n \"index\": 0,\n \"sheetType\": \"GRID\",\n \"gridProperties\": {\n \"rowCount\": 1000,\n \"columnCount\": 26\n }\n }\n }\n ],\n \"spreadsheetUrl\": \"https://docs.google.com/spreadsheets/d/1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY/edit\"\n}\n" + } + } + }, + { + "request": { + "method": "GET", + "uri": "https://sheets.googleapis.com/v4/spreadsheets/1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY?includeGridData=false", + "body": null, + "headers": { + "User-Agent": [ + "python-requests/2.32.3" + ], + "Accept-Encoding": [ + "gzip, deflate" + ], + "Accept": [ + "*/*" + ], + "Connection": [ + "keep-alive" + ], + "authorization": [ + "" + ] + } + }, + "response": { + "status": { + "code": 200, + "message": "OK" + }, + "headers": { + "Transfer-Encoding": [ + "chunked" + ], + "X-Frame-Options": [ + "SAMEORIGIN" + ], + "Content-Type": [ + "application/json; charset=UTF-8" + ], + "Alt-Svc": [ + "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000" + ], + "Server": [ + "ESF" + ], + "Date": [ + "Tue, 24 Sep 2024 16:46:40 GMT" + ], + "x-l2-request-path": [ + "l2-managed-6" + ], + "X-Content-Type-Options": [ + "nosniff" + ], + "X-XSS-Protection": [ + "0" + ], + "Cache-Control": [ + "private" + ], + "Vary": [ + "Origin", + "X-Origin", + "Referer" + ], + "content-length": [ + "3340" + ] + }, + "body": { + "string": "{\n \"spreadsheetId\": \"1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY\",\n \"properties\": {\n \"title\": \"Test WorksheetTest test_batch_merged_cells\",\n \"locale\": \"en_US\",\n \"autoRecalc\": \"ON_CHANGE\",\n \"timeZone\": \"Etc/GMT\",\n \"defaultFormat\": {\n \"backgroundColor\": {\n \"red\": 1,\n \"green\": 1,\n \"blue\": 1\n },\n \"padding\": {\n \"top\": 2,\n \"right\": 3,\n \"bottom\": 2,\n \"left\": 3\n },\n \"verticalAlignment\": \"BOTTOM\",\n \"wrapStrategy\": \"OVERFLOW_CELL\",\n \"textFormat\": {\n \"foregroundColor\": {},\n \"fontFamily\": \"arial,sans,sans-serif\",\n \"fontSize\": 10,\n \"bold\": false,\n \"italic\": false,\n \"strikethrough\": false,\n \"underline\": false,\n \"foregroundColorStyle\": {\n \"rgbColor\": {}\n }\n },\n \"backgroundColorStyle\": {\n \"rgbColor\": {\n \"red\": 1,\n \"green\": 1,\n \"blue\": 1\n }\n }\n },\n \"spreadsheetTheme\": {\n \"primaryFontFamily\": \"Arial\",\n \"themeColors\": [\n {\n \"colorType\": \"TEXT\",\n \"color\": {\n \"rgbColor\": {}\n }\n },\n {\n \"colorType\": \"BACKGROUND\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 1,\n \"green\": 1,\n \"blue\": 1\n }\n }\n },\n {\n \"colorType\": \"ACCENT1\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.25882354,\n \"green\": 0.52156866,\n \"blue\": 0.95686275\n }\n }\n },\n {\n \"colorType\": \"ACCENT2\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.91764706,\n \"green\": 0.2627451,\n \"blue\": 0.20784314\n }\n }\n },\n {\n \"colorType\": \"ACCENT3\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.9843137,\n \"green\": 0.7372549,\n \"blue\": 0.015686275\n }\n }\n },\n {\n \"colorType\": \"ACCENT4\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.20392157,\n \"green\": 0.65882355,\n \"blue\": 0.3254902\n }\n }\n },\n {\n \"colorType\": \"ACCENT5\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 1,\n \"green\": 0.42745098,\n \"blue\": 0.003921569\n }\n }\n },\n {\n \"colorType\": \"ACCENT6\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.27450982,\n \"green\": 0.7411765,\n \"blue\": 0.7764706\n }\n }\n },\n {\n \"colorType\": \"LINK\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.06666667,\n \"green\": 0.33333334,\n \"blue\": 0.8\n }\n }\n }\n ]\n }\n },\n \"sheets\": [\n {\n \"properties\": {\n \"sheetId\": 0,\n \"title\": \"Sheet1\",\n \"index\": 0,\n \"sheetType\": \"GRID\",\n \"gridProperties\": {\n \"rowCount\": 1000,\n \"columnCount\": 26\n }\n }\n }\n ],\n \"spreadsheetUrl\": \"https://docs.google.com/spreadsheets/d/1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY/edit\"\n}\n" + } + } + }, + { + "request": { + "method": "POST", + "uri": "https://sheets.googleapis.com/v4/spreadsheets/1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY/values/%27Sheet1%27:clear", + "body": null, + "headers": { + "User-Agent": [ + "python-requests/2.32.3" + ], + "Accept-Encoding": [ + "gzip, deflate" + ], + "Accept": [ + "*/*" + ], + "Connection": [ + "keep-alive" + ], + "Content-Length": [ + "0" + ], + "authorization": [ + "" + ] + } + }, + "response": { + "status": { + "code": 200, + "message": "OK" + }, + "headers": { + "Transfer-Encoding": [ + "chunked" + ], + "X-Frame-Options": [ + "SAMEORIGIN" + ], + "Content-Type": [ + "application/json; charset=UTF-8" + ], + "Alt-Svc": [ + "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000" + ], + "Server": [ + "ESF" + ], + "Date": [ + "Tue, 24 Sep 2024 16:46:41 GMT" + ], + "x-l2-request-path": [ + "l2-managed-6" + ], + "X-Content-Type-Options": [ + "nosniff" + ], + "X-XSS-Protection": [ + "0" + ], + "Cache-Control": [ + "private" + ], + "Vary": [ + "Origin", + "X-Origin", + "Referer" + ], + "content-length": [ + "107" + ] + }, + "body": { + "string": "{\n \"spreadsheetId\": \"1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY\",\n \"clearedRange\": \"Sheet1!A1:Z1000\"\n}\n" + } + } + }, + { + "request": { + "method": "POST", + "uri": "https://sheets.googleapis.com/v4/spreadsheets/1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY:batchUpdate", + "body": "{\"requests\": [{\"updateSheetProperties\": {\"properties\": {\"sheetId\": 0, \"gridProperties\": {\"rowCount\": 4, \"columnCount\": 4}}, \"fields\": \"gridProperties/rowCount,gridProperties/columnCount\"}}]}", + "headers": { + "User-Agent": [ + "python-requests/2.32.3" + ], + "Accept-Encoding": [ + "gzip, deflate" + ], + "Accept": [ + "*/*" + ], + "Connection": [ + "keep-alive" + ], + "Content-Length": [ + "190" + ], + "Content-Type": [ + "application/json" + ], + "authorization": [ + "" + ] + } + }, + "response": { + "status": { + "code": 200, + "message": "OK" + }, + "headers": { + "Transfer-Encoding": [ + "chunked" + ], + "X-Frame-Options": [ + "SAMEORIGIN" + ], + "Content-Type": [ + "application/json; charset=UTF-8" + ], + "Alt-Svc": [ + "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000" + ], + "Server": [ + "ESF" + ], + "Date": [ + "Tue, 24 Sep 2024 16:46:42 GMT" + ], + "x-l2-request-path": [ + "l2-managed-6" + ], + "X-Content-Type-Options": [ + "nosniff" + ], + "X-XSS-Protection": [ + "0" + ], + "Cache-Control": [ + "private" + ], + "Vary": [ + "Origin", + "X-Origin", + "Referer" + ], + "content-length": [ + "97" + ] + }, + "body": { + "string": "{\n \"spreadsheetId\": \"1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY\",\n \"replies\": [\n {}\n ]\n}\n" + } + } + }, + { + "request": { + "method": "PUT", + "uri": "https://sheets.googleapis.com/v4/spreadsheets/1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY/values/%27Sheet1%27%21A1%3AD4?valueInputOption=RAW", + "body": "{\"values\": [[\"1\", \"\", \"\", \"\"], [\"\", \"\", \"title\", \"\"], [\"\", \"\", \"2\", \"\"], [\"num\", \"val\", \"\", \"0\"]], \"majorDimension\": null}", + "headers": { + "User-Agent": [ + "python-requests/2.32.3" + ], + "Accept-Encoding": [ + "gzip, deflate" + ], + "Accept": [ + "*/*" + ], + "Connection": [ + "keep-alive" + ], + "Content-Length": [ + "122" + ], + "Content-Type": [ + "application/json" + ], + "authorization": [ + "" + ] + } + }, + "response": { + "status": { + "code": 200, + "message": "OK" + }, + "headers": { + "Transfer-Encoding": [ + "chunked" + ], + "X-Frame-Options": [ + "SAMEORIGIN" + ], + "Content-Type": [ + "application/json; charset=UTF-8" + ], + "Alt-Svc": [ + "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000" + ], + "Server": [ + "ESF" + ], + "Date": [ + "Tue, 24 Sep 2024 16:46:43 GMT" + ], + "x-l2-request-path": [ + "l2-managed-6" + ], + "X-Content-Type-Options": [ + "nosniff" + ], + "X-XSS-Protection": [ + "0" + ], + "Cache-Control": [ + "private" + ], + "Vary": [ + "Origin", + "X-Origin", + "Referer" + ], + "content-length": [ + "169" + ] + }, + "body": { + "string": "{\n \"spreadsheetId\": \"1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY\",\n \"updatedRange\": \"Sheet1!A1:D4\",\n \"updatedRows\": 4,\n \"updatedColumns\": 4,\n \"updatedCells\": 16\n}\n" + } + } + }, + { + "request": { + "method": "POST", + "uri": "https://sheets.googleapis.com/v4/spreadsheets/1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY:batchUpdate", + "body": "{\"requests\": [{\"mergeCells\": {\"range\": {\"startRowIndex\": 0, \"endRowIndex\": 2, \"startColumnIndex\": 0, \"endColumnIndex\": 2, \"sheetId\": 0}, \"mergeType\": \"MERGE_ALL\"}}, {\"mergeCells\": {\"range\": {\"startRowIndex\": 1, \"endRowIndex\": 2, \"startColumnIndex\": 2, \"endColumnIndex\": 4, \"sheetId\": 0}, \"mergeType\": \"MERGE_ALL\"}}, {\"mergeCells\": {\"range\": {\"startRowIndex\": 2, \"endRowIndex\": 4, \"startColumnIndex\": 2, \"endColumnIndex\": 3, \"sheetId\": 0}, \"mergeType\": \"MERGE_ALL\"}}]}", + "headers": { + "User-Agent": [ + "python-requests/2.32.3" + ], + "Accept-Encoding": [ + "gzip, deflate" + ], + "Accept": [ + "*/*" + ], + "Connection": [ + "keep-alive" + ], + "Content-Length": [ + "467" + ], + "Content-Type": [ + "application/json" + ], + "authorization": [ + "" + ] + } + }, + "response": { + "status": { + "code": 200, + "message": "OK" + }, + "headers": { + "Transfer-Encoding": [ + "chunked" + ], + "X-Frame-Options": [ + "SAMEORIGIN" + ], + "Content-Type": [ + "application/json; charset=UTF-8" + ], + "Alt-Svc": [ + "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000" + ], + "Server": [ + "ESF" + ], + "Date": [ + "Tue, 24 Sep 2024 16:46:43 GMT" + ], + "x-l2-request-path": [ + "l2-managed-6" + ], + "X-Content-Type-Options": [ + "nosniff" + ], + "X-XSS-Protection": [ + "0" + ], + "Cache-Control": [ + "private" + ], + "Vary": [ + "Origin", + "X-Origin", + "Referer" + ], + "content-length": [ + "113" + ] + }, + "body": { + "string": "{\n \"spreadsheetId\": \"1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY\",\n \"replies\": [\n {},\n {},\n {}\n ]\n}\n" + } + } + }, + { + "request": { + "method": "GET", + "uri": "https://sheets.googleapis.com/v4/spreadsheets/1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY/values/%27Sheet1%27", + "body": null, + "headers": { + "User-Agent": [ + "python-requests/2.32.3" + ], + "Accept-Encoding": [ + "gzip, deflate" + ], + "Accept": [ + "*/*" + ], + "Connection": [ + "keep-alive" + ], + "authorization": [ + "" + ] + } + }, + "response": { + "status": { + "code": 200, + "message": "OK" + }, + "headers": { + "Transfer-Encoding": [ + "chunked" + ], + "X-Frame-Options": [ + "SAMEORIGIN" + ], + "Content-Type": [ + "application/json; charset=UTF-8" + ], + "Alt-Svc": [ + "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000" + ], + "Server": [ + "ESF" + ], + "Date": [ + "Tue, 24 Sep 2024 16:46:44 GMT" + ], + "x-l2-request-path": [ + "l2-managed-6" + ], + "X-Content-Type-Options": [ + "nosniff" + ], + "X-XSS-Protection": [ + "0" + ], + "Cache-Control": [ + "private" + ], + "Vary": [ + "Origin", + "X-Origin", + "Referer" + ], + "content-length": [ + "248" + ] + }, + "body": { + "string": "{\n \"range\": \"Sheet1!A1:D4\",\n \"majorDimension\": \"ROWS\",\n \"values\": [\n [\n \"1\"\n ],\n [\n \"\",\n \"\",\n \"title\"\n ],\n [\n \"\",\n \"\",\n \"2\"\n ],\n [\n \"num\",\n \"val\",\n \"\",\n \"0\"\n ]\n ]\n}\n" + } + } + }, + { + "request": { + "method": "GET", + "uri": "https://sheets.googleapis.com/v4/spreadsheets/1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY/values/%27Sheet1%27", + "body": null, + "headers": { + "User-Agent": [ + "python-requests/2.32.3" + ], + "Accept-Encoding": [ + "gzip, deflate" + ], + "Accept": [ + "*/*" + ], + "Connection": [ + "keep-alive" + ], + "authorization": [ + "" + ] + } + }, + "response": { + "status": { + "code": 200, + "message": "OK" + }, + "headers": { + "Transfer-Encoding": [ + "chunked" + ], + "X-Frame-Options": [ + "SAMEORIGIN" + ], + "Content-Type": [ + "application/json; charset=UTF-8" + ], + "Alt-Svc": [ + "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000" + ], + "Server": [ + "ESF" + ], + "Date": [ + "Tue, 24 Sep 2024 16:46:45 GMT" + ], + "x-l2-request-path": [ + "l2-managed-6" + ], + "X-Content-Type-Options": [ + "nosniff" + ], + "X-XSS-Protection": [ + "0" + ], + "Cache-Control": [ + "private" + ], + "Vary": [ + "Origin", + "X-Origin", + "Referer" + ], + "content-length": [ + "248" + ] + }, + "body": { + "string": "{\n \"range\": \"Sheet1!A1:D4\",\n \"majorDimension\": \"ROWS\",\n \"values\": [\n [\n \"1\"\n ],\n [\n \"\",\n \"\",\n \"title\"\n ],\n [\n \"\",\n \"\",\n \"2\"\n ],\n [\n \"num\",\n \"val\",\n \"\",\n \"0\"\n ]\n ]\n}\n" + } + } + }, + { + "request": { + "method": "GET", + "uri": "https://sheets.googleapis.com/v4/spreadsheets/1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY?includeGridData=false", + "body": null, + "headers": { + "User-Agent": [ + "python-requests/2.32.3" + ], + "Accept-Encoding": [ + "gzip, deflate" + ], + "Accept": [ + "*/*" + ], + "Connection": [ + "keep-alive" + ], + "authorization": [ + "" + ] + } + }, + "response": { + "status": { + "code": 200, + "message": "OK" + }, + "headers": { + "Transfer-Encoding": [ + "chunked" + ], + "X-Frame-Options": [ + "SAMEORIGIN" + ], + "Content-Type": [ + "application/json; charset=UTF-8" + ], + "Alt-Svc": [ + "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000" + ], + "Server": [ + "ESF" + ], + "Date": [ + "Tue, 24 Sep 2024 16:46:45 GMT" + ], + "x-l2-request-path": [ + "l2-managed-6" + ], + "X-Content-Type-Options": [ + "nosniff" + ], + "X-XSS-Protection": [ + "0" + ], + "Cache-Control": [ + "private" + ], + "Vary": [ + "Origin", + "X-Origin", + "Referer" + ], + "content-length": [ + "3788" + ] + }, + "body": { + "string": "{\n \"spreadsheetId\": \"1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY\",\n \"properties\": {\n \"title\": \"Test WorksheetTest test_batch_merged_cells\",\n \"locale\": \"en_US\",\n \"autoRecalc\": \"ON_CHANGE\",\n \"timeZone\": \"Etc/GMT\",\n \"defaultFormat\": {\n \"backgroundColor\": {\n \"red\": 1,\n \"green\": 1,\n \"blue\": 1\n },\n \"padding\": {\n \"top\": 2,\n \"right\": 3,\n \"bottom\": 2,\n \"left\": 3\n },\n \"verticalAlignment\": \"BOTTOM\",\n \"wrapStrategy\": \"OVERFLOW_CELL\",\n \"textFormat\": {\n \"foregroundColor\": {},\n \"fontFamily\": \"arial,sans,sans-serif\",\n \"fontSize\": 10,\n \"bold\": false,\n \"italic\": false,\n \"strikethrough\": false,\n \"underline\": false,\n \"foregroundColorStyle\": {\n \"rgbColor\": {}\n }\n },\n \"backgroundColorStyle\": {\n \"rgbColor\": {\n \"red\": 1,\n \"green\": 1,\n \"blue\": 1\n }\n }\n },\n \"spreadsheetTheme\": {\n \"primaryFontFamily\": \"Arial\",\n \"themeColors\": [\n {\n \"colorType\": \"TEXT\",\n \"color\": {\n \"rgbColor\": {}\n }\n },\n {\n \"colorType\": \"BACKGROUND\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 1,\n \"green\": 1,\n \"blue\": 1\n }\n }\n },\n {\n \"colorType\": \"ACCENT1\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.25882354,\n \"green\": 0.52156866,\n \"blue\": 0.95686275\n }\n }\n },\n {\n \"colorType\": \"ACCENT2\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.91764706,\n \"green\": 0.2627451,\n \"blue\": 0.20784314\n }\n }\n },\n {\n \"colorType\": \"ACCENT3\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.9843137,\n \"green\": 0.7372549,\n \"blue\": 0.015686275\n }\n }\n },\n {\n \"colorType\": \"ACCENT4\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.20392157,\n \"green\": 0.65882355,\n \"blue\": 0.3254902\n }\n }\n },\n {\n \"colorType\": \"ACCENT5\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 1,\n \"green\": 0.42745098,\n \"blue\": 0.003921569\n }\n }\n },\n {\n \"colorType\": \"ACCENT6\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.27450982,\n \"green\": 0.7411765,\n \"blue\": 0.7764706\n }\n }\n },\n {\n \"colorType\": \"LINK\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.06666667,\n \"green\": 0.33333334,\n \"blue\": 0.8\n }\n }\n }\n ]\n }\n },\n \"sheets\": [\n {\n \"properties\": {\n \"sheetId\": 0,\n \"title\": \"Sheet1\",\n \"index\": 0,\n \"sheetType\": \"GRID\",\n \"gridProperties\": {\n \"rowCount\": 4,\n \"columnCount\": 4\n }\n },\n \"merges\": [\n {\n \"startRowIndex\": 0,\n \"endRowIndex\": 2,\n \"startColumnIndex\": 0,\n \"endColumnIndex\": 2\n },\n {\n \"startRowIndex\": 1,\n \"endRowIndex\": 2,\n \"startColumnIndex\": 2,\n \"endColumnIndex\": 4\n },\n {\n \"startRowIndex\": 2,\n \"endRowIndex\": 4,\n \"startColumnIndex\": 2,\n \"endColumnIndex\": 3\n }\n ]\n }\n ],\n \"spreadsheetUrl\": \"https://docs.google.com/spreadsheets/d/1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY/edit\"\n}\n" + } + } + }, + { + "request": { + "method": "GET", + "uri": "https://sheets.googleapis.com/v4/spreadsheets/1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY/values/%27Sheet1%27%21A1%3AD4", + "body": null, + "headers": { + "User-Agent": [ + "python-requests/2.32.3" + ], + "Accept-Encoding": [ + "gzip, deflate" + ], + "Accept": [ + "*/*" + ], + "Connection": [ + "keep-alive" + ], + "authorization": [ + "" + ] + } + }, + "response": { + "status": { + "code": 200, + "message": "OK" + }, + "headers": { + "Transfer-Encoding": [ + "chunked" + ], + "X-Frame-Options": [ + "SAMEORIGIN" + ], + "Content-Type": [ + "application/json; charset=UTF-8" + ], + "Alt-Svc": [ + "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000" + ], + "Server": [ + "ESF" + ], + "Date": [ + "Tue, 24 Sep 2024 16:46:46 GMT" + ], + "x-l2-request-path": [ + "l2-managed-6" + ], + "X-Content-Type-Options": [ + "nosniff" + ], + "X-XSS-Protection": [ + "0" + ], + "Cache-Control": [ + "private" + ], + "Vary": [ + "Origin", + "X-Origin", + "Referer" + ], + "content-length": [ + "248" + ] + }, + "body": { + "string": "{\n \"range\": \"Sheet1!A1:D4\",\n \"majorDimension\": \"ROWS\",\n \"values\": [\n [\n \"1\"\n ],\n [\n \"\",\n \"\",\n \"title\"\n ],\n [\n \"\",\n \"\",\n \"2\"\n ],\n [\n \"num\",\n \"val\",\n \"\",\n \"0\"\n ]\n ]\n}\n" + } + } + }, + { + "request": { + "method": "GET", + "uri": "https://sheets.googleapis.com/v4/spreadsheets/1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY?includeGridData=false", + "body": null, + "headers": { + "User-Agent": [ + "python-requests/2.32.3" + ], + "Accept-Encoding": [ + "gzip, deflate" + ], + "Accept": [ + "*/*" + ], + "Connection": [ + "keep-alive" + ], + "authorization": [ + "" + ] + } + }, + "response": { + "status": { + "code": 200, + "message": "OK" + }, + "headers": { + "Transfer-Encoding": [ + "chunked" + ], + "X-Frame-Options": [ + "SAMEORIGIN" + ], + "Content-Type": [ + "application/json; charset=UTF-8" + ], + "Alt-Svc": [ + "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000" + ], + "Server": [ + "ESF" + ], + "Date": [ + "Tue, 24 Sep 2024 16:46:47 GMT" + ], + "x-l2-request-path": [ + "l2-managed-6" + ], + "X-Content-Type-Options": [ + "nosniff" + ], + "X-XSS-Protection": [ + "0" + ], + "Cache-Control": [ + "private" + ], + "Vary": [ + "Origin", + "X-Origin", + "Referer" + ], + "content-length": [ + "3788" + ] + }, + "body": { + "string": "{\n \"spreadsheetId\": \"1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY\",\n \"properties\": {\n \"title\": \"Test WorksheetTest test_batch_merged_cells\",\n \"locale\": \"en_US\",\n \"autoRecalc\": \"ON_CHANGE\",\n \"timeZone\": \"Etc/GMT\",\n \"defaultFormat\": {\n \"backgroundColor\": {\n \"red\": 1,\n \"green\": 1,\n \"blue\": 1\n },\n \"padding\": {\n \"top\": 2,\n \"right\": 3,\n \"bottom\": 2,\n \"left\": 3\n },\n \"verticalAlignment\": \"BOTTOM\",\n \"wrapStrategy\": \"OVERFLOW_CELL\",\n \"textFormat\": {\n \"foregroundColor\": {},\n \"fontFamily\": \"arial,sans,sans-serif\",\n \"fontSize\": 10,\n \"bold\": false,\n \"italic\": false,\n \"strikethrough\": false,\n \"underline\": false,\n \"foregroundColorStyle\": {\n \"rgbColor\": {}\n }\n },\n \"backgroundColorStyle\": {\n \"rgbColor\": {\n \"red\": 1,\n \"green\": 1,\n \"blue\": 1\n }\n }\n },\n \"spreadsheetTheme\": {\n \"primaryFontFamily\": \"Arial\",\n \"themeColors\": [\n {\n \"colorType\": \"TEXT\",\n \"color\": {\n \"rgbColor\": {}\n }\n },\n {\n \"colorType\": \"BACKGROUND\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 1,\n \"green\": 1,\n \"blue\": 1\n }\n }\n },\n {\n \"colorType\": \"ACCENT1\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.25882354,\n \"green\": 0.52156866,\n \"blue\": 0.95686275\n }\n }\n },\n {\n \"colorType\": \"ACCENT2\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.91764706,\n \"green\": 0.2627451,\n \"blue\": 0.20784314\n }\n }\n },\n {\n \"colorType\": \"ACCENT3\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.9843137,\n \"green\": 0.7372549,\n \"blue\": 0.015686275\n }\n }\n },\n {\n \"colorType\": \"ACCENT4\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.20392157,\n \"green\": 0.65882355,\n \"blue\": 0.3254902\n }\n }\n },\n {\n \"colorType\": \"ACCENT5\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 1,\n \"green\": 0.42745098,\n \"blue\": 0.003921569\n }\n }\n },\n {\n \"colorType\": \"ACCENT6\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.27450982,\n \"green\": 0.7411765,\n \"blue\": 0.7764706\n }\n }\n },\n {\n \"colorType\": \"LINK\",\n \"color\": {\n \"rgbColor\": {\n \"red\": 0.06666667,\n \"green\": 0.33333334,\n \"blue\": 0.8\n }\n }\n }\n ]\n }\n },\n \"sheets\": [\n {\n \"properties\": {\n \"sheetId\": 0,\n \"title\": \"Sheet1\",\n \"index\": 0,\n \"sheetType\": \"GRID\",\n \"gridProperties\": {\n \"rowCount\": 4,\n \"columnCount\": 4\n }\n },\n \"merges\": [\n {\n \"startRowIndex\": 0,\n \"endRowIndex\": 2,\n \"startColumnIndex\": 0,\n \"endColumnIndex\": 2\n },\n {\n \"startRowIndex\": 1,\n \"endRowIndex\": 2,\n \"startColumnIndex\": 2,\n \"endColumnIndex\": 4\n },\n {\n \"startRowIndex\": 2,\n \"endRowIndex\": 4,\n \"startColumnIndex\": 2,\n \"endColumnIndex\": 3\n }\n ]\n }\n ],\n \"spreadsheetUrl\": \"https://docs.google.com/spreadsheets/d/1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY/edit\"\n}\n" + } + } + }, + { + "request": { + "method": "DELETE", + "uri": "https://www.googleapis.com/drive/v3/files/1z_YjzCNQiR55V8p6KJKYEE4teROoa1OGD0ziuTadlpY?supportsAllDrives=True", + "body": null, + "headers": { + "User-Agent": [ + "python-requests/2.32.3" + ], + "Accept-Encoding": [ + "gzip, deflate" + ], + "Accept": [ + "*/*" + ], + "Connection": [ + "keep-alive" + ], + "Content-Length": [ + "0" + ], + "authorization": [ + "" + ] + } + }, + "response": { + "status": { + "code": 204, + "message": "No Content" + }, + "headers": { + "X-Frame-Options": [ + "SAMEORIGIN" + ], + "Content-Type": [ + "text/html" + ], + "Alt-Svc": [ + "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000" + ], + "Expires": [ + "Mon, 01 Jan 1990 00:00:00 GMT" + ], + "Server": [ + "ESF" + ], + "Pragma": [ + "no-cache" + ], + "Date": [ + "Tue, 24 Sep 2024 16:46:48 GMT" + ], + "Content-Length": [ + "0" + ], + "X-XSS-Protection": [ + "0" + ], + "Cache-Control": [ + "no-cache, no-store, max-age=0, must-revalidate" + ], + "X-Content-Type-Options": [ + "nosniff" + ], + "Vary": [ + "Origin, X-Origin" + ] + }, + "body": { + "string": "" + } + } + } + ] +} From d872ba45499d8a4821e29a0a8dcacc61c5341b87 Mon Sep 17 00:00:00 2001 From: alifeee Date: Tue, 24 Sep 2024 17:48:38 +0100 Subject: [PATCH 6/8] fix spelling --- gspread/worksheet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gspread/worksheet.py b/gspread/worksheet.py index d6940db3..d04d8f67 100644 --- a/gspread/worksheet.py +++ b/gspread/worksheet.py @@ -2621,7 +2621,7 @@ def batch_merge( defaults to ``MergeType.merge_row``. :type merge_type: ``MergeType`` - :returns: The body of the request repsonse. + :returns: The body of the request response. :rtype: dict """ From f5a6360a201cc635453eba6ff5f668fc72e4e244 Mon Sep 17 00:00:00 2001 From: alifeee Date: Tue, 24 Sep 2024 17:55:57 +0100 Subject: [PATCH 7/8] change type annotation for Python < 3.10 --- gspread/worksheet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gspread/worksheet.py b/gspread/worksheet.py index d04d8f67..783204e2 100644 --- a/gspread/worksheet.py +++ b/gspread/worksheet.py @@ -2608,7 +2608,7 @@ def unmerge_cells(self, name: str) -> JSONResponse: def batch_merge( self, - merges: List[Dict[Literal["range", "mergeType"], Union[str | MergeType]]], + merges: List[Dict[Literal["range", "mergeType"], Union[str, MergeType]]], merge_type: MergeType = MergeType.merge_all, ) -> Any: """Merge multiple ranges at the same time. From 474851d9c0b2c5b60b5291bfd51f017a5ec181a9 Mon Sep 17 00:00:00 2001 From: alifeee Date: Tue, 24 Sep 2024 17:58:48 +0100 Subject: [PATCH 8/8] small opinion changes --- gspread/worksheet.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/gspread/worksheet.py b/gspread/worksheet.py index 783204e2..43a9f691 100644 --- a/gspread/worksheet.py +++ b/gspread/worksheet.py @@ -2616,9 +2616,9 @@ def batch_merge( :param merges: list of dictionaries with the ranges(is A1-notation), and an optional ``MergeType`` field. See `MergeType`_ in the Sheets API reference. - :type merges: List[Dict[Literal["range", "mergeType"], Union[str | MergeType]]] + :type merges: List[Dict[Literal["range", "mergeType"], Union[str, MergeType]]] :params merge_type: (optional) default ``MergeType`` for all merges missing the merges. - defaults to ``MergeType.merge_row``. + defaults to ``MergeType.merge_all``. :type merge_type: ``MergeType`` :returns: The body of the request response. @@ -2628,11 +2628,11 @@ def batch_merge( requests = [ { "mergeCells": { - "range": a1_range_to_grid_range(i["range"], self.id), - "mergeType": i.get("mergeType", merge_type), + "range": a1_range_to_grid_range(merge["range"], self.id), + "mergeType": merge.get("mergeType", merge_type), } } - for i in merges + for merge in merges ] return self.client.batch_update(self.spreadsheet_id, {"requests": requests})