-
Notifications
You must be signed in to change notification settings - Fork 53
Code Examples
chdastolfo edited this page Sep 25, 2021
·
2 revisions
This is the home of code examples that might be useful to you, but are too long to include in the README.
Because Monday column values can be formatted in variable ways, here's one way you can loop through all of the items on a board and format them to your liking and according to their column type. Thanks to aidanlister for the example!
def get_col_defs(client, board_id):
data = client.boards.fetch_boards_by_id([board_id])
columns = data['data']['boards'][0]['columns']
col_defs = {}
for column in columns:
col_defs[column['id']] = column
return col_defs
def get_formatted_value(col_defs, field_name, value):
""" Take a cell and make it human readable. """
def format_default(col_defs, field_name, value):
return value
def format_text_field(col_defs, field_name, value):
return json.loads(value)
def format_date_field(col_defs, field_name, value):
if value is None:
return ''
return json.loads(value)['date']
def format_numeric_field(col_defs, field_name, value):
if value is None:
return ''
return json.loads(value)
def format_longtext_field(col_defs, field_name, value):
if value is None:
return ''
value = json.loads(value)['text']
return value.strip() if value else ''
def format_color_field(col_defs, field_name, value):
if value is None:
return ''
labels = json.loads(col_defs[field_name]['settings_str'])['labels']
return labels.get(str(json.loads(value)['index']))
def format_dropdown_field(col_defs, field_name, value):
if value is None:
return ''
labels = json.loads(col_defs[field_name]['settings_str'])['labels']
label_map = dict([(row['id'], row['name']) for row in labels])
return ", ".join([label_map.get(id) for id in json.loads(value)['ids']])
type_to_callable_map = {
'color': format_color_field,
'dropdown': format_dropdown_field,
'long-text': format_longtext_field,
'date': format_date_field,
'numeric': format_numeric_field,
'text': format_text_field,
}
t = col_defs[field_name]['type']
formatter = type_to_callable_map.get(t, format_default)
return formatter(col_defs, field_name, value)
monday_api_key = 'xxx'
board_id = xxx
# Connect to Monday
conn = MondayClient(monday_api_key)
# Grab a map of column IDs and their settings
col_defs = monday.utils.get_col_defs(conn, board_id)
# Grab all the rows (items) from a board
data = conn.boards.fetch_items_by_board_id([board_id])
# Loop through each cell and format it according to the cell settings
rows = []
for item in data['data']['boards'][0]['items']:
row = {}
for col in item['column_values']:
row.append(get_formatted_value(col_defs, col['id'], col['value']))
rows.append(row)
# Use e.g. tablib to format for console.
ds = tablib.Dataset()
ds.dict = rows
print(ds.tsv)