gen_sheet 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 857524349be36a3f44223116375a88133a5c8a7a
4
+ data.tar.gz: 8f83041d811147a825aa0658f9edbbebc2490aae
5
+ SHA512:
6
+ metadata.gz: 526bfea3d59ea6d8a370222e8054a8a1bb8773fee0559d05a34ffb9c5f90d0b2eac76df03528f4eb1088cf4e59621f45c4134f8c4d51b37b22920fadd020b70b
7
+ data.tar.gz: d2210a34617c2cb992a8b5601009490d4832e5c72a96dc8ff2454bf082701caf70641082c1dba40e904e0b5f3a95b9fac3efa6f91d6defd8b3f41c7c24ca0440
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ *.swo
2
+ *.swp
3
+ .DS_Store
4
+ .project
5
+ Gemfile.lock
6
+ Guardfile
7
+ *.gem
8
+ /tmp
9
+ /spec/files/xls_out.xls
10
+ /spec/files/ods_out.ods
11
+ /spec/files/xls_out.ods
12
+ /spec/files/ods_out.xls
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'roo'
4
+ gem 'writeexcel'
5
+ gem 'rodf'
6
+
7
+ group :development do
8
+ gem 'libnotify'
9
+ gem 'guard'
10
+ gem 'guard-rspec'
11
+ end
data/README.md ADDED
@@ -0,0 +1,12 @@
1
+ GenSheet [![Code Climate](https://codeclimate.com/github/Genshin/GenSheet.png)](https://codeclimate.com/github/Genshin/GenSheet)
2
+ ========
3
+
4
+ GenSheet is a spreadsheet [xls and ods] generator for Roo.
5
+ Roo is awesome at reading and processing spreadsheets all on its own but has no file generation.
6
+
7
+ During export sheet data and formatting is preserved from the original sheet or Roo object.
8
+ This allows you to use spreadsheets as templates, loading them with Roo, filling or altering data, then exporting.
9
+
10
+ Future plans are to add in-sheet hooks and data markers to allow for true template-like handling of source sheets.
11
+
12
+ This library is licensed under the LGPL.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'rake'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
data/gen_sheet.gemspec ADDED
@@ -0,0 +1,11 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'gen_sheet'
3
+ s.version = '0.0.3'
4
+ s.date = '2013-03-14'
5
+ s.summary = "A spreadsheet generator (ODS, XLS) for Roo."
6
+ s.description = "Takes a Roo spreadsheet object and renders it in XLS or ODS. Also allows you to use a template sheet and fill it with data from Roo, then output a rendered sheet."
7
+ s.authors = ["Rei Kagetsuki"]
8
+ s.email = 'zero@genshin.org'
9
+ s.files = `git ls-files`.split("\n")
10
+ s.homepage = 'https://github.com/Genshin/GenSheet'
11
+ end
data/lib/GenSheet.rb ADDED
@@ -0,0 +1,167 @@
1
+ require 'roo'
2
+ require 'writeexcel'
3
+ require 'odf/spreadsheet'
4
+
5
+ class GenSheet
6
+
7
+ @roo
8
+
9
+ def to_xls(filename)
10
+ @outbook_xls = WriteExcel.new(filename)
11
+
12
+ workbook = @roo.instance_variable_get('@workbook')
13
+ formats = workbook.instance_variable_get('@formats')
14
+ worksheets = workbook.instance_variable_get('@worksheets')
15
+
16
+ # 出力シート準備、元シート分解
17
+ outsheets = Array.new
18
+ originalsheets = Array.new
19
+ names = Array.new
20
+ pre_sheet_xls(outsheets, originalsheets, names)
21
+
22
+ # mergeしてあるセルのセット
23
+ merges = Array.new
24
+ set_mergedcell_sheet(worksheets, outsheets, merges)
25
+
26
+ # シートのセット
27
+ set_sheet_xls(outsheets, originalsheets, names, merges)
28
+
29
+ # # 出力
30
+ # for i in 0..originalsheets.size - 1
31
+ # originalsheets[i].parse do |row|
32
+ # puts row
33
+ # end
34
+ # end
35
+
36
+ @outbook_xls.close
37
+ end
38
+
39
+ def pre_sheet_xls(outsheets, originalsheets, names)
40
+ @roo.each_with_pagename do |name, sheet|
41
+ outsheets << @outbook_xls.add_worksheet(name)
42
+ originalsheets << sheet.clone
43
+ names << name
44
+ end
45
+ end
46
+
47
+ def set_mergedcell_sheet(worksheets, outsheets, merges)
48
+ for i in 0..worksheets.size - 1
49
+ merges << worksheets[i].instance_variable_get('@merged_cells')
50
+ if merges[i] != nil && merges[i] != []
51
+ set_mergedcell(outsheets[i], merges[i])
52
+ end
53
+ end
54
+ end
55
+
56
+ def set_mergedcell(outsheet, merge)
57
+ format = @outbook_xls.add_format(:align => 'merge')
58
+ for j in 0..merge.size - 1
59
+ outsheet.merge_range(merge[j][0], merge[j][2], merge[j][1], merge[j][3], '', format)
60
+ end
61
+ end
62
+
63
+ def set_sheet_xls(outsheets, originalsheets, names, merges)
64
+ #@getformat = @formats[(x + 1) * (y + 1) - 1] # fontもborderも入ってるけどもインデックスがわからない
65
+ fonts = @roo.instance_variable_get('@fonts')
66
+ for i in 0..originalsheets.size - 1
67
+ originalsheets[i].each_with_index do |row, y|
68
+ row.each_with_index do |cell, x|
69
+ font = fonts[names[i]][[y + 1, x + 1]]
70
+ set_cell_xls(outsheets[i], cell, x, y, font, merges[i])
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ def set_cell_xls(outsheet, cell, x, y, font, merge)
77
+ # cellが空の場合は何もしない
78
+ return if cell == nil
79
+
80
+ # 各フォントフォーマットのセット
81
+ format = set_format(font)
82
+
83
+ # mergeしてあるセルの場合フォーマット追加
84
+ for i in 0..merge.size - 1
85
+ if y == merge[i][0] && x == merge[i][2]
86
+ set_mergedcell_format(format)
87
+ end
88
+ end
89
+
90
+ outsheet.write(y, x, cell, format)
91
+ end
92
+
93
+ def set_format(font)
94
+ format = @outbook_xls.add_format(
95
+ #:bottom => 1, #
96
+ #:top => 1, # border
97
+ #:left => 1, #
98
+ #:right => 1, #
99
+ :color => font.instance_variable_get('@color'),
100
+ :italic => font.instance_variable_get('@italic') ? 1 : 0,
101
+ :font => font.instance_variable_get('@name'),
102
+ :outline => font.instance_variable_get('@outline'),
103
+ :shadow => font.instance_variable_get('@shadow'),
104
+ :size => font.instance_variable_get('@size'),
105
+ :strikeout => font.instance_variable_get('@strikeout'),
106
+ :underline => font..instance_variable_get('@underline') == :none ? 0 : 1, # アンダーラインの種類は4つだけどとりあえず
107
+ :bold => font.instance_variable_get('@weight') > 400 ? 1 : 0 # weightが普通だと400、boldだと700になるようなのでとりあえず
108
+ #:encoding => font.encoding, #
109
+ #:escapement => font.escapement, # fontにまとめて入っていたけど
110
+ #:family => font.family, # どれに対応するのか..
111
+ #:previous_fast_key => font.previous_fast_key, #
112
+ )
113
+
114
+ return format
115
+ end
116
+
117
+ def set_mergedcell_format(format)
118
+ format.set_align('center')
119
+ format.set_valign('vcenter')
120
+ end
121
+
122
+ def to_ods(filename)
123
+ @outbook_ods = ODF::SpreadSheet.new
124
+
125
+ tables = Array.new
126
+ sheets = Array.new
127
+ pre_sheet_ods(tables, sheets)
128
+
129
+ @outbook_ods.write_to filename
130
+ end
131
+
132
+ def pre_sheet_ods(tables, sheets)
133
+ @roo.each_with_pagename do |name, sheet|
134
+ # シート作成
135
+ tables << (@outbook_ods.table name)
136
+ sheets << sheet.clone
137
+ end
138
+
139
+ set_sheet_ods(tables, sheets)
140
+ end
141
+
142
+ def set_sheet_ods(tables, sheets)
143
+ for i in 0..sheets.size - 1
144
+ sheets[i].each_with_index do |row, y|
145
+ # 行作成
146
+ ob_row = tables[i].row
147
+ set_cell_ods(row, ob_row)
148
+ end
149
+ end
150
+ end
151
+
152
+ def set_cell_ods(row, ob_row)
153
+ row.each_with_index do |cell, x|
154
+ # スタイル作成
155
+ @outbook_ods.style 'font-style', :family => :cell do
156
+ #property :text, 'font-weight' => 'bold', 'color' => '#ff0000'
157
+ end
158
+ # セル作成、スタイル適用
159
+ ob_cell = ob_row.cell(cell, :style => 'font-style')
160
+ end
161
+ end
162
+
163
+ def initialize(roo)
164
+ @roo = roo
165
+ end
166
+
167
+ end
Binary file
Binary file
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'open ods with roo' do
4
+ it 'opens and inspects an xls file' do
5
+ open_ods()
6
+ end
7
+ end
8
+
9
+ describe '#new' do
10
+ it 'creates a new GenSheet object from Roo ODS' do
11
+ create_gens_from_ods()
12
+ end
13
+ end
14
+
15
+ describe '#to_ods' do
16
+ it 'creates an ods file' do
17
+ create_gens_from_ods()
18
+ @gens.to_ods('./spec/files/ods_out.ods')
19
+ end
20
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#to_xls' do
4
+ it 'creates an xls file from an ods base' do
5
+ create_gens_from_ods()
6
+ @gens.to_xls('./spec/files/ods_out.xls')
7
+ end
8
+ end
@@ -0,0 +1,26 @@
1
+ require 'roo'
2
+ require_relative '../lib/GenSheet.rb'
3
+
4
+ @xls
5
+ @ods
6
+ @gens
7
+
8
+ def open_xls()
9
+ @xls = Roo::Spreadsheet.open('./spec/files/template.xls')
10
+ @xls.inspect
11
+ end
12
+
13
+ def open_ods()
14
+ @ods = Roo::Spreadsheet.open('./spec/files/template.ods')
15
+ @ods.inspect
16
+ end
17
+
18
+ def create_gens_from_xls()
19
+ open_xls()
20
+ @gens = GenSheet.new(@xls)
21
+ end
22
+
23
+ def create_gens_from_ods()
24
+ open_ods()
25
+ @gens = GenSheet.new(@ods)
26
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#to_ods' do
4
+ it 'creates an ods file from an xls base' do
5
+ create_gens_from_xls()
6
+ @gens.to_ods('./spec/files/xls_out.ods')
7
+ end
8
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'open xls with roo' do
4
+ it 'opens and inspects an xls file' do
5
+ open_xls()
6
+ end
7
+ end
8
+
9
+ describe '#new' do
10
+ it 'creates a new GenSheet object from Roo XLS' do
11
+ create_gens_from_xls()
12
+ end
13
+ end
14
+
15
+ describe '#to_xls' do
16
+ it 'creates an xls file' do
17
+ create_gens_from_xls()
18
+ @gens.to_xls('./spec/files/xls_out.xls')
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gen_sheet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Rei Kagetsuki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Takes a Roo spreadsheet object and renders it in XLS or ODS. Also allows
14
+ you to use a template sheet and fill it with data from Roo, then output a rendered
15
+ sheet.
16
+ email: zero@genshin.org
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - README.md
24
+ - Rakefile
25
+ - gen_sheet.gemspec
26
+ - lib/GenSheet.rb
27
+ - spec/files/template.ods
28
+ - spec/files/template.xls
29
+ - spec/ods_ods_spec.rb
30
+ - spec/ods_xls_spec.rb
31
+ - spec/spec_helper.rb
32
+ - spec/xls_ods_spec.rb
33
+ - spec/xls_xls_spec.rb
34
+ homepage: https://github.com/Genshin/GenSheet
35
+ licenses: []
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 2.0.0.rc.2
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: A spreadsheet generator (ODS, XLS) for Roo.
57
+ test_files: []