Class: WordWps::Text
- Inherits:
-
Object
- Object
- WordWps::Text
- Defined in:
- lib/word_wps.rb
Overview
只对内容服务
Instance Method Summary collapse
-
#add_head_image(path) ⇒ Object
插入头部的图片.
-
#add_image(path) ⇒ Object
插入图片.
-
#add_table(row, col, &block) ⇒ Object
插入表格.
-
#add_table_by_value(tbl, &block) ⇒ Object
通过已有的数组插入表格.
-
#center ⇒ Object
文本居中.
-
#chart(tbl, &block) ⇒ Object
绘制图表 测试图表 tbl = [ [“a”, “1”], [“b”, “2”], [“c”, “3”], [“d”, “4”], ] text.chart(tbl) do |chart| chart.title = “Name” chart.type = 5 #chart.axes_x = “year” #chart.axes_y = “value” chart.style = 251 end.
-
#doc_work ⇒ Object
ActiveDocument.
-
#entry ⇒ Object
回车.
-
#first_line_indent=(cent) ⇒ Object
修改首行缩进(厘米).
-
#font ⇒ Object
调整字体.
-
#initialize(selection, document) ⇒ Text
constructor
A new instance of Text.
-
#insert_line ⇒ Object
插入一条横线.
-
#insert_table(row, col) ⇒ Object
插入表格,并返回Table类.
-
#justify ⇒ Object
两端对其.
-
#left ⇒ Object
文本左对齐.
-
#move_down ⇒ Object
移动到下一行.
-
#now ⇒ Object
ActiveDocument.Selecton.
-
#page_break ⇒ Object
插入分页符.
-
#picture_size(int) ⇒ Object
修改最近一个图片尺寸倍数.
-
#print(str) ⇒ Object
写入元数据.
-
#puts(str) ⇒ Object
写入数据,抬头Tab, 并且换行.
-
#replace(find, replace, style) ⇒ Object
替换,并替换风格.
-
#right ⇒ Object
文本右对齐.
-
#section(style_name = nil) ⇒ Object
按一个段设置风格和内容.
-
#size=(int) ⇒ Object
调整字体大小.
-
#style(name) ⇒ Object
(also: #style=)
设置风格.
Constructor Details
#initialize(selection, document) ⇒ Text
Returns a new instance of Text.
180 181 182 183 184 185 186 |
# File 'lib/word_wps.rb', line 180 def initialize(selection, document) # 现在所选项 @selection = selection # Document 类 @document = document end |
Instance Method Details
#add_head_image(path) ⇒ Object
插入头部的图片
318 319 320 321 322 323 324 |
# File 'lib/word_wps.rb', line 318 def add_head_image(path) self.first_line_indent = -2 add_image(path) picture_size(1.25) self.entry self.first_line_indent = 0 end |
#add_image(path) ⇒ Object
插入图片
327 328 329 |
# File 'lib/word_wps.rb', line 327 def add_image(path) now.InlineShapes.AddPicture(path, false, true) end |
#add_table(row, col, &block) ⇒ Object
插入表格
294 295 296 297 298 299 300 |
# File 'lib/word_wps.rb', line 294 def add_table(row, col, &block) table = insert_table(row, col) block.call(table) # 跳出表格 now.MoveDown(5, 1) end |
#add_table_by_value(tbl, &block) ⇒ Object
通过已有的数组插入表格
303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/word_wps.rb', line 303 def add_table_by_value(tbl, &block) row = tbl.size col = tbl[0].size table = insert_table(row, col) block.call(table) # 保证在写入数据的时候在第一个单元格 table.top # 一维化数组 table << tbl.flatten # 跳出表格 now.MoveDown(5, 1) end |
#center ⇒ Object
文本居中
214 215 216 |
# File 'lib/word_wps.rb', line 214 def center now.ParagraphFormat.Alignment = 1 end |
#chart(tbl, &block) ⇒ Object
绘制图表测试图表tbl = [
["a", "1"],
["b", "2"],
["c", "3"],
["d", "4"],
] text.chart(tbl) do |chart|
chart.title = "Name"
chart.type = 5
#chart.axes_x = "year"
#chart.axes_y = "value"
chart.style = 251
end
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/word_wps.rb', line 367 def chart(tbl, &block) begin excel = ExcelWps::WorkBook.new #excel.show excel.display_alerts = false worksheet = excel.add_worksheet("sheet") tbl.each do |r| worksheet.add_row { |row| row << r } end # 获取结束的单元格 col = tbl[0].size end_cell = worksheet.current_row.cell_name(col - 1) worksheet.add_chart do |chart| chart.source = worksheet.range("A1:#{end_cell}") block.call(chart) end ensure excel.close end self.style("正文") self.center doc_work.Application.Selection.PasteAndFormat(13) # 移动到下一行 self.move_down self.left end |
#doc_work ⇒ Object
ActiveDocument
189 190 191 |
# File 'lib/word_wps.rb', line 189 def doc_work @document.doc_work end |
#entry ⇒ Object
回车
229 230 231 |
# File 'lib/word_wps.rb', line 229 def entry @document.entry end |
#first_line_indent=(cent) ⇒ Object
修改首行缩进(厘米)
332 333 334 |
# File 'lib/word_wps.rb', line 332 def first_line_indent=(cent) now.ParagraphFormat.FirstLineIndent = @document.cent_to_point(cent) end |
#font ⇒ Object
调整字体
199 200 201 |
# File 'lib/word_wps.rb', line 199 def font now.Font end |
#insert_line ⇒ Object
插入一条横线
344 345 346 347 348 349 350 |
# File 'lib/word_wps.rb', line 344 def insert_line # 通过导入图片的方法 path = File.join(BaseFile, "aio", "resource", "line.png") self.add_image(path) self.move_down end |
#insert_table(row, col) ⇒ Object
插入表格,并返回Table类
288 289 290 291 |
# File 'lib/word_wps.rb', line 288 def insert_table(row, col) obj = @document.add_table(row, col) Table.new(now, row, col, obj) end |
#justify ⇒ Object
两端对其
224 225 226 |
# File 'lib/word_wps.rb', line 224 def justify now.ParagraphFormat.Alignment = 3 end |
#left ⇒ Object
文本左对齐
209 210 211 |
# File 'lib/word_wps.rb', line 209 def left now.ParagraphFormat.Alignment = 0 end |
#move_down ⇒ Object
移动到下一行
234 235 236 |
# File 'lib/word_wps.rb', line 234 def move_down @document.move_down end |
#now ⇒ Object
ActiveDocument.Selecton
194 195 196 |
# File 'lib/word_wps.rb', line 194 def now @selection end |
#page_break ⇒ Object
插入分页符
399 400 401 |
# File 'lib/word_wps.rb', line 399 def page_break now.InsertBreak(7) end |
#picture_size(int) ⇒ Object
修改最近一个图片尺寸倍数
337 338 339 340 341 |
# File 'lib/word_wps.rb', line 337 def picture_size(int) num = doc_work.InlineShapes.Count pic = doc_work.InlineShapes(num) pic.width *= int end |
#print(str) ⇒ Object
写入元数据
239 240 241 |
# File 'lib/word_wps.rb', line 239 def print(str) now.TypeText(str) end |
#puts(str) ⇒ Object
写入数据,抬头Tab, 并且换行
244 245 246 247 |
# File 'lib/word_wps.rb', line 244 def puts(str) now.TypeText("\t" + str) self.entry end |
#replace(find, replace, style) ⇒ Object
替换,并替换风格
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/word_wps.rb', line 250 def replace(find, replace, style) rep_opt = now.Find rep_opt.ClearFormatting rep_opt.Replacement.ClearFormatting rep_opt.Replacement.Style = doc_work.Styles(style) rep_opt.Text = find rep_opt.Replacement.Text = replace rep_opt.Forward = true rep_opt.wrap = 1 # 到达开头或结尾时继续搜索 rep_opt.Format = true rep_opt.MatchCase = false rep_opt.MatchWholeWord = false rep_opt.MatchByte = true rep_opt.MatchWildcards = false rep_opt.MatchSoundsLike = false rep_opt.MatchAllWordForms = false rep_opt.Execute(nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,1) end |
#right ⇒ Object
文本右对齐
219 220 221 |
# File 'lib/word_wps.rb', line 219 def right now.ParagraphFormat.Alignment = 2 end |
#section(style_name = nil) ⇒ Object
按一个段设置风格和内容
277 278 279 280 281 282 283 284 285 |
# File 'lib/word_wps.rb', line 277 def section(style_name=nil) self.style(style_name) unless style_name.nil? res = yield if res.kind_of? ::Array res.join("\n") end print(res) self.entry end |
#size=(int) ⇒ Object
调整字体大小
204 205 206 |
# File 'lib/word_wps.rb', line 204 def size=(int) self.font.Size = int end |
#style(name) ⇒ Object Also known as: style=
设置风格
272 273 274 |
# File 'lib/word_wps.rb', line 272 def style(name) now.Style = @document.styles(name) end |