office_elin 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/word_wps.rb ADDED
@@ -0,0 +1,488 @@
1
+ module WordWps
2
+ class Word
3
+
4
+ def initialize(encoding = "utf-8")
5
+
6
+ if OS.windows?
7
+ require "win32ole"
8
+ else
9
+ print "只有Windows系统才能使用Excel模块"
10
+ exit 0
11
+ end
12
+
13
+
14
+ @word = WIN32OLE.new("Word.Application")
15
+ @word.visible = false
16
+ @encoding = encoding
17
+ end
18
+
19
+ # 警告提示开关
20
+ def display_alerts=(bool)
21
+ @word.DisplayAlerts = bool
22
+ end
23
+
24
+ def init_document
25
+ @document = @word.Documents.add
26
+ return Document.new(@document, @word)
27
+ end
28
+
29
+
30
+ def show
31
+ @word.visible = true
32
+ end
33
+
34
+ def save(path)
35
+ @document.saveas(path)
36
+ end
37
+
38
+ def close
39
+ @document.close
40
+ @word.quit
41
+ end
42
+ end
43
+
44
+ # 对文本控件的调用
45
+ class Document
46
+
47
+ def initialize(document, word)
48
+ @doc_work = document
49
+ @word = word
50
+ #@text = Text.new(@word.Selection, self)
51
+ create_style
52
+ end
53
+
54
+ # ActiveDocument
55
+ def doc_work
56
+ @doc_work
57
+ end
58
+
59
+ # 自动化对象
60
+ def word_basic
61
+ doc_work.Application.WordBasic
62
+ end
63
+
64
+ # 创建样式
65
+ def create_style
66
+
67
+ # 正文 楷体五号
68
+ sty = @doc_work.styles("正文")
69
+ sty.font.size = 10
70
+ sty.font.NameFarEast = "宋体"
71
+ sty.font.NameAscii = "Times New Roman"
72
+ sty.font.NameOther = "Times New Roman"
73
+ sty.ParagraphFormat.Alignment = 3
74
+ sty.ParagraphFormat.LineSpacingRule = 1
75
+ sty.NextParagraphStyle = "正文"
76
+
77
+ # 楷体3号字体
78
+ sty = @doc_work.styles.add("Cover 1", 1)
79
+ sty.font.size = 16
80
+ sty.font.NameFarEast = "楷体"
81
+ sty.font.NameAscii = "Times New Roman"
82
+ sty.font.NameOther = "Times New Roman"
83
+ end
84
+
85
+ # 由实例自己创建风格
86
+ def create_style_self(name)
87
+ sty = @doc_work.styles.add(name, 1)
88
+ yield sty
89
+ end
90
+
91
+ # 返回Selection
92
+ def now
93
+ @word.Selection
94
+ end
95
+
96
+ # 返回Text类
97
+ def add_text
98
+ return Text.new(@word.Selection, self)
99
+ entry
100
+ end
101
+
102
+ # 风格设置
103
+ def styles(name)
104
+ @doc_work.Styles(name)
105
+ end
106
+
107
+ # 设置表格
108
+ def add_table(row, col)
109
+ @doc_work.tables.add(now.Range, row, col)
110
+ end
111
+
112
+ # 创建目录
113
+ def create_catalog
114
+ #range = doc_work.Range(0, 0)
115
+ range = now.range
116
+ doc_work.TablesOfContents.Add(
117
+ range, # Range
118
+ true, # UseHeadingStyles
119
+ 1, # UpperHeadingLevel
120
+ 3, # LowerHeadingLevel default: 9
121
+ false, # UseFields
122
+ nil, # TableId
123
+ true, # RightAlignPageNumbers
124
+ true, # IncludePageNumbers
125
+ "", # AddedStyles
126
+ true, # UseHyperlinks
127
+ true, # HidePageNumbersInWeb
128
+ true, # UseOutlineLevels default: false
129
+ )
130
+
131
+ # 换到下一行
132
+ move_down
133
+ end
134
+
135
+ # 更新目录
136
+ def update_catalog
137
+ word_basic.UpdateTableOfContents
138
+ end
139
+
140
+ # 回车
141
+ def entry
142
+ now.TypeParagraph
143
+ end
144
+
145
+ # 移动到行首
146
+ def home_key
147
+ now.HomeKey(5)
148
+ end
149
+
150
+ # 移动到行尾
151
+ def end_key
152
+ now.EndKey(5)
153
+ end
154
+
155
+ # 移动到下一行
156
+ def move_down
157
+ self.end_key
158
+ self.entry
159
+ end
160
+
161
+ # 右移
162
+ # count 数值, 移动距离
163
+ # ext 0or1 , 是否扩展
164
+ # unit wdCharachter
165
+ def move_right(count=nil, ext=nil, unit=nil)
166
+ now.MoveRight(unit, count, ext)
167
+ end
168
+
169
+ # 将厘米换算成磅
170
+ def cent_to_point(int)
171
+ @word.CentimetersToPoints(int)
172
+ end
173
+
174
+ end
175
+
176
+ # 只对内容服务
177
+ class Text
178
+
179
+ def initialize(selection, document)
180
+ # 现在所选项
181
+ @selection = selection
182
+
183
+ # Document 类
184
+ @document = document
185
+ end
186
+
187
+ # ActiveDocument
188
+ def doc_work
189
+ @document.doc_work
190
+ end
191
+
192
+ # ActiveDocument.Selecton
193
+ def now
194
+ @selection
195
+ end
196
+
197
+ # 调整字体
198
+ def font
199
+ now.Font
200
+ end
201
+
202
+ # 调整字体大小
203
+ def size=(int)
204
+ self.font.Size = int
205
+ end
206
+
207
+ # 文本左对齐
208
+ def left
209
+ now.ParagraphFormat.Alignment = 0
210
+ end
211
+
212
+ # 文本居中
213
+ def center
214
+ now.ParagraphFormat.Alignment = 1
215
+ end
216
+
217
+ # 文本右对齐
218
+ def right
219
+ now.ParagraphFormat.Alignment = 2
220
+ end
221
+
222
+ # 两端对其
223
+ def justify
224
+ now.ParagraphFormat.Alignment = 3
225
+ end
226
+
227
+ # 回车
228
+ def entry
229
+ @document.entry
230
+ end
231
+
232
+ # 移动到下一行
233
+ def move_down
234
+ @document.move_down
235
+ end
236
+
237
+ # 写入元数据
238
+ def print(str)
239
+ now.TypeText(str)
240
+ end
241
+
242
+ # 写入数据,抬头Tab, 并且换行
243
+ def puts(str)
244
+ now.TypeText("\t" + str)
245
+ self.entry
246
+ end
247
+
248
+ # 替换,并替换风格
249
+ def replace(find, replace, style)
250
+ rep_opt = now.Find
251
+ rep_opt.ClearFormatting
252
+ rep_opt.Replacement.ClearFormatting
253
+ rep_opt.Replacement.Style = doc_work.Styles(style)
254
+ rep_opt.Text = find
255
+ rep_opt.Replacement.Text = replace
256
+ rep_opt.Forward = true
257
+ rep_opt.wrap = 1 # 到达开头或结尾时继续搜索
258
+ rep_opt.Format = true
259
+ rep_opt.MatchCase = false
260
+ rep_opt.MatchWholeWord = false
261
+ rep_opt.MatchByte = true
262
+ rep_opt.MatchWildcards = false
263
+ rep_opt.MatchSoundsLike = false
264
+ rep_opt.MatchAllWordForms = false
265
+
266
+ rep_opt.Execute(nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,1)
267
+ end
268
+
269
+
270
+ # 设置风格
271
+ def style(name)
272
+ now.Style = @document.styles(name)
273
+ end
274
+
275
+ # 按一个段设置风格和内容
276
+ def section(style_name=nil)
277
+ self.style(style_name) unless style_name.nil?
278
+ res = yield
279
+ if res.kind_of? ::Array
280
+ res.join("\n")
281
+ end
282
+ print(res)
283
+ self.entry
284
+ end
285
+
286
+ # 插入表格,并返回Table类
287
+ def insert_table(row, col)
288
+ obj = @document.add_table(row, col)
289
+ Table.new(now, row, col, obj)
290
+ end
291
+
292
+ # 插入表格
293
+ def add_table(row, col, &block)
294
+ table = insert_table(row, col)
295
+ block.call(table)
296
+
297
+ # 跳出表格
298
+ now.MoveDown(5, 1)
299
+ end
300
+
301
+ # 通过已有的数组插入表格
302
+ def add_table_by_value(tbl, &block)
303
+ row = tbl.size
304
+ col = tbl[0].size
305
+ table = insert_table(row, col)
306
+ block.call(table)
307
+
308
+ # 保证在写入数据的时候在第一个单元格
309
+ table.top
310
+ # 一维化数组
311
+ table << tbl.flatten
312
+ # 跳出表格
313
+ now.MoveDown(5, 1)
314
+ end
315
+
316
+ # 插入头部的图片
317
+ def add_head_image(path)
318
+ self.first_line_indent = -2
319
+ add_image(path)
320
+ picture_size(1.25)
321
+ self.entry
322
+ self.first_line_indent = 0
323
+ end
324
+
325
+ # 插入图片
326
+ def add_image(path)
327
+ now.InlineShapes.AddPicture(path, false, true)
328
+ end
329
+
330
+ # 修改首行缩进(厘米)
331
+ def first_line_indent=(cent)
332
+ now.ParagraphFormat.FirstLineIndent = @document.cent_to_point(cent)
333
+ end
334
+
335
+ # 修改最近一个图片尺寸倍数
336
+ def picture_size(int)
337
+ num = doc_work.InlineShapes.Count
338
+ pic = doc_work.InlineShapes(num)
339
+ pic.width *= int
340
+ end
341
+
342
+ # 插入一条横线
343
+ def insert_line
344
+
345
+ # 通过导入图片的方法
346
+ path = File.join(BaseFile, "aio", "resource", "line.png")
347
+ self.add_image(path)
348
+ self.move_down
349
+ end
350
+
351
+ # 绘制图表
352
+ # 测试图表
353
+ #tbl = [
354
+ # ["a", "1"],
355
+ # ["b", "2"],
356
+ # ["c", "3"],
357
+ # ["d", "4"],
358
+ #]
359
+ #text.chart(tbl) do |chart|
360
+ # chart.title = "Name"
361
+ # chart.type = 5
362
+ # #chart.axes_x = "year"
363
+ # #chart.axes_y = "value"
364
+ # chart.style = 251
365
+ #end
366
+ def chart(tbl, &block)
367
+ begin
368
+ excel = ExcelWps::WorkBook.new
369
+ #excel.show
370
+ excel.display_alerts = false
371
+ worksheet = excel.add_worksheet("sheet")
372
+ tbl.each do |r|
373
+ worksheet.add_row { |row| row << r }
374
+ end
375
+
376
+ # 获取结束的单元格
377
+ col = tbl[0].size
378
+ end_cell = worksheet.current_row.cell_name(col - 1)
379
+
380
+ worksheet.add_chart do |chart|
381
+ chart.source = worksheet.range("A1:#{end_cell}")
382
+ block.call(chart)
383
+ end
384
+ ensure
385
+ excel.close
386
+ end
387
+
388
+ self.style("正文")
389
+ self.center
390
+ doc_work.Application.Selection.PasteAndFormat(13)
391
+
392
+ # 移动到下一行
393
+ self.move_down
394
+ self.left
395
+ end
396
+
397
+ # 插入分页符
398
+ def page_break
399
+ now.InsertBreak(7)
400
+ end
401
+
402
+ alias :style= :style
403
+ end
404
+
405
+ # 表格
406
+ class Table
407
+ def initialize(selection, row, col, obj)
408
+ @row = row
409
+ @col = col
410
+ @count = row * col
411
+ @selection = selection
412
+ @obj = obj
413
+ end
414
+
415
+ def now
416
+ @selection
417
+ end
418
+
419
+ # 合并列
420
+ def merge_row
421
+ now.SelectRow
422
+ now.Cells.Merge
423
+ @count = @count - @row + 1
424
+ end
425
+
426
+ # 放入数据
427
+ def puts(str)
428
+ now.SelectCell
429
+ now.TypeText(str)
430
+ @count -= 1
431
+ now.MoveRight(12) unless @count < 1
432
+ end
433
+
434
+ # 放入数据, 可以按数组放入
435
+ def <<(arr)
436
+ case arr
437
+ when ::String
438
+ puts(arr)
439
+ when ::Array
440
+ arr.each { |a| puts(a) }
441
+ end
442
+ end
443
+
444
+ # 设置行高
445
+ def height=(point)
446
+ now.Rows.Height = point
447
+ end
448
+
449
+ # 设置风格
450
+ def style=(sty)
451
+ select_all
452
+ now.Style = sty
453
+ end
454
+
455
+ # 设置列宽度
456
+ def set_columns_width(col, point)
457
+ @obj.Columns(col).SetWidth(point, 2)
458
+ end
459
+
460
+ # 移动到第一个单元格
461
+ def top
462
+ @obj.Cell(1,1).Select
463
+ end
464
+
465
+ # 居中
466
+ def center
467
+ select_all
468
+ now.ParagraphFormat.Alignment = 1
469
+ end
470
+
471
+ # 选择整个表格
472
+ def select_all
473
+ @obj.Select
474
+ end
475
+
476
+ # 网格线
477
+ def border_line
478
+ select_all
479
+ now.Borders(-1).LineStyle = 1
480
+ now.Borders(-2).LineStyle = 1
481
+ now.Borders(-3).LineStyle = 1
482
+ now.Borders(-4).LineStyle = 1
483
+ now.Borders(-5).LineStyle = 1
484
+ now.Borders(-6).LineStyle = 1
485
+ end
486
+ end
487
+
488
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: office_elin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Elin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-31 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: WPS excel 和 word 控制器
14
+ email: 365433079@qq.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files:
18
+ - README.md
19
+ files:
20
+ - LICENSE.txt
21
+ - README.md
22
+ - lib/excel_office.rb
23
+ - lib/excel_wps.rb
24
+ - lib/office.rb
25
+ - lib/os.rb
26
+ - lib/string.rb
27
+ - lib/word_wps.rb
28
+ homepage: http://rubygems.org/gems/office
29
+ licenses:
30
+ - NGPL
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.7.6
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: WPS excel and word
52
+ test_files: []