Class: Aio::Base::Toolkit::WordWps::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/aio/base/toolkit/word_wps.rb

Overview

只对内容服务

Instance Method Summary collapse

Constructor Details

#initialize(selection, document) ⇒ Text

Returns a new instance of Text.



182
183
184
185
186
187
188
# File 'lib/aio/base/toolkit/word_wps.rb', line 182

def initialize(selection, document)
  # 现在所选项
  @selection = selection

  # Document 类
  @document = document
end

Instance Method Details

#add_head_image(path) ⇒ Object

插入头部的图片



320
321
322
323
324
325
326
# File 'lib/aio/base/toolkit/word_wps.rb', line 320

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

插入图片



329
330
331
# File 'lib/aio/base/toolkit/word_wps.rb', line 329

def add_image(path)
  now.InlineShapes.AddPicture(path, false, true)
end

#add_table(row, col, &block) ⇒ Object

插入表格



296
297
298
299
300
301
302
# File 'lib/aio/base/toolkit/word_wps.rb', line 296

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

通过已有的数组插入表格



305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/aio/base/toolkit/word_wps.rb', line 305

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

#centerObject

文本居中



216
217
218
# File 'lib/aio/base/toolkit/word_wps.rb', line 216

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



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
397
398
399
400
401
402
# File 'lib/aio/base/toolkit/word_wps.rb', line 369

def chart(tbl, &block)

  # 当没有内容的时候,则不绘制
  return nil if tbl.empty?

  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_workObject

ActiveDocument



191
192
193
# File 'lib/aio/base/toolkit/word_wps.rb', line 191

def doc_work
  @document.doc_work
end

#entryObject

回车



231
232
233
# File 'lib/aio/base/toolkit/word_wps.rb', line 231

def entry
  @document.entry
end

#first_line_indent=(cent) ⇒ Object

修改首行缩进(厘米)



334
335
336
# File 'lib/aio/base/toolkit/word_wps.rb', line 334

def first_line_indent=(cent)
  now.ParagraphFormat.FirstLineIndent = @document.cent_to_point(cent)
end

#fontObject

调整字体



201
202
203
# File 'lib/aio/base/toolkit/word_wps.rb', line 201

def font
  now.Font
end

#insert_lineObject

插入一条横线



346
347
348
349
350
351
352
# File 'lib/aio/base/toolkit/word_wps.rb', line 346

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类



290
291
292
293
# File 'lib/aio/base/toolkit/word_wps.rb', line 290

def insert_table(row, col)
  obj = @document.add_table(row, col)
  Table.new(now, row, col, obj)
end

#justifyObject

两端对其



226
227
228
# File 'lib/aio/base/toolkit/word_wps.rb', line 226

def justify
  now.ParagraphFormat.Alignment = 3
end

#leftObject

文本左对齐



211
212
213
# File 'lib/aio/base/toolkit/word_wps.rb', line 211

def left
  now.ParagraphFormat.Alignment = 0
end

#move_downObject

移动到下一行



236
237
238
# File 'lib/aio/base/toolkit/word_wps.rb', line 236

def move_down
  @document.move_down
end

#nowObject

ActiveDocument.Selecton



196
197
198
# File 'lib/aio/base/toolkit/word_wps.rb', line 196

def now
  @selection
end

#page_breakObject

插入分页符



405
406
407
# File 'lib/aio/base/toolkit/word_wps.rb', line 405

def page_break
  now.InsertBreak(7)
end

#picture_size(int) ⇒ Object

修改最近一个图片尺寸倍数



339
340
341
342
343
# File 'lib/aio/base/toolkit/word_wps.rb', line 339

def picture_size(int)
  num = doc_work.InlineShapes.Count
  pic = doc_work.InlineShapes(num)
  pic.width *= int
end

写入元数据



241
242
243
# File 'lib/aio/base/toolkit/word_wps.rb', line 241

def print(str)
  now.TypeText(str)
end

#puts(str) ⇒ Object

写入数据,抬头Tab, 并且换行



246
247
248
249
# File 'lib/aio/base/toolkit/word_wps.rb', line 246

def puts(str)
  now.TypeText("\t" + str)
  self.entry
end

#replace(find, replace, style) ⇒ Object

替换,并替换风格



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/aio/base/toolkit/word_wps.rb', line 252

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

#rightObject

文本右对齐



221
222
223
# File 'lib/aio/base/toolkit/word_wps.rb', line 221

def right
  now.ParagraphFormat.Alignment = 2
end

#section(style_name = nil) ⇒ Object

按一个段设置风格和内容



279
280
281
282
283
284
285
286
287
# File 'lib/aio/base/toolkit/word_wps.rb', line 279

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

调整字体大小



206
207
208
# File 'lib/aio/base/toolkit/word_wps.rb', line 206

def size=(int)
  self.font.Size = int
end

#style(name) ⇒ Object Also known as: style=

设置风格



274
275
276
# File 'lib/aio/base/toolkit/word_wps.rb', line 274

def style(name)
  now.Style = @document.styles(name)
end