Skip to content

Commit

Permalink
Merge pull request #1498 from muddi900/add/batch-merge
Browse files Browse the repository at this point in the history
Created a `batch_merge` function [Issue #1473]
  • Loading branch information
lavigne958 authored Oct 7, 2024
2 parents 802cc88 + 474851d commit d606420
Show file tree
Hide file tree
Showing 3 changed files with 1,051 additions and 0 deletions.
31 changes: 31 additions & 0 deletions gspread/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2608,6 +2608,37 @@ 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.merge_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.merge_all``.
:type merge_type: ``MergeType``
:returns: The body of the request response.
:rtype: dict
"""

requests = [
{
"mergeCells": {
"range": a1_range_to_grid_range(merge["range"], self.id),
"mergeType": merge.get("mergeType", merge_type),
}
}
for merge 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.
Expand Down
Loading

0 comments on commit d606420

Please sign in to comment.