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

Feature request. Batch Merge in worksheet class #1473

Closed
MildogMiller opened this issue May 30, 2024 · 4 comments · Fixed by #1498
Closed

Feature request. Batch Merge in worksheet class #1473

MildogMiller opened this issue May 30, 2024 · 4 comments · Fixed by #1498
Milestone

Comments

@MildogMiller
Copy link

Is your feature request related to a problem? Please describe.
I am using gspread to create quite a large spreadsheet in a particular format. One of the things that is very present in the worksheet are merged cells. The batch_update, batch_format, and batch_get have been invaluable to me. However, there is no way from the worksheet side (that i can see) to do a batch merge.

I understand that logically merging cells should be done before you insert data. One thing that i find myself doing is having a long list of merge_cells at the beginning of my script, which sets up all the merges. However, each makes an API request.

Describe the solution you'd like
I would like a batch_merge method defined in the Worksheet class that takes in a list of ranges and uses the batch_update method of the Google Sheets API, resulting in only 1 API request. It would most likely need to append multiple mergeCells to a request body.

Describe alternatives you've considered
Currently one of the ways i get around it by making the request myself and then using the Speadsheet.batch_update method. However, it would be better if i could do it through some sort of abstraction and keep everything in the Worksheet class

Additional context
N/A

I am more than happy to attempt to write the method on my own and create a pull request. I understand it is a bit of a niche issue

@lavigne958
Copy link
Collaborator

Hello, thank you for your feature request, this is something we can do.

I would like to clarify a few details:

  • yes, you can insert your data, then merge your cells. You can insert data in cell A1 then merge A1:B1 so you data is merged in those cells.
  • currently you don't need to make 1 API call per merge request. You can build the request yourself and use the batch_update method to do it in a single API call. This requires some work. You can use that until we provide the new feature.
    I suppose this is what you currently do ?

In order to do batch merge request here is a sample code you can use, I believe this is what you already use, but just in case I put the example here.

file = gspread.open("myFile")
sheet = gspread.sheet1

request = {
    "requests": [
        {
            "mergeCells": {
                "range": gspread.utils.a1_range_to_grid_range("A1:B1", sheet.id),
                "mergeType": gspread.utils.MergeType.merge_rows,
            }
        },
        {
            "mergeCells": {
                "range": gspread.utils.a1_range_to_grid_range("A2:B2", sheet.id),
                "mergeType": gspread.utils.MergeType.merge_rows,
            }
        }
    ]
}

res = client.http_client.batch_update(file.id, request)

When we get a chance we'll add this feature.

for the new feature I propose we make this way:

spreadsheet.batch_merge(merges: Dict[str, MergeType])

The above type is to be adjusted after some test.

@MildogMiller
Copy link
Author

Thank you so much @lavigne958 for your speedy reply on the issue. Yes that is indeed what i currently do and it does work perfectly.

I should say the reason i requested the feature is more from an abstraction side. One of those nice to haves. It is definitely a feature that is low priority.

Thanks again!

@lavigne958 lavigne958 added this to the 6.2.0 milestone Jun 17, 2024
@muddi900
Copy link
Contributor

muddi900 commented Aug 1, 2024

When we get a chance we'll add this feature.

for the new feature I propose we make this way:

spreadsheet.batch_merge(merges: Dict[str, MergeType])

The above type is to be adjusted after some test.

@lavigne958 wouldn't it be better to have this method on the Worksheet level. Also, the type of merges params should be List[Dict].

muddi900 added a commit to muddi900/gspread that referenced this issue Aug 1, 2024
muddi900 added a commit to muddi900/gspread that referenced this issue Aug 1, 2024
@lavigne958
Copy link
Collaborator

When we get a chance we'll add this feature.
for the new feature I propose we make this way:

spreadsheet.batch_merge(merges: Dict[str, MergeType])

The above type is to be adjusted after some test.

@lavigne958 wouldn't it be better to have this method on the Worksheet level. Also, the type of merges params should be List[Dict].

yes, it would be better to have it at the worksheet level.
no it should be a Dict, each key of the dict will be a range, then each value for that range will be a merge type.

the pseudo code should like this:

requests: empty list
for each pair (key, value) in merges:
    full_range = a1_range_to_grid_range(key, sheet_id)
    append to request: {"MergeCells": {"range": full_range, "mergeType": value}}
end for loop

send batch_update(requests)

unless I miss something ?

lavigne958 added a commit that referenced this issue Oct 7, 2024
Created a `batch_merge` function [Issue #1473]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants