class Aio::Base::Toolkit::WordWps::Text

只对内容服务

Constants

Alignment
FirstLineIndent
Format
Forward
MatchAllWordForms
MatchByte
MatchCase
MatchSoundsLike
MatchWholeWord
MatchWildcards
Size
Style
Text

Public Class Methods

new(selection, document) click to toggle source
# File lib/aio/base/toolkit/word_wps.rb, line 182
def initialize(selection, document)
  # 现在所选项
  @selection = selection

  # Document 类
  @document = document
end

Public Instance Methods

add_head_image(path) click to toggle source

插入头部的图片

# 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) click to toggle source

插入图片

# 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) click to toggle source

插入表格

# 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) click to toggle source

通过已有的数组插入表格

# 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
center() click to toggle source

文本居中

# File lib/aio/base/toolkit/word_wps.rb, line 216
def center
  now.ParagraphFormat.Alignment = 1
end
chart(tbl, &block) click to toggle source
绘制图表
测试图表

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

# 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_work() click to toggle source

ActiveDocument

# File lib/aio/base/toolkit/word_wps.rb, line 191
def doc_work
  @document.doc_work
end
entry() click to toggle source

回车

# File lib/aio/base/toolkit/word_wps.rb, line 231
def entry
  @document.entry
end
first_line_indent=(cent) click to toggle source

修改首行缩进(厘米)

# File lib/aio/base/toolkit/word_wps.rb, line 334
def first_line_indent=(cent)
  now.ParagraphFormat.FirstLineIndent = @document.cent_to_point(cent)
end
font() click to toggle source

调整字体

# File lib/aio/base/toolkit/word_wps.rb, line 201
def font
  now.Font
end
insert_line() click to toggle source

插入一条横线

# 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) click to toggle source

插入表格,并返回Table类

# 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
justify() click to toggle source

两端对其

# File lib/aio/base/toolkit/word_wps.rb, line 226
def justify
  now.ParagraphFormat.Alignment = 3
end
left() click to toggle source

文本左对齐

# File lib/aio/base/toolkit/word_wps.rb, line 211
def left
  now.ParagraphFormat.Alignment = 0
end
move_down() click to toggle source

移动到下一行

# File lib/aio/base/toolkit/word_wps.rb, line 236
def move_down
  @document.move_down
end
now() click to toggle source

ActiveDocument.Selecton

# File lib/aio/base/toolkit/word_wps.rb, line 196
def now
  @selection
end
page_break() click to toggle source

插入分页符

# File lib/aio/base/toolkit/word_wps.rb, line 405
def page_break
  now.InsertBreak(7)
end
picture_size(int) click to toggle source

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

# 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
print(str) click to toggle source

写入元数据

puts(str) click to toggle source

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

# File lib/aio/base/toolkit/word_wps.rb, line 246
def puts(str)
  now.TypeText("\t" + str)
  self.entry
end
replace(find, replace, style) click to toggle source

替换,并替换风格

# 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
right() click to toggle source

文本右对齐

# File lib/aio/base/toolkit/word_wps.rb, line 221
def right
  now.ParagraphFormat.Alignment = 2
end
section(style_name=nil) { || ... } click to toggle source

按一个段设置风格和内容

# 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) click to toggle source

调整字体大小

# File lib/aio/base/toolkit/word_wps.rb, line 206
def size=(int)
  self.font.Size = int
end
style(name) click to toggle source

设置风格

# File lib/aio/base/toolkit/word_wps.rb, line 274
def style(name)
  now.Style = @document.styles(name)
end
Also aliased as: style=
style=(name)
Alias for: style