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

Add maintain_size argument to get_value #1281

Closed
wants to merge 1 commit into from

Conversation

botcs
Copy link

@botcs botcs commented Aug 24, 2023

New feature for #947

The google API strips:

  • the trailing empty cells for each row in the returned range
  • the trailing empty rows

This new feature allows the user to maintain the size of the range by adding cells containing an empty string "".

Comment on lines +365 to +398
def _get_size_from_range(self, range_name):
"""
GSHEET API returns a list of lists but it strips empty TRAILING cells
and strips empty TRAILING rows
"""

# find out the width and height of the range from `data_range`
start_cell, end_cell = range_name.split(':')
start_col, start_row = re.findall(r'\d+|\D+', start_cell)
end_col, end_row = re.findall(r'\d+|\D+', end_cell)
# the columns can be A-Z or AA-ZZ or AAA-ZZZ
# so we need to convert the column letters to numbers
# and then calculate the number of columns
if len(start_col) == 1:
# e.g ord("A") = 65, ord("B") = 66, ord("C") = 67
num_cols = ord(end_col) - ord(start_col) + 1
else:
# In the case of multiple letters,
# we use the letters at each position as "digits"
# in a base-26 representation
start_col_int = 0
for i, col in enumerate(start_col[::-1]):
start_col_int += (ord(col) - 64) * (26 ** i)

end_col_int = 0
for i, col in enumerate(end_col[::-1]):
end_col_int += (ord(col) - 64) * (26 ** i)

num_cols = end_col_int - start_col_int + 1

# calculate the number of rows
num_rows = int(end_row) - int(start_row) + 1

return num_rows, num_cols
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have a function that does that computation 😉

see

def a1_range_to_grid_range(name, sheet_id=None):

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the function a1_range_to_grid_range work with named ranges? (see #947 (comment))

e.g., what happens when the user asks for the range "A3:B" (wanting an expanding column) or just "Sheet1". How can we know the size of the data requested?

@@ -464,7 +503,13 @@ def get_values(self, range_name=None, combine_merged_cells=False, **kwargs):
worksheet.get_values('A2:B4', value_render_option=ValueRenderOption.formula)
"""
try:
vals = fill_gaps(self.get(range_name, **kwargs))
if maintain_size:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please compare it to a the very specific value you expect so here it should be if maintain_size is True in order to prevent false positive like if maintain_size is a string or anything else that evaluates to True.

@@ -464,7 +503,13 @@ def get_values(self, range_name=None, combine_merged_cells=False, **kwargs):
worksheet.get_values('A2:B4', value_render_option=ValueRenderOption.formula)
"""
try:
vals = fill_gaps(self.get(range_name, **kwargs))
if maintain_size:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be the other way around right ? if maintain_size is True we call the get range size, then fill gaps from the computed range size and not just the values right ?

@alifeee
Copy link
Collaborator

alifeee commented Oct 1, 2023

replaced by #1305

@alifeee alifeee closed this Oct 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants