File tree Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Original file line number Diff line number Diff line change @@ -142,15 +142,19 @@ require 'docx'
142142# Create a Docx::Document object for our existing docx file
143143doc = 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
You can’t perform that action at this time.
0 commit comments