Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions lib/rubyXL/convenience_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ def change_row_border(row, direction, weight)
sheet_data.rows[row].style_index = @workbook.modify_border(get_row_style(row), direction, weight)

sheet_data[row].cells.each { |c|
c.change_border(direction, weight) unless c.nil?
c.change_border(direction, weight, :direct_match) unless c.nil?
}
end

Expand All @@ -589,7 +589,7 @@ def change_row_border_color(row, direction, color = '000000')
sheet_data.rows[row].style_index = @workbook.modify_border_color(get_row_style(row), direction, color)

sheet_data[row].cells.each { |c|
c.change_border_color(direction, color) unless c.nil?
c.change_border_color(direction, color, :direct_match) unless c.nil?
}
end

Expand Down Expand Up @@ -753,10 +753,9 @@ def change_column_border(column_index, direction, weight)
ensure_cell_exists(0, column_index)

cols.get_range(column_index).style_index = @workbook.modify_border(get_col_style(column_index), direction, weight)

sheet_data.rows.each { |row|
c = row.cells[column_index]
c.change_border(direction, weight) unless c.nil?
c.change_border(direction, weight, :direct_match) unless c.nil?
}
end

Expand All @@ -769,7 +768,7 @@ def change_column_border_color(column_index, direction, color)

sheet_data.rows.each { |row|
c = row.cells[column_index]
c.change_border_color(direction, color) unless c.nil?
c.change_border_color(direction, color, :direct_match) unless c.nil?
}
end

Expand Down Expand Up @@ -856,15 +855,33 @@ def change_text_wrap(wrap = false)
self.style_index = workbook.modify_alignment(self.style_index) { |a| a.wrap_text = wrap }
end

def change_border(direction, weight)
def change_border(direction, weight, direct_match = false)
validate_worksheet
self.style_index = workbook.modify_border(self.style_index, direction, weight)
merged_cell = !direct_match && worksheet.merged_cells && worksheet.merged_cells.detect { |mc| mc.cover?(row, column) }
if merged_cell
merged_cell.ref.row_range.each do |r|
merged_cell.ref.col_range.each do |c|
worksheet.add_cell(r, c, '', nil, false).change_border(direction, weight, :direct_match)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is that if merged cell is, like, 3 cells wide and 3 cells high, then the central cell (which technically does not have any visible borders) will get a border as well...

Also, what will happen if a cell already exists at those coordinates?

end
end
else
self.style_index = workbook.modify_border(self.style_index, direction, weight)
end
end

def change_border_color(direction, color)
def change_border_color(direction, color, direct_match = false)
validate_worksheet
Color.validate_color(color)
self.style_index = workbook.modify_border_color(self.style_index, direction, color)
merged_cell = !direct_match && worksheet.merged_cells && worksheet.merged_cells.detect { |mc| mc.cover?(row, column) }
if merged_cell
merged_cell.ref.row_range.each do |r|
merged_cell.ref.col_range.each do |c|
worksheet.add_cell(r, c, '', nil, false).change_border_color(direction, color, :direct_match)
end
end
else
self.style_index = workbook.modify_border_color(self.style_index, direction, color)
end
end

def is_italicized()
Expand Down
3 changes: 3 additions & 0 deletions lib/rubyXL/objects/worksheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ class TableParts < OOXMLContainerObject
class MergedCell < OOXMLObject
define_attribute(:ref, :ref)
define_element_name 'mergeCell'
def cover?(r,c)
ref.row_range.cover?(r) && ref.col_range.cover?(c)
end
end

# http://www.datypic.com/sc/ooxml/e-ssml_mergeCells-1.html
Expand Down
196 changes: 196 additions & 0 deletions spec/lib/cell_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,104 @@
expect(@cell.get_border_color(:top)).to eq('FF0000')
expect(@cell.get_border(:top)).to eq('thin')
end

context "when the cell is merged horizontally" do
before :each do
@worksheet.merge_cells(0, 0, 0, 1)
end

it "should change the left border color of both merged cells" do
@cell.change_border_color(:left, "FF0000")
expect(@worksheet[0][0].get_border_color(:left)).to eq("FF0000")
expect(@worksheet[0][1].get_border_color(:left)).to eq("FF0000")
end

it "should change the right border color of the both merged cells" do
@cell.change_border_color(:right, "FF0000")
expect(@worksheet[0][0].get_border_color(:right)).to eq("FF0000")
expect(@worksheet[0][1].get_border_color(:right)).to eq("FF0000")
end

it "should change the top border color for both merged cells" do
@cell.change_border_color(:top, "FF0000")
expect(@worksheet[0][0].get_border_color(:top)).to eq("FF0000")
expect(@worksheet[0][1].get_border_color(:top)).to eq("FF0000")
end

it "should change the bottom border color for both merged cells" do
@cell.change_border_color(:bottom, "FF0000")
expect(@worksheet[0][0].get_border_color(:bottom)).to eq("FF0000")
expect(@worksheet[0][1].get_border_color(:bottom)).to eq("FF0000")
end
end

context "when the cell is merged vertically" do
before :each do
@worksheet.merge_cells(0, 0, 1, 0)
end

it "should change the left border color of both merged cells" do
@cell.change_border_color(:left, "FF0000")
expect(@worksheet[0][0].get_border_color(:left)).to eq("FF0000")
expect(@worksheet[1][0].get_border_color(:left)).to eq("FF0000")
end

it "should change the right border color of the both merged cells" do
@cell.change_border_color(:right, "FF0000")
expect(@worksheet[0][0].get_border_color(:right)).to eq("FF0000")
expect(@worksheet[1][0].get_border_color(:right)).to eq("FF0000")
end

it "should change the top border color for both merged cells" do
@cell.change_border_color(:top, "FF0000")
expect(@worksheet[0][0].get_border_color(:top)).to eq("FF0000")
expect(@worksheet[1][0].get_border_color(:top)).to eq("FF0000")
end

it "should change the bottom border color for both merged cells" do
@cell.change_border_color(:bottom, "FF0000")
expect(@worksheet[0][0].get_border_color(:bottom)).to eq("FF0000")
expect(@worksheet[1][0].get_border_color(:bottom)).to eq("FF0000")
end
end

context "when the cell is merged horizontally and veritically" do
before :each do
@worksheet.merge_cells(0, 0, 1, 1)
end

it "should change the left border color of both merged cells" do
@cell.change_border_color(:left, "FF0000")
expect(@worksheet[0][0].get_border_color(:left)).to eq("FF0000")
expect(@worksheet[0][1].get_border_color(:left)).to eq("FF0000")
expect(@worksheet[1][0].get_border_color(:left)).to eq("FF0000")
expect(@worksheet[1][1].get_border_color(:left)).to eq("FF0000")
end

it "should change the right border color of the both merged cells" do
@cell.change_border_color(:right, "FF0000")
expect(@worksheet[0][0].get_border_color(:right)).to eq("FF0000")
expect(@worksheet[0][1].get_border_color(:right)).to eq("FF0000")
expect(@worksheet[1][0].get_border_color(:right)).to eq("FF0000")
expect(@worksheet[1][1].get_border_color(:right)).to eq("FF0000")
end

it "should change the top border color for both merged cells" do
@cell.change_border_color(:top, "FF0000")
expect(@worksheet[0][0].get_border_color(:top)).to eq("FF0000")
expect(@worksheet[0][1].get_border_color(:top)).to eq("FF0000")
expect(@worksheet[1][0].get_border_color(:top)).to eq("FF0000")
expect(@worksheet[1][1].get_border_color(:top)).to eq("FF0000")
end

it "should change the bottom border color for both merged cells" do
@cell.change_border_color(:bottom, "FF0000")
expect(@worksheet[0][0].get_border_color(:bottom)).to eq("FF0000")
expect(@worksheet[0][1].get_border_color(:bottom)).to eq("FF0000")
expect(@worksheet[1][0].get_border_color(:bottom)).to eq("FF0000")
expect(@worksheet[1][1].get_border_color(:bottom)).to eq("FF0000")
end
end
end

describe '.change_border' do
Expand Down Expand Up @@ -214,6 +312,104 @@
@cell.change_border(:diagonal, 'thin')
expect(@cell.get_border(:diagonal)).to eq('thin')
end

context "when the cell is merged horizontally" do
before :each do
@worksheet.merge_cells(0, 0, 0, 1)
end

it "should change the left border of both merged cells" do
@cell.change_border(:left, "thin")
expect(@worksheet[0][0].get_border(:left)).to eq("thin")
expect(@worksheet[0][1].get_border(:left)).to eq("thin")
end

it "should change the right border of the both merged cells" do
@cell.change_border(:right, "thin")
expect(@worksheet[0][0].get_border(:right)).to eq("thin")
expect(@worksheet[0][1].get_border(:right)).to eq("thin")
end

it "should change the top border for both merged cells" do
@cell.change_border(:top, "thin")
expect(@worksheet[0][0].get_border(:top)).to eq("thin")
expect(@worksheet[0][1].get_border(:top)).to eq("thin")
end

it "should change the bottom border for both merged cells" do
@cell.change_border(:bottom, "thin")
expect(@worksheet[0][0].get_border(:bottom)).to eq("thin")
expect(@worksheet[0][1].get_border(:bottom)).to eq("thin")
end
end

context "when the cell is merged vertically" do
before :each do
@worksheet.merge_cells(0, 0, 1, 0)
end

it "should change the left border of both merged cells" do
@cell.change_border(:left, "thin")
expect(@worksheet[0][0].get_border(:left)).to eq("thin")
expect(@worksheet[1][0].get_border(:left)).to eq("thin")
end

it "should change the right border of the both merged cells" do
@cell.change_border(:right, "thin")
expect(@worksheet[0][0].get_border(:right)).to eq("thin")
expect(@worksheet[1][0].get_border(:right)).to eq("thin")
end

it "should change the top border for both merged cells" do
@cell.change_border(:top, "thin")
expect(@worksheet[0][0].get_border(:top)).to eq("thin")
expect(@worksheet[1][0].get_border(:top)).to eq("thin")
end

it "should change the bottom border for both merged cells" do
@cell.change_border(:bottom, "thin")
expect(@worksheet[0][0].get_border(:bottom)).to eq("thin")
expect(@worksheet[1][0].get_border(:bottom)).to eq("thin")
end
end

context "when the cell is merged horizontally and veritically" do
before :each do
@worksheet.merge_cells(0, 0, 1, 1)
end

it "should change the left border of both merged cells" do
@cell.change_border(:left, "thin")
expect(@worksheet[0][0].get_border(:left)).to eq("thin")
expect(@worksheet[0][1].get_border(:left)).to eq("thin")
expect(@worksheet[1][0].get_border(:left)).to eq("thin")
expect(@worksheet[1][1].get_border(:left)).to eq("thin")
end

it "should change the right border of the both merged cells" do
@cell.change_border(:right, "thin")
expect(@worksheet[0][0].get_border(:right)).to eq("thin")
expect(@worksheet[0][1].get_border(:right)).to eq("thin")
expect(@worksheet[1][0].get_border(:right)).to eq("thin")
expect(@worksheet[1][1].get_border(:right)).to eq("thin")
end

it "should change the top border for both merged cells" do
@cell.change_border(:top, "thin")
expect(@worksheet[0][0].get_border(:top)).to eq("thin")
expect(@worksheet[0][1].get_border(:top)).to eq("thin")
expect(@worksheet[1][0].get_border(:top)).to eq("thin")
expect(@worksheet[1][1].get_border(:top)).to eq("thin")
end

it "should change the bottom border for both merged cells" do
@cell.change_border(:bottom, "thin")
expect(@worksheet[0][0].get_border(:bottom)).to eq("thin")
expect(@worksheet[0][1].get_border(:bottom)).to eq("thin")
expect(@worksheet[1][0].get_border(:bottom)).to eq("thin")
expect(@worksheet[1][1].get_border(:bottom)).to eq("thin")
end
end
end

describe '.value' do
Expand Down
Loading