Skip to content

Instantly share code, notes, and snippets.

@jbennett
Created October 20, 2023 17:40
Show Gist options
  • Save jbennett/7d453ea8f2c28350231719e3e53dfb47 to your computer and use it in GitHub Desktop.
Save jbennett/7d453ea8f2c28350231719e3e53dfb47 to your computer and use it in GitHub Desktop.
Conditional Formatting w/ RubyXL
workbook = RubyXL::Workbook.new
worksheet = workbook.worksheets[0]
# Style to highlight red
style = RubyXL::DXF.new
style.font = RubyXL::Font.new
style.font.color = RubyXL::Color.new("FF0000")
# Add style to workbook
workbook.stylesheet.dxfs ||= RubyXL::DXFs.new # This was my initial problem
workbook.stylesheet.dxfs << style
style_id = workbook.stylesheet.dxfs.index(style) # need to reference this later
# Rule to highlight negative numbers
rule = RubyXL::ConditionalFormattingRule.new
rule.dxf_id = style_id
rule.type = :cellIs
rule.operator = :lessThan
rule.priority = 1
rule.formulas << RubyXL::Formula.new(expression: "0")
range = "A3:A10" # area to apply the rule
formatting = RubyXL::ConditionalFormatting.new(sgref: range)
formatting.cf_rule << rule
worksheet.conditional_formatting << formatting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment