Skip to content

Commit

Permalink
feat: Add formulas integration
Browse files Browse the repository at this point in the history
We can now get formulas with method Range.get_formulas.
To set formulas, we can simply set_values with formulas as string. If a string starts with '=' it will be interpreted as a formula.
The method Range.set_formulas now raise an error when used.
  • Loading branch information
philippe2803 committed Nov 26, 2021
1 parent 97ecb8a commit 590df68
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
7 changes: 5 additions & 2 deletions sheetfu/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,11 @@ def set_formulas(self, formulas, batch_to=None):
Set formulas for the Range.
:param formulas: 2D array of formulas (size must match range coordinates).
"""
# todo: set formula.
return formulas
raise NotImplementedError(
"Setting formulas via Range.set_formulas is not implemented yet.\n"
"You can however set formulas string (starting with '=') with the "
"method Range.set_values and they will be interpreted as formulas"
)

def persist_a1_data_range(self, a1):
"""
Expand Down
36 changes: 26 additions & 10 deletions sheetfu/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@


class CellParsers:
"""
Class to register every setters and getters from the Google Sheets API.
Following documentation is essential for understanding what we're doing here.
https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/cells
"""
@staticmethod
def get_value(cell):
value_body = cell["effectiveValue"]
Expand All @@ -13,7 +19,12 @@ def get_value(cell):
.get("numberFormat", {})
.get("type")
)
for value_type in ['stringValue', 'numberValue', 'boolValue', 'formulaValue']:
for value_type in [
'stringValue',
'numberValue',
'boolValue',
'formulaValue'
]:
if value_body.get(value_type) is not None:
if format_type in ["DATE", "DATE_TIME"]:
return serial_number_to_datetime(value_body[value_type])
Expand All @@ -23,6 +34,8 @@ def get_value(cell):
def set_value(cell):
if cell is not None:
if isinstance(cell, str):
if len(cell) > 0 and cell[0] == "=":
return {"userEnteredValue": {"formulaValue": cell}}
return {"userEnteredValue": {"stringValue": cell}}
elif isinstance(cell, bool):
return {"userEnteredValue": {"boolValue": cell}}
Expand Down Expand Up @@ -92,21 +105,24 @@ def set_font_color(cell):
font_color = hex_to_rgb(cell)
else:
font_color = hex_to_rgb('#000000')
return {"userEnteredFormat": {'textFormat': {'foregroundColor': font_color}}}
return {
"userEnteredFormat": {
"textFormat": {
"foregroundColor": font_color
}
}
}

@staticmethod
def get_formula(cell):
formula = cell["formula"]
formula = cell["userEnteredValue"]["formulaValue"]
if not formula:
return ''
return formula

@staticmethod
def set_formula(cell):
if cell:
formula = cell
else:
formula = ''
return {'formula': formula}


if not cell:
cell = ""
formula = cell
return {'userEnteredValue': {"formulaValue": formula}}

0 comments on commit 590df68

Please sign in to comment.