From 81b9febc760aea48b0fa28636b6d64806f8ab51d Mon Sep 17 00:00:00 2001 From: Madhur Jain <92226302+immortalcodes@users.noreply.github.com> Date: Sun, 8 Oct 2023 12:39:49 +0530 Subject: [PATCH 1/2] Update google_spreadsheets.py --- redash/query_runner/google_spreadsheets.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/redash/query_runner/google_spreadsheets.py b/redash/query_runner/google_spreadsheets.py index 279a329dc1..1d65d473c4 100644 --- a/redash/query_runner/google_spreadsheets.py +++ b/redash/query_runner/google_spreadsheets.py @@ -100,18 +100,28 @@ def __init__(self, worksheet_title): def parse_query(query): values = query.split("|") key = values[0] # key of the spreadsheet - worksheet_num_or_title = 0 # A default value for when a number of inputs is invalid + worksheet_num = 0 # A default value for when a number of inputs is invalid + worksheet_title = '' # No title initially if len(values) == 2: s = values[1].strip() if len(s) > 0: if re.match(r"^\"(.*?)\"$", s): # A string quoted by " means a title of worksheet - worksheet_num_or_title = s[1:-1] - else: + worksheet_title = s[1:-1] + elif re.match(r"^\d+$", s): # if spreadsheet contains more than one worksheet - this is the number of it - worksheet_num_or_title = int(s) - - return key, worksheet_num_or_title + worksheet_num = int(s) + else: + # To match for non-quoted one-word file name + if len(s.split()) == 1 : + worksheet_title = s + if worksheet_title != '': + return key, worksheet_title + else: + return key, worksheet_num + + else: + return key, worksheet_num def parse_worksheet(worksheet): From 64f73ffd6df35f3652549c8c1d16527045b88809 Mon Sep 17 00:00:00 2001 From: Madhur Jain <92226302+immortalcodes@users.noreply.github.com> Date: Mon, 16 Oct 2023 09:53:37 +0530 Subject: [PATCH 2/2] Update google_spreadsheets.py --- redash/query_runner/google_spreadsheets.py | 42 +++++++++------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/redash/query_runner/google_spreadsheets.py b/redash/query_runner/google_spreadsheets.py index 1d65d473c4..e074b36a0b 100644 --- a/redash/query_runner/google_spreadsheets.py +++ b/redash/query_runner/google_spreadsheets.py @@ -100,28 +100,20 @@ def __init__(self, worksheet_title): def parse_query(query): values = query.split("|") key = values[0] # key of the spreadsheet - worksheet_num = 0 # A default value for when a number of inputs is invalid - worksheet_title = '' # No title initially + worksheet_pointer = ' 1' # A default value for when a number of inputs is invalid if len(values) == 2: s = values[1].strip() if len(s) > 0: if re.match(r"^\"(.*?)\"$", s): # A string quoted by " means a title of worksheet - worksheet_title = s[1:-1] - elif re.match(r"^\d+$", s): - # if spreadsheet contains more than one worksheet - this is the number of it - worksheet_num = int(s) + worksheet_pointer = s[1:-1] else: - # To match for non-quoted one-word file name - if len(s.split()) == 1 : - worksheet_title = s - if worksheet_title != '': - return key, worksheet_title - else: - return key, worksheet_num - - else: - return key, worksheet_num + if re.match(r"^\d+$", s.split()[0]): + # index is initialized with a space to differentiate from the title of worksheet + worksheet_pointer = ' '+s.split()[0] + else: + worksheet_pointer = s.split()[0] + return key, worksheet_pointer def parse_worksheet(worksheet): @@ -141,17 +133,17 @@ def parse_worksheet(worksheet): return data -def parse_spreadsheet(spreadsheet, worksheet_num_or_title): +def parse_spreadsheet(spreadsheet, worksheet_pointer): worksheet = None - if isinstance(worksheet_num_or_title, int): - worksheet = spreadsheet.get_worksheet_by_index(worksheet_num_or_title) + if worksheet_pointer[0] == ' ': # An index of worksheet + worksheet = spreadsheet.get_worksheet_by_index(int(worksheet_pointer)) if worksheet is None: worksheet_count = len(spreadsheet.worksheets()) - raise WorksheetNotFoundError(worksheet_num_or_title, worksheet_count) - elif isinstance(worksheet_num_or_title, str): - worksheet = spreadsheet.get_worksheet_by_title(worksheet_num_or_title) + raise WorksheetNotFoundError(int(worksheet_pointer), worksheet_count) + elif worksheet_pointer[0] == ' ': # A title of worksheet + worksheet = spreadsheet.get_worksheet_by_title(worksheet_pointer) if worksheet is None: - raise WorksheetNotFoundByTitleError(worksheet_num_or_title) + raise WorksheetNotFoundByTitleError(worksheet_pointer) worksheet_values = worksheet.get_all_values() @@ -250,7 +242,7 @@ def test_connection(self): def run_query(self, query, user): logger.debug("Spreadsheet is about to execute query: %s", query) - key, worksheet_num_or_title = parse_query(query) + key, worksheet_pointer = parse_query(query) try: spreadsheet_service = self._get_spreadsheet_service() @@ -260,7 +252,7 @@ def run_query(self, query, user): else: spreadsheet = spreadsheet_service.open_by_key(key) - data = parse_spreadsheet(SpreadsheetWrapper(spreadsheet), worksheet_num_or_title) + data = parse_spreadsheet(SpreadsheetWrapper(spreadsheet), worksheet_pointer) return json_dumps(data), None except gspread.SpreadsheetNotFound: