Skip to content

Instantly share code, notes, and snippets.

@calexandrepcjr
Created October 22, 2020 20:52
Show Gist options
  • Save calexandrepcjr/865d144093d9e55d3e921c7ca4df4f15 to your computer and use it in GitHub Desktop.
Save calexandrepcjr/865d144093d9e55d3e921c7ca4df4f15 to your computer and use it in GitHub Desktop.
Sheets module improves the productivity when dealing with spreadsheets
"""
Sheets module improves the productivity when dealing with spreadsheets
"""
def detect_complex_types(_row):
return [index for (index, row) in enumerate(_row) if type(_row) is dict]
def map_complex_types_into_string(_values):
return [str(value) for (index, value) in enumerate(_values) if index in detect_complex_types(value)]
def add_column(_sheet_values, _appended_values, _column_name="New Column"):
_sheet_values[2] += [_column_name]
for (index, row) in enumerate(_sheet_values[3:]):
try:
row += [_appended_values[index]]
except IndexError:
row += [_appended_values[len(_appended_values) - 1]]
return _sheet_values
def replace_column(_sheet_values, _appended_values, _column_number, _column_name="Replaced Column"):
if _column_number == 0:
return _sheet_values
_appended_values = map_complex_types_into_string(_appended_values)
try:
_sheet_values[2][_column_number] = _column_name
except IndexError:
return add_column(_sheet_values, _appended_values, _column_name)
for (index, row) in enumerate(_sheet_values[3:]):
try:
row[_column_number] = _appended_values[index]
except IndexError:
row[_column_number] = _appended_values[len(_appended_values) - 1]
return _sheet_values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment