axlsx icon indicating copy to clipboard operation
axlsx copied to clipboard

Apply multiple styles

Open kleine2 opened this issue 14 years ago • 20 comments

A way to apply multiple styles to the same cell. To encourage style reuse and not duplication when there are several orthangonal styles which need to be applied. My example was I wanted a yellow background to highlight rows which were updated since the last report. This is in addition to any other styles that are applied to all the rows.

Also would be ideal if the styling could be in a separate file (maybe a view)? Kind of like css.

kleine2 avatar Feb 15 '12 05:02 kleine2

+1

nodrog avatar Aug 06 '12 22:08 nodrog

+1

dre-hh avatar Jan 07 '13 15:01 dre-hh

+1

exsemt avatar Jan 07 '13 15:01 exsemt

would be very cool, we have to create n styles to mix up different requirements like bg-color, format etc. very nasty

dinge avatar Jan 07 '13 15:01 dinge

+1

agileapplications avatar Mar 08 '13 10:03 agileapplications

+1

aboltart avatar May 23 '13 18:05 aboltart

CFP: what kind of syntax are we looking for? Send in examples of how YOU want to define styles.

randym avatar May 23 '13 18:05 randym

Probably following syntax sheet.rows.last.cells[0].style = [style1, style2]

aboltart avatar May 27 '13 06:05 aboltart

+1

lovio avatar Jul 19 '13 04:07 lovio

+1

mekhovov avatar Aug 20 '13 19:08 mekhovov

+1

ottodude125 avatar Sep 27 '13 17:09 ottodude125

+1

ghost avatar Jan 06 '14 17:01 ghost

+1

ifroz avatar Mar 18 '14 11:03 ifroz

I digged a little into it and it seems that the Office Open XML Format does not support applying multiple styles on a cell, so this job would be up to ruby

3.3.1.3 c(Cell) s (Style Index) The index of this cell's style. Style records are stored in the Styles Part. The possible values for this attribute are defined by the XML Schema unsignedInt datatype.

<c s=[style_index] ...>
.....
</c>

So the proposed Syntax above would be very hard to implement

 sheet.rows.last.cells[0].style = [style1, style2]

And it also doesn't fit into the add_row api

#apply mutliple cell styles on a row
sheet.add_row([value_1, value_2, value_3], [style_id_1, style_id_2, style_id_3])
#apply one style on a row
sheet.add_row([value_1, value_2, value_3], style_id)

It already differentiates between an Arry and Integer Parameter.

I would suggest the following:

We should introduce style merging/combination

@bold                   = @workbook.styles.add_style  :b => true
@money               = @workbook.styles.add_style  :format_code => '#,###,##0.00'

@bold_money      = @workbook.styles.add_styles [@bold, @money]
#or in case of dxfs styles
@workbook.styles.add_styles [@bold, @money], :dxfs

This could be more or less easily implemented by making all the XML Node style Objects mergable

def add_styles(indexes)
 indexes.map{ |i| cellXfs[i] }.reduce{ |combined, s| combined + s }
end

@randym Gues i could provide a pull request by next month, if you agree to the general strategy

dre-hh avatar May 15 '14 07:05 dre-hh

@randym @dre-hh
There any preview for that feature? Add multiple styles in that way would turn everything much easier :+1:

lucasldemello avatar Oct 29 '14 10:10 lucasldemello

@dre-hh @randym Is this feature available now?

anandghegde avatar Jan 25 '15 06:01 anandghegde

Nope. The above solution was just a suggestion from me. Unfortionately nobody provided feedback.

Actuallly i had another couple of thoughts on my solution and the benefit of it is marginal as styles are configured by the set of hashed options. You can keep the options as separate variables and merge them by yourself.

@bold_money_options = @bold_options.merge @money_options
@workbook.styles.add_styles @bold_money_options

The real benefit would be the Open XML Format of Excel definiting multiple Style references.

dre-hh avatar Jan 28 '15 21:01 dre-hh

+1

westonganger avatar Jul 09 '16 20:07 westonganger

+1

dsimel avatar Jul 30 '16 17:07 dsimel

I am in the process of adding this feature to the axlsx_styler gem.

If you want a solution without adding another library you could add the following monkey patch which allows a second argument for the add_styles method where you can pass in the cells style index.

module Axlsx
  class Styles
    # Patches the original Axlsx::Styles#add_style_method so that we can combine 
    # the existing cell style with the new one by adding the optional 'style_index' argument
    def add_style(options={}, style_index=nil)
      options[:type] ||= :xf # Default to :xf
      raise ArgumentError, "Type must be one of [:xf, :dxf]" unless [:xf, :dxf].include?(options[:type])

      fill = parse_fill_options(options)
      font = parse_font_options(options)
      numFmt = parse_num_fmt_options(options)
      border = parse_border_options(options)
      alignment = parse_alignment_options(options)
      protection = parse_protection_options(options)

      if options[:type] == :dxf
        style = Dxf.new(fill: fill, font: font, numFmt: numFmt, border: border, alignment: alignment, protection: protection)
      else
        unless style_index.nil?
          style_object = cellXfs[style_index]

          unless font.nil?
            font = parse_font_options(options, style_object.fontId)
          end

          fill ||= style_object.fillId
          numFmt ||= style_object.numFmtId
          border ||= style_object.borderId
          alignment ||= style_object.alignment
          protection ||= style_object.protection
        end

        style = Xf.new(fillId: (fill || 0), fontId: (font || 0), numFmtId: (numFmt || 0), borderId: (border || 0), alignment: alignment, protection: protection, applyFill: !fill.nil?, applyFont: !font.nil?, applyNumberFormat: !numFmt.nil?, applyBorder: !border.nil?, applyAlignment: !alignment.nil?, applyProtection: !protection.nil?)
      end

      if options[:type] == :xf
        cellXfs << style
      else
        dxfs << style
      end
    end

    # allow to pass in fontId instead of always defaulting first, also apply name and color before merging options
    def parse_font_options(options={}, fontId=0)
      return if (options.keys & [:fg_color, :sz, :b, :i, :u, :strike, :outline, :shadow, :charset, :family, :font_name]).empty?

      options[:name] = options[:font_name] unless options[:font_name].nil?
      options[:color] = Color.new(rgb: options[:fg_color]) unless options[:fg_color].nil?

      options = fonts[fontId].instance_values.merge(options)
      font = Font.new(options)

      if options[:type] == :dxf
        font
      else
        fonts << font
      end
    end
  end
end

You can then use this as follows for example on the first cell of the worksheet.

merged_style = workbook.styles.add_style({b: true}, worksheet.cells[0].style)

worksheet.cells[0].style = merged_style

westonganger avatar Jul 30 '16 17:07 westonganger