-
Notifications
You must be signed in to change notification settings - Fork 949
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
base: master
Are you sure you want to change the base?
Changes from all commits
09e46ea
486e4e6
3428a17
09200aa
890c17d
5e0da11
4e7561e
4049286
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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 = [] | ||
|
||
ranges = [] | ||
|
||
for worksheet in self.worksheets().worksheets(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that won't work, it should be only |
||
if worksheet.title not in skip_worksheet_titles: | ||
ranges.append(worksheet.title) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we should protect the worksheet title using the util function |
||
|
||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"'(.*?)'!.*") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't always work, here we can have 2 cases:
The regex should be improved to match all possible titles. |
||
|
||
Dimension = namedtuple("Dimension", ["rows", "cols"])("ROWS", "COLUMNS") | ||
ValueRenderOption = namedtuple( | ||
"ValueRenderOption", ["formatted", "unformatted", "formula"] | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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