Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
worksheet.conditional_formatting.adjustPriority()
for range_string, rules in iteritems(worksheet.conditional_formatting.cf_rules):
if not len(rules):
# Skip if there are no rules. This is possible if a dataBar rule was read in and ignored.
continue
start_tag(doc, 'conditionalFormatting', {'sqref': range_string})
for rule in rules:
if rule['type'] == 'dataBar':
# Ignore - uses extLst tag which is currently unsupported.
continue
attr = {'type': rule['type']}
for rule_attr in ConditionalFormatting.rule_attributes:
if rule_attr in rule:
attr[rule_attr] = str(rule[rule_attr])
start_tag(doc, 'cfRule', attr)
if 'formula' in rule:
for f in rule['formula']:
tag(doc, 'formula', None, f)
if 'colorScale' in rule:
start_tag(doc, 'colorScale')
for cfvo in rule['colorScale']['cfvo']:
tag(doc, 'cfvo', cfvo)
for color in rule['colorScale']['color']:
if str(color.index).split(':')[0] == 'theme': # strip prefix theme if marked as such
if str(color.index).split(':')[2]:
tag(doc, 'color', {'theme': str(color.index).split(':')[1],
'tint': str(color.index).split(':')[2]})
else:
tag(doc, 'color', {'theme': str(color.index).split(':')[1]})
else:
tag(doc, 'color', {'rgb': str(color.index)})
def write_worksheet_conditional_formatting(doc, worksheet):
"""Write conditional formatting to xml."""
for range_string, rules in iteritems(worksheet.conditional_formatting.cf_rules):
if not len(rules):
# Skip if there are no rules. This is possible if a dataBar rule was read in and ignored.
continue
start_tag(doc, 'conditionalFormatting', {'sqref': range_string})
for rule in rules:
if rule['type'] == 'dataBar':
# Ignore - uses extLst tag which is currently unsupported.
continue
attr = {'type': rule['type']}
for rule_attr in ConditionalFormatting.rule_attributes:
if rule_attr in rule:
attr[rule_attr] = str(rule[rule_attr])
start_tag(doc, 'cfRule', attr)
if 'formula' in rule:
for f in rule['formula']:
tag(doc, 'formula', None, f)
if 'colorScale' in rule:
start_tag(doc, 'colorScale')
for cfvo in rule['colorScale']['cfvo']:
tag(doc, 'cfvo', cfvo)
for color in rule['colorScale']['color']:
if str(color.index).split(':')[0] == 'theme': # strip prefix theme if marked as such
if str(color.index).split(':')[2]:
tag(doc, 'color', {'theme': str(color.index).split(':')[1],
'tint': str(color.index).split(':')[2]})
else:
tag(doc, 'color', {'theme': str(color.index).split(':')[1]})
else:
tag(doc, 'color', {'rgb': str(color.index)})
continue
start_tag(doc, 'conditionalFormatting', {'sqref': range_string})
for rule in rules:
if rule['type'] == 'dataBar':
# Ignore - uses extLst tag which is currently unsupported.
continue
attr = {'type': rule['type']}
for rule_attr in ConditionalFormatting.rule_attributes:
if rule_attr in rule:
attr[rule_attr] = str(rule[rule_attr])
start_tag(doc, 'cfRule', attr)
if 'formula' in rule:
for f in rule['formula']:
tag(doc, 'formula', None, f)
if 'colorScale' in rule:
start_tag(doc, 'colorScale')
for cfvo in rule['colorScale']['cfvo']:
tag(doc, 'cfvo', cfvo)
for color in rule['colorScale']['color']:
if str(color.index).split(':')[0] == 'theme': # strip prefix theme if marked as such
if str(color.index).split(':')[2]:
tag(doc, 'color', {'theme': str(color.index).split(':')[1],
'tint': str(color.index).split(':')[2]})
else:
tag(doc, 'color', {'theme': str(color.index).split(':')[1]})
else:
tag(doc, 'color', {'rgb': str(color.index)})
end_tag(doc, 'colorScale')
if 'iconSet' in rule:
iconAttr = {}
for icon_attr in ConditionalFormatting.icon_attributes:
if icon_attr in rule['iconSet']:
def write_worksheet_datavalidations(doc, worksheet):
""" Write data validation(s) to xml."""
# Filter out "empty" data-validation objects (i.e. with 0 cells)
required_dvs = [x for x in worksheet._data_validations
if len(x.cells) or len(x.ranges)]
count = len(required_dvs)
if count == 0:
return
start_tag(doc, 'dataValidations', {'count': str(count)})
for data_validation in required_dvs:
start_tag(doc, 'dataValidation', data_validation.generate_attributes_map())
if data_validation.formula1:
tag(doc, 'formula1', body=data_validation.formula1)
if data_validation.formula2:
tag(doc, 'formula2', body=data_validation.formula2)
end_tag(doc, 'dataValidation')
end_tag(doc, 'dataValidations')
def write_worksheet_hyperlinks(doc, worksheet):
"""Write worksheet hyperlinks to xml."""
write_hyperlinks = False
for cell in worksheet.get_cell_collection():
if cell.hyperlink_rel_id is not None:
write_hyperlinks = True
break
if write_hyperlinks:
start_tag(doc, 'hyperlinks')
for cell in worksheet.get_cell_collection():
if cell.hyperlink_rel_id is not None:
attrs = {'display': cell.hyperlink,
'ref': cell.coordinate,
'r:id': cell.hyperlink_rel_id}
tag(doc, 'hyperlink', attrs)
end_tag(doc, 'hyperlinks')
def write_worksheet_conditional_formatting(doc, worksheet):
"""Write conditional formatting to xml."""
worksheet.conditional_formatting.adjustPriority()
for range_string, rules in iteritems(worksheet.conditional_formatting.cf_rules):
if not len(rules):
# Skip if there are no rules. This is possible if a dataBar rule was read in and ignored.
continue
start_tag(doc, 'conditionalFormatting', {'sqref': range_string})
for rule in rules:
if rule['type'] == 'dataBar':
# Ignore - uses extLst tag which is currently unsupported.
continue
attr = {'type': rule['type']}
for rule_attr in ConditionalFormatting.rule_attributes:
if rule_attr in rule:
attr[rule_attr] = str(rule[rule_attr])
start_tag(doc, 'cfRule', attr)
if 'formula' in rule:
for f in rule['formula']:
tag(doc, 'formula', None, f)
if 'colorScale' in rule:
start_tag(doc, 'colorScale')
for cfvo in rule['colorScale']['cfvo']:
tag(doc, 'cfvo', cfvo)
def write_worksheet_autofilter(doc, worksheet):
auto_filter = worksheet.auto_filter
if auto_filter.filter_columns or auto_filter.sort_conditions:
start_tag(doc, 'autoFilter', {'ref': auto_filter.ref})
for col_id, filter_column in sorted(auto_filter.filter_columns.items()):
start_tag(doc, 'filterColumn', {'colId': str(col_id)})
if filter_column.blank:
start_tag(doc, 'filters', {'blank': '1'})
else:
start_tag(doc, 'filters')
for val in filter_column.vals:
tag(doc, 'filter', {'val': val})
end_tag(doc, 'filters')
end_tag(doc, 'filterColumn')
if auto_filter.sort_conditions:
start_tag(doc, 'sortState', {'ref': auto_filter.ref})
for sort_condition in auto_filter.sort_conditions:
sort_attr = {'ref': sort_condition.ref}
if sort_condition.descending:
sort_attr['descending'] = '1'
def write_worksheet_autofilter(doc, worksheet):
auto_filter = worksheet.auto_filter
if auto_filter.filter_columns or auto_filter.sort_conditions:
start_tag(doc, 'autoFilter', {'ref': auto_filter.ref})
for col_id, filter_column in sorted(auto_filter.filter_columns.items()):
start_tag(doc, 'filterColumn', {'colId': str(col_id)})
if filter_column.blank:
start_tag(doc, 'filters', {'blank': '1'})
else:
start_tag(doc, 'filters')
for val in filter_column.vals:
tag(doc, 'filter', {'val': val})
end_tag(doc, 'filters')
end_tag(doc, 'filterColumn')
if auto_filter.sort_conditions:
start_tag(doc, 'sortState', {'ref': auto_filter.ref})
for sort_condition in auto_filter.sort_conditions:
sort_attr = {'ref': sort_condition.ref}
if sort_condition.descending:
sort_attr['descending'] = '1'
tag(doc, 'sortCondtion', sort_attr)
end_tag(doc, 'sortState')
end_tag(doc, 'autoFilter')
elif auto_filter.ref:
tag(doc, 'autoFilter', {'ref': auto_filter.ref})
def write_worksheet_mergecells(doc, worksheet):
"""Write merged cells to xml."""
if len(worksheet._merged_cells) > 0:
start_tag(doc, 'mergeCells', {'count': str(len(worksheet._merged_cells))})
for range_string in worksheet._merged_cells:
attrs = {'ref': range_string}
tag(doc, 'mergeCell', attrs)
end_tag(doc, 'mergeCells')
def write_worksheet_sheetviews(doc, worksheet):
start_tag(doc, 'sheetViews')
start_tag(doc, 'sheetView', {'workbookViewId': '0'})
selectionAttrs = {}
topLeftCell = worksheet.freeze_panes
if topLeftCell:
colName, row = coordinate_from_string(topLeftCell)
column = column_index_from_string(colName)
pane = 'topRight'
paneAttrs = {}
if column > 1:
paneAttrs['xSplit'] = str(column - 1)
if row > 1:
paneAttrs['ySplit'] = str(row - 1)
pane = 'bottomLeft'
if column > 1:
pane = 'bottomRight'
paneAttrs.update(dict(topLeftCell=topLeftCell,