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

get_all_worksheet_values feature #1180

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
40 changes: 38 additions & 2 deletions gspread/spreadsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@
SPREADSHEET_VALUES_CLEAR_URL,
SPREADSHEET_VALUES_URL,
)
from .utils import ExportFormat, finditem, quote
from .worksheet import Worksheet
from .utils import (
ExportFormat,
column_index_to_letter,
extract_title_from_range,
fill_gaps,
finditem,
quote,
)
from .worksheet import ValueRange, Worksheet


class Spreadsheet:
Expand Down Expand Up @@ -739,3 +746,32 @@ def list_protected_ranges(self, sheetid):
raise WorksheetNotFound("worksheet id {} not found".format(sheetid))

return sheet.get("protectedRanges", [])

def get_all_worksheet_values(self, skip_worksheet_titles: list[str] = None):
"""Grabs all the data from all the worksheets in one API call. Skips any worksheets that were named in the
skip_worksheet_title param.
:returns Dict of worksheet data with worksheet title as key
"""

if skip_worksheet_titles is None:
skip_worksheet_titles = []

Comment on lines +752 to +754
Copy link
Collaborator

Choose a reason for hiding this comment

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

if you set it to [] empty list when it's not set, then just set the default value to [] in the argument in the method definition.

Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose the initial solution was right, because usage of a mutable object as default argument can follow wrong behaviour. You can see example of such problem there

ranges = []

for worksheet in self.google_sheet.worksheets():
swimninja247 marked this conversation as resolved.
Show resolved Hide resolved
if worksheet.title not in skip_worksheet_titles:
ranges.append(
f"{worksheet.title}!A1:{column_index_to_letter(worksheet.col_count)}"
)
swimninja247 marked this conversation as resolved.
Show resolved Hide resolved

values = self.google_sheet.values_batch_get(ranges=ranges)
swimninja247 marked this conversation as resolved.
Show resolved Hide resolved

return_data = {}

for values in values["valueRanges"]:
value_range = ValueRange.from_json(values)
return_data[extract_title_from_range(value_range.range)] = fill_gaps(
value_range
)

return return_data
48 changes: 48 additions & 0 deletions gspread/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
URL_KEY_V1_RE = re.compile(r"key=([^&#]+)")
URL_KEY_V2_RE = re.compile(r"/spreadsheets/d/([a-zA-Z0-9-_]+)")

TITLE_RANGE_RE = re.compile(r"'(.*?)'!.*")
Copy link
Collaborator

Choose a reason for hiding this comment

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

This won't always work, here we can have 2 cases:

  1. names with a blank space or that starts with ' so they start with a ' but they may have multiple ' surrounding the actual name
  2. no blank space, no ' at starts or end. so it's a single string with only characters.

The regex should be improved to match all possible titles.


Dimension = namedtuple("Dimension", ["rows", "cols"])("ROWS", "COLUMNS")
ValueRenderOption = namedtuple(
"ValueRenderOption", ["formatted", "unformatted", "formula"]
Expand Down Expand Up @@ -482,6 +484,31 @@ def column_letter_to_index(column):
return index


def column_index_to_letter(column: int) -> str:
"""Converts a column's numerical index to its letter

This function is case insensitive

:param str letter: An int corresponding to the column's numerical index.
Indexed from 1.
:returns: A column label in A1 notation, e.g. 'B'.
Letter case is ignored.
:rtype: int

Raises :exc:`gspread.exceptions.InvalidInputValue` in case of invalid input.

Example:

>>> a1_to_rowcol(1)
'A'
"""

if not isinstance(column, int) or column < 1:
raise InvalidInputValue(f"invalid value: {column}, must be an int >= 1")

return rowcol_to_a1(1, column).strip("1")


swimninja247 marked this conversation as resolved.
Show resolved Hide resolved
def cast_to_a1_notation(method):
"""Decorator function casts wrapped arguments to A1 notation in range
method calls.
Expand Down Expand Up @@ -519,6 +546,27 @@ def extract_id_from_url(url):
raise NoValidUrlKeyFound


def extract_title_from_range(range_string: str) -> str:
"""Will extract the sheet title from a range.

:param str letter: A range string
:returns: the title of the worksheet from the range string
:rtype: str

Raises :exc: `gspread.exceptions.InvalidInputValue`

Example:

>>> extract_title_from_range("'Volunteer Portal'!A1:Z1005" -> "Volunteer Portal")
'Volunteer Portal'
"""
match = TITLE_RANGE_RE.search(range_string)
if match:
return match.group(1)

raise InvalidInputValue


def wid_to_gid(wid):
"""Calculate gid of a worksheet from its wid."""
widval = wid[1:] if len(wid) > 3 else wid
Expand Down
44 changes: 44 additions & 0 deletions tests/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,47 @@ def test_column_letter_to_index(self):
label, expected
),
)

def test_column_index_to_letter(self):
# All the input values to test one after an other
# [0] input value
# [1] expected return value
# [2] expected exception to raise
inputs = [
("", None, gspread.exceptions.InvalidInputValue),
(1, "A", None),
(26, "Z", None),
(27, "AA", None),
(703, "AAA", None),
(256094574536617744129141650397448476, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", None),
]

for label, expected, exception in inputs:
if exception is not None:
# assert the exception is raised
with self.assertRaises(exception):
utils.column_letter_to_index(label)
else:
# assert the return values is correct
result = utils.column_index_to_letter(label)
self.assertEqual(result, expected)

def test_extract_title_from_range(self):
# All the input values to test one after an other
# [0] input value
# [1] expected return value
# [2] expected exception to raise
inputs = [
("asdf", None, gspread.exceptions.InvalidInputValue),
("'Volunteer Portal'!A1:Z1005", "Volunteer Portal", None),
]

for label, expected, exception in inputs:
if exception is not None:
# assert the exception is raised
with self.assertRaises(exception):
utils.extract_title_from_range(label)
else:
# assert the return values is correct
result = utils.extract_title_from_range(label)
self.assertEqual(result, expected)