Skip to content

Commit 21e8dad

Browse files
committed
Update README.md with table writing example: copy of a row and string substitution in a cell
1 parent e78cecf commit 21e8dad

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,31 @@ end
134134
doc.save('example-edited.docx')
135135
```
136136

137+
### Writing to tables
138+
139+
``` ruby
140+
require 'docx'
141+
142+
# Create a Docx::Document object for our existing docx file
143+
doc = Docx::Document.open('tables.docx')
144+
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')
154+
end
155+
end
156+
end
157+
end
158+
159+
doc.save('tables-edited.docx')
160+
```
161+
137162
### Advanced
138163

139164
``` ruby

0 commit comments

Comments
 (0)