class 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/word_wps.rb, line 180
def initialize(selection, document)
        # 现在所选项
        @selection = selection

        # Document 类
        @document = document
end

Public Instance Methods

add_head_image(path) click to toggle source

插入头部的图片

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

插入图片

# File lib/word_wps.rb, line 327
def add_image(path)
        now.InlineShapes.AddPicture(path, false, true)
end
add_table(row, col, &block) click to toggle source

插入表格

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

通过已有的数组插入表格

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

文本居中

# File lib/word_wps.rb, line 214
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/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() click to toggle source

ActiveDocument

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

回车

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

修改首行缩进(厘米)

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

调整字体

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

插入一条横线

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

插入表格,并返回Table类

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

两端对其

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

文本左对齐

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

移动到下一行

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

ActiveDocument.Selecton

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

插入分页符

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

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

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

写入元数据

puts(str) click to toggle source

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

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

替换,并替换风格

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

文本右对齐

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

按一个段设置风格和内容

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

调整字体大小

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

设置风格

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