Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created a batch_merge function [Issue #1473] #1498

Merged
merged 8 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions gspread/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2606,6 +2606,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