Different left/right border style #132

raiis opened this Issue Oct 2, 2012 · 9 comments


None yet
3 participants

raiis commented Oct 2, 2012

thanks for great gem.
But I have one problem, is there any way, how to set different width for cell borders?

For example, I want to set left border thin and right border thick.



randym commented Oct 4, 2012

Hi @raiis

This can be done, but it is not as convenient as it could be.

If you have a look at https://github.com/randym/axlsx/blob/master/lib/axlsx/stylesheet/styles.rb#L315
you can see how borders are parsed via Styles#add_style. This needs to be refactored to support the existing implementation as well as individual border parts and their properties.

As things are now you would need to load the xfs created when when you use add_style, pull the index of the border, load up that border and find the part you want to edit and update its style / color.

something fairly involved, and really not a good way to use xlsx.

I dont suppose I could convince you to have a look at refactoring that method?

Here is an example of doing it manually that shows editing existing parts and adding new ones.

p = Axlsx::Package.new
wb = p.workbook
wb.styles do |s|
  top_bottom =  s.add_style :border => { :style => :thick, :color =>"FFFF0000", :edges => [:top, :bottom] }  
  border = s.borders[s.cellXfs[top_bottom].borderId]
  border.prs.each do |part|
    case part.name
    when :top
      part.color = Axlsx::Color.new(:rgb => "FFFF0000")
    when :bottom
      part.color = Axlsx::Color.new(:rgb => "FF00FF00")

  border.prs << Axlsx::BorderPr.new(:name => :left, :color => Axlsx::Color.new(:rgb => '0000FF'), :style => :mediumDashed)
  wb.add_worksheet(:name => 'hacked borders') do |sheet|
    sheet.add_row [1,2,3], :style=>top_bottom
p.serilaize 'crazy_border.xlsx'



randym commented Oct 15, 2012

@raiis What do you think?


raiis commented Oct 15, 2012

Hi @randym

Thanks for fast and extensive answer.
I looked last week at your solution, and it worked fine for me, but yes, that's not the clearest way how to do that.

My excel worksheet at end was so complex, that using this approach I also used to copy existing borders :)

Honestly, I don't have any current experience in contribution, but after vacation I could take a look at method refactoring.


randym commented Oct 15, 2012

@raiis Awesome to hear that things worked out in the end. I must say I am very pleased to see you fork the repo prior to your vacation! When you get back, please don't hesitate to contact me if you have any questions at all.

I think in OSS, the most important thing is to just write the code to get the job done - stack in some specs and docs and see where we go from there. It really is easy to get involved and make things better for everyone, but I must warn; It is very addictive!


randym commented Nov 16, 2012

feeling ready to tackle this? happy to help in any way i can.


raiis commented Nov 20, 2012

Yep, I finally ready to do that :)

First lets think, how will it look for end user? If I currently have:
s.add_style :border => { :style => :thin, :color =>"000000" }
then it could be, extended to
s.add_style :border => { :style => :thin, :color =>"000000" }, :border-left => { :style => :thick, :color =>"000000" }
and every border attribute (left, right, top, bottom) would just override default border attribute style?

Or you maybe have some better idea?


randym commented Nov 23, 2012

@raiis I think that is awesome and makes a lot of sense. border prs are created with whatever is in :border and the specifics like :border-left override their respective parts. My only worry is how we maintain backward compatibility with the :edges option.

assuming something like:

:border => { :style => :thin, :edges => [:right, :left]} , :border-left => { :style => :thick }

We need to decide what gets precedence.
I am leaning toward the border-left hash as it feels the most specific. What do you think?

Looking forward to your pull request!!


randym commented Dec 13, 2012

fix merged!

randym closed this Dec 13, 2012

For those who stumble upon this issue looking for the solution, check out pull request #147

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment