Class: WordWps::Text

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

Overview

只对内容服务

Instance Method Summary collapse

Constructor Details

#initialize(selection, document) ⇒ Text

Returns a new instance of Text.



179
180
181
182
183
184
185
# File 'lib/word_wps.rb', line 179

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

	# Document 类
	@document = document
end

Instance Method Details

#add_head_image(path) ⇒ Object

插入头部的图片



317
318
319
320
321
322
323
# File 'lib/word_wps.rb', line 317

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

插入图片



326
327
328
# File 'lib/word_wps.rb', line 326

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

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

插入表格



293
294
295
296
297
298
299
# File 'lib/word_wps.rb', line 293

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

通过已有的数组插入表格



302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/word_wps.rb', line 302

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

文本居中



213
214
215
# File 'lib/word_wps.rb', line 213

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



366
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
# File 'lib/word_wps.rb', line 366

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_workObject

ActiveDocument



188
189
190
# File 'lib/word_wps.rb', line 188

def doc_work
	@document.doc_work
end

#entryObject

回车



228
229
230
# File 'lib/word_wps.rb', line 228

def entry
	@document.entry
end

#first_line_indent=(cent) ⇒ Object

修改首行缩进(厘米)



331
332
333
# File 'lib/word_wps.rb', line 331

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

#fontObject

调整字体



198
199
200
# File 'lib/word_wps.rb', line 198

def font
	now.Font
end

#insert_lineObject

插入一条横线



343
344
345
346
347
348
349
# File 'lib/word_wps.rb', line 343

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类



287
288
289
290
# File 'lib/word_wps.rb', line 287

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

#justifyObject

两端对其



223
224
225
# File 'lib/word_wps.rb', line 223

def justify
	now.ParagraphFormat.Alignment = 3
end

#leftObject

文本左对齐



208
209
210
# File 'lib/word_wps.rb', line 208

def left
	now.ParagraphFormat.Alignment = 0
end

#move_downObject

移动到下一行



233
234
235
# File 'lib/word_wps.rb', line 233

def move_down
	@document.move_down
end

#nowObject

ActiveDocument.Selecton



193
194
195
# File 'lib/word_wps.rb', line 193

def now
	@selection
end

#page_breakObject

插入分页符



398
399
400
# File 'lib/word_wps.rb', line 398

def page_break
	now.InsertBreak(7)
end

#picture_size(int) ⇒ Object

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



336
337
338
339
340
# File 'lib/word_wps.rb', line 336

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

写入元数据



238
239
240
# File 'lib/word_wps.rb', line 238

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

#puts(str) ⇒ Object

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



243
244
245
246
# File 'lib/word_wps.rb', line 243

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

#replace(find, replace, style) ⇒ Object

替换,并替换风格



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/word_wps.rb', line 249

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

文本右对齐



218
219
220
# File 'lib/word_wps.rb', line 218

def right
	now.ParagraphFormat.Alignment = 2
end

#section(style_name = nil) ⇒ Object

按一个段设置风格和内容



276
277
278
279
280
281
282
283
284
# File 'lib/word_wps.rb', line 276

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

调整字体大小



203
204
205
# File 'lib/word_wps.rb', line 203

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

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

设置风格



271
272
273
# File 'lib/word_wps.rb', line 271

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