Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add repeat table headers #105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ docx.table [['Header 1','Header 2'],['Cell 1', 'Cell 2']] do
border_line :single # sets the border style. defaults to :single. see OOXML docs for details.
border_size 4 # sets the border width. defaults to 0. units in twips.
border_spacing 4 # sets the spacing around the border. defaults to 0. units in twips.
repeat_header 1 # sets the number of header rows that is repeated on each page. defaults to 0.
end
```

Expand Down
8 changes: 6 additions & 2 deletions lib/caracal/core/models/table_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class TableModel < BaseModel
const_set(:DEFAULT_TABLE_BORDER_COLOR, 'auto')
const_set(:DEFAULT_TABLE_BORDER_LINE, :single)
const_set(:DEFAULT_TABLE_BORDER_SIZE, 0) # units in 1/8 points
const_set(:DEFAULT_TABLE_BORDER_SPACING, 0)
const_set(:DEFAULT_TABLE_BORDER_SPACING, 0)
const_set(:DEFAULT_TABLE_REPEAT_HEADER, 0)

# accessors
attr_reader :table_align
Expand All @@ -36,6 +37,7 @@ class TableModel < BaseModel
attr_reader :table_border_right # returns border model
attr_reader :table_border_horizontal # returns border model
attr_reader :table_border_vertical # returns border model
attr_reader :table_repeat_header

# initialization
def initialize(options={}, &block)
Expand All @@ -44,6 +46,7 @@ def initialize(options={}, &block)
@table_border_line = DEFAULT_TABLE_BORDER_LINE
@table_border_size = DEFAULT_TABLE_BORDER_SIZE
@table_border_spacing = DEFAULT_TABLE_BORDER_SPACING
@table_repeat_header = DEFAULT_TABLE_REPEAT_HEADER

super options, &block
end
Expand Down Expand Up @@ -121,7 +124,7 @@ def cell_style(models, options={})
#=============== SETTERS ==============================

# integers
[:border_size, :border_spacing, :width].each do |m|
[:border_size, :border_spacing, :width, :repeat_header].each do |m|
define_method "#{ m }" do |value|
instance_variable_set("@table_#{ m }", value.to_i)
end
Expand Down Expand Up @@ -196,6 +199,7 @@ def option_keys
k << [:data, :align, :width]
k << [:border_color, :border_line, :border_size, :border_spacing]
k << [:border_bottom, :border_left, :border_right, :border_top, :border_horizontal, :border_vertical]
k << [:repeat_header]
k.flatten
end

Expand Down
9 changes: 8 additions & 1 deletion lib/caracal/renderers/document_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,15 @@ def render_table(xml, model)
end

rowspan_hash = {}
model.rows.each do |row|
model.rows.each_with_index do |row, index|
xml['w'].tr do
if model.table_repeat_header > 0
if index < model.table_repeat_header
xml['w'].trPr do
xml['w'].tblHeader
end
end
end
row.each_with_index do |tc, tc_index|
xml['w'].tc do
xml['w'].tcPr do
Expand Down
12 changes: 11 additions & 1 deletion spec/lib/caracal/core/models/table_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
it { expect(described_class::DEFAULT_TABLE_BORDER_LINE).to eq :single }
it { expect(described_class::DEFAULT_TABLE_BORDER_SIZE).to eq 0 }
it { expect(described_class::DEFAULT_TABLE_BORDER_SPACING).to eq 0 }
it { expect(described_class::DEFAULT_TABLE_REPEAT_HEADER).to eq 0 }
end

# accessors
Expand All @@ -36,6 +37,7 @@
it { expect(subject.table_border_line).to eq :double }
it { expect(subject.table_border_size).to eq 8 }
it { expect(subject.table_border_spacing).to eq 4 }
it { expect(subject.table_repeat_header).to eq 0 }
end

end
Expand Down Expand Up @@ -179,6 +181,13 @@
it { expect(subject.table_width).to eq 7500 }
end

# .repeat_header
describe '.repeat_header' do
before { subject.repeat_header(2) }

it { expect(subject.table_repeat_header).to eq 2 }
end




Expand Down Expand Up @@ -212,7 +221,8 @@
let(:expected1) { [:data, :align, :width] }
let(:expected2) { [:border_color, :border_line, :border_size, :border_spacing] }
let(:expected3) { [:border_top, :border_bottom, :border_left, :border_right, :border_horizontal, :border_vertical] }
let(:expected) { (expected1 + expected2 + expected3).sort }
let(:expected4) { [:repeat_header] }
let(:expected) { (expected1 + expected2 + expected3 + expected4).sort }

it { expect(actual).to eq expected }
end
Expand Down