Skip to content

Commit 559b78b

Browse files
Update README.md
Add @WaKeMaTTa suggested example Co-authored-by: Mohamed Ziata <[email protected]>
1 parent 21e8dad commit 559b78b

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,19 @@ require 'docx'
142142
# Create a Docx::Document object for our existing docx file
143143
doc = Docx::Document.open('tables.docx')
144144

145-
# Copy row of a table and then substitute text in a cell
146-
doc.tables.first.rows.first.then do |row|
147-
new_row = row.copy
148-
new_row.insert_after(row)
149-
150-
row.cells.first.then do |c|
151-
c.paragraphs.each do |p|
152-
p.each_text_run do |tr|
153-
tr.substitute('_table_placeholder_', 'replacement value')
145+
# Iterate over each table
146+
doc.tables.each do |table|
147+
last_row = table.rows.last
148+
149+
# Copy last row and insert a new one before last row
150+
new_row = last_row.copy
151+
new_row.insert_before(last_row)
152+
153+
# Substitute text in each cell of this new row
154+
new_row.cells.each do |cell|
155+
cell.paragraphs.each do |paragraph|
156+
paragraph.each_text_run do |text|
157+
text.substitute('_placeholder_', 'replacement value')
154158
end
155159
end
156160
end

0 commit comments

Comments
 (0)