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 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
34 changes: 32 additions & 2 deletions gspread/spreadsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
SPREADSHEET_VALUES_CLEAR_URL,
SPREADSHEET_VALUES_URL,
)
from .utils import ExportFormat, finditem, quote
from .worksheet import Worksheet
from .utils import ExportFormat, extract_title_from_range, fill_gaps, finditem, quote
from .worksheet import ValueRange, Worksheet


class Spreadsheet:
Expand Down Expand Up @@ -739,3 +739,33 @@ 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 = 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.

:param list skip_worksheet_titles: A list of worksheet titles to skip.
:returns: a dict of worksheet data with worksheet title as key
:rtype: dict
"""

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.worksheets().worksheets():
Copy link
Collaborator

Choose a reason for hiding this comment

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

that won't work, it should be only self.worksheets()

if worksheet.title not in skip_worksheet_titles:
ranges.append(worksheet.title)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here we should protect the worksheet title using the util function absolute_range_name


values = self.values_batch_get(ranges=ranges)

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
24 changes: 24 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 @@ -519,6 +521,28 @@ 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:
:class:`~gspread.exceptions.InvalidInputValue`: if can't extract a title

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
20 changes: 20 additions & 0 deletions tests/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,23 @@ def test_column_letter_to_index(self):
label, 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)