Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import os.path
import json
from gspread.models import Cell
from gspread_dataframe import _cellrepr
def contents_of_file(filename, et_parse=True):
with open(os.path.join(os.path.dirname(__file__), filename), 'r') as f:
return json.load(f)
SHEET_CONTENTS_FORMULAS = contents_of_file('sheet_contents_formulas.json')
SHEET_CONTENTS_EVALUATED = contents_of_file('sheet_contents_evaluated.json')
CELL_LIST = [
Cell(row=i+1, col=j+1, value=value)
for i, row in enumerate(contents_of_file('cell_list.json'))
for j, value in enumerate(row)
]
CELL_LIST_STRINGIFIED = [
Cell(row=i+1, col=j+1, value=_cellrepr(value, allow_formulas=True))
for i, row in enumerate(contents_of_file('cell_list.json'))
for j, value in enumerate(row)
]
_without_index = contents_of_file('cell_list.json')
for _r in _without_index:
del _r[0]
CELL_LIST_STRINGIFIED_NO_THINGY = [
Cell(row=i+1, col=j+1, value=_cellrepr(value, allow_formulas=True))
for i, row in enumerate(_without_index)
data = self.spreadsheet.values_get(range_label)
start, end = name.split(':')
(row_offset, column_offset) = a1_to_rowcol(start)
(last_row, last_column) = a1_to_rowcol(end)
values = data.get('values', [])
rect_values = fill_gaps(
values,
rows=last_row - row_offset + 1,
cols=last_column - column_offset + 1
)
return [
Cell(row=i + row_offset, col=j + column_offset, value=value)
for i, row in enumerate(rect_values)
for j, value in enumerate(row)
]
'valueRenderOption': ('UNFORMATTED_VALUE' if evaluate_formulas else 'FORMULA'),
'dateTimeRenderOption': 'FORMATTED_STRING'
}
)
(row_offset, column_offset) = (1, 1)
(last_row, last_column) = (worksheet.row_count, worksheet.col_count)
values = data.get('values', [])
rect_values = fill_gaps(
values,
rows=last_row - row_offset + 1,
cols=last_column - column_offset + 1
)
cells = [
Cell(row=i + row_offset, col=j + column_offset, value=value)
for i, row in enumerate(rect_values)
for j, value in enumerate(row)
]
# defaultdicts fill in gaps for empty rows/cells not returned by gdocs
rows = defaultdict(lambda: defaultdict(str))
for cell in cells:
row = rows.setdefault(int(cell.row), defaultdict(str))
row[cell.col] = cell.value
if not rows:
return []
all_row_keys = chain.from_iterable(row.keys() for row in rows.values())
rect_cols = range(1, max(all_row_keys) + 1)
rect_rows = range(1, max(rows.keys()) + 1)
"""
range_label = '%s!%s' % (self.title, rowcol_to_a1(row, col))
data = self.spreadsheet.values_get(
range_label,
params={'valueRenderOption': value_render_option}
)
try:
value = data['values'][0][0]
except KeyError:
value = ''
return Cell(row, col, value)
def _finder(self, func, query):
data = self.spreadsheet.values_get(self.title)
try:
values = fill_gaps(data['values'])
except KeyError:
values = []
cells = [
Cell(row=i + 1, col=j + 1, value=value)
for i, row in enumerate(values)
for j, value in enumerate(row)
]
if isinstance(query, basestring):
match = lambda x: x.value == query
else:
match = lambda x: query.search(x.value)
return func(match, cells)
index_value = [ index_value ]
value_row = list(index_value) + list(value_row)
values.append(value_row)
for y_idx, value_row in enumerate(values):
for x_idx, cell_value in enumerate(value_row):
updates.append(
(y_idx+row,
x_idx+col,
_cellrepr(cell_value, allow_formulas))
)
if not updates:
logger.debug("No updates to perform on worksheet.")
return
cells_to_update = [ Cell(row, col, value) for row, col, value in updates ]
logger.debug("%d cell updates to send", len(cells_to_update))
resp = worksheet.update_cells(cells_to_update, value_input_option='USER_ENTERED')
logger.debug("Cell update response: %s", resp)