listen360-rubyXL 1.2.10.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +16 -0
- data/Gemfile.lock +34 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +197 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/.DS_Store +0 -0
- data/lib/rubyXL.rb +10 -0
- data/lib/rubyXL/Hash.rb +60 -0
- data/lib/rubyXL/cell.rb +461 -0
- data/lib/rubyXL/color.rb +14 -0
- data/lib/rubyXL/parser.rb +468 -0
- data/lib/rubyXL/private_class.rb +265 -0
- data/lib/rubyXL/workbook.rb +450 -0
- data/lib/rubyXL/worksheet.rb +1493 -0
- data/lib/rubyXL/writer/app_writer.rb +62 -0
- data/lib/rubyXL/writer/calc_chain_writer.rb +33 -0
- data/lib/rubyXL/writer/content_types_writer.rb +77 -0
- data/lib/rubyXL/writer/core_writer.rb +51 -0
- data/lib/rubyXL/writer/root_rels_writer.rb +25 -0
- data/lib/rubyXL/writer/shared_strings_writer.rb +30 -0
- data/lib/rubyXL/writer/styles_writer.rb +407 -0
- data/lib/rubyXL/writer/theme_writer.rb +343 -0
- data/lib/rubyXL/writer/workbook_rels_writer.rb +59 -0
- data/lib/rubyXL/writer/workbook_writer.rb +77 -0
- data/lib/rubyXL/writer/worksheet_writer.rb +230 -0
- data/lib/rubyXL/zip.rb +20 -0
- data/rubyXL.gemspec +92 -0
- data/spec/lib/cell_spec.rb +385 -0
- data/spec/lib/color_spec.rb +14 -0
- data/spec/lib/hash_spec.rb +28 -0
- data/spec/lib/parser_spec.rb +66 -0
- data/spec/lib/workbook_spec.rb +51 -0
- data/spec/lib/worksheet_spec.rb +1782 -0
- metadata +207 -0
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "shoulda", ">= 0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.6.0"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
gem "nokogiri", ">= 1.4.4"
|
14
|
+
gem "rubyzip", ">= 0.9.4"
|
15
|
+
gem "rspec", ">= 1.3.4"
|
16
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.6.0)
|
7
|
+
bundler (~> 1.0.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
nokogiri (1.4.4)
|
11
|
+
rake (0.8.7)
|
12
|
+
rcov (0.9.9)
|
13
|
+
rspec (2.6.0)
|
14
|
+
rspec-core (~> 2.6.0)
|
15
|
+
rspec-expectations (~> 2.6.0)
|
16
|
+
rspec-mocks (~> 2.6.0)
|
17
|
+
rspec-core (2.6.4)
|
18
|
+
rspec-expectations (2.6.0)
|
19
|
+
diff-lcs (~> 1.1.2)
|
20
|
+
rspec-mocks (2.6.0)
|
21
|
+
rubyzip (0.9.4)
|
22
|
+
shoulda (2.11.3)
|
23
|
+
|
24
|
+
PLATFORMS
|
25
|
+
ruby
|
26
|
+
|
27
|
+
DEPENDENCIES
|
28
|
+
bundler (~> 1.0.0)
|
29
|
+
jeweler (~> 1.6.0)
|
30
|
+
nokogiri (>= 1.4.4)
|
31
|
+
rcov
|
32
|
+
rspec (>= 1.3.4)
|
33
|
+
rubyzip (>= 0.9.4)
|
34
|
+
shoulda
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Vivek Bhagwat
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,197 @@
|
|
1
|
+
= rubyXL
|
2
|
+
|
3
|
+
== To Install:
|
4
|
+
gem install rubyXL
|
5
|
+
|
6
|
+
== To Use:
|
7
|
+
require 'rubyXL' #assuming rubygems is already required
|
8
|
+
|
9
|
+
=== Parsing an existing workbook
|
10
|
+
workbook = RubyXL::Parser.parse("path/to/Excel/file.xlsx")
|
11
|
+
|
12
|
+
|
13
|
+
=== Creating a new Workbook
|
14
|
+
workbook = RubyXL::Workbook.new
|
15
|
+
|
16
|
+
=== Accessing
|
17
|
+
|
18
|
+
==== Accessing a Worksheet
|
19
|
+
workbook.worksheets[0] #returns first worksheet
|
20
|
+
workbook[0] #returns first worksheet
|
21
|
+
|
22
|
+
==== Accessing Only the Values
|
23
|
+
workbook.worksheets[0].extract_data #produces a 2d array which consists only of the values (instead of the Cell objects which include other variables)
|
24
|
+
|
25
|
+
==== Accessing a Row (Array of Cells)
|
26
|
+
workbook[0].sheet_data[0] #returns first row in first worksheet
|
27
|
+
workbook[0][0] #returns first row in first worksheet
|
28
|
+
|
29
|
+
==== Accessing a Cell
|
30
|
+
workbook[0].sheet_data[0][0] #returns A1 in first worksheet
|
31
|
+
workbook[0][0][0] #returns A1 in first worksheet
|
32
|
+
|
33
|
+
==== Accessing Cell properties
|
34
|
+
workbook[0][0][0].is_struckthrough() #returns if A1 is struckthrough, other boolean properties have same syntax
|
35
|
+
workbook[0][0][0].font_name #returns font name for A1
|
36
|
+
workbook[0][0][0].font_size #returns font size for A1
|
37
|
+
workbook[0][0][0].font_color #returns font color for A1
|
38
|
+
workbook[0][0][0].fill_color #returns fill color for A1
|
39
|
+
workbook[0][0][0].horizontal_alignment #returns horizontal alignment for A1 (or nil if it does not exist)
|
40
|
+
workbook[0][0][0].vertical_alignment #returns vertical alignment for A1 (or nil if it does not exist)
|
41
|
+
workbook[0][0][0].border_top #returns type of border on top of A1 (nil if none exists), other directions have same syntax
|
42
|
+
|
43
|
+
==== Accessing row properties
|
44
|
+
workbook[0].get_row_fill(0) #returns fill color for first row
|
45
|
+
workbook[0].get_row_font_name(0) #returns font name for first row
|
46
|
+
workbook[0].get_row_font_size(0) #returns font size for first row
|
47
|
+
workbook[0].get_row_font_color(0) #returns font color for first row
|
48
|
+
workbook[0].is_row_underlined(0) #returns if first row is italicized, other boolean properties have same syntax
|
49
|
+
workbook[0].get_row_height(0) #returns height of first row
|
50
|
+
workbook[0].get_row_horizontal_alignment(0) #returns horizontal alignment of first row (nil if none exists)
|
51
|
+
workbook[0].get_row_vertical_alignment(0) #returns vertical alignment of first row (nil if none exists)
|
52
|
+
workbook[0].get_row_border_right(0) #returns weight of right border of first row (nil if none exists), other directions have the same syntax
|
53
|
+
|
54
|
+
==== Accessing column properties
|
55
|
+
workbook[0].get_column_fill(0) #returns fill color for first column
|
56
|
+
workbook[0].get_column_font_name(0) #returns font name for first column
|
57
|
+
workbook[0].get_column_font_size(0) #returns font size for first column
|
58
|
+
workbook[0].get_column_font_color(0) #returns font color for first column
|
59
|
+
workbook[0].is_column_underlined(0) #returns if first column is italicized, other boolean properties have same syntax
|
60
|
+
workbook[0].get_column_height(0) #returns height of first column
|
61
|
+
workbook[0].get_column_horizontal_alignment(0) #returns horizontal alignment of first column (nil if none exists)
|
62
|
+
workbook[0].get_column_vertical_alignment(0) #returns vertical alignment of first column (nil if none exists)
|
63
|
+
workbook[0].get_column_border_right(0) #returns weight of right border of first column (nil if none exists), other directions have the same syntax
|
64
|
+
|
65
|
+
==== Table identification
|
66
|
+
workbook[0].get_table(["NAME", "AGE", "HEIGHT"]) #returns hash of a table in the first worksheet, with the specified strings as headers, accessible by row and column
|
67
|
+
#it returns the following structure
|
68
|
+
{
|
69
|
+
:Name=>["John", "Jane", "Joe"],
|
70
|
+
:Height=>[70, 65, 68],
|
71
|
+
:Age=>[30, 25, 35]
|
72
|
+
:table=>[
|
73
|
+
{:Name=>"John", :Height=>70, :Age=>30},
|
74
|
+
{:Name=>"Jane", :Height=>65, :Age=>25},
|
75
|
+
{:Name=>"Joe", :Height=>68, :Age=>35}
|
76
|
+
]
|
77
|
+
}
|
78
|
+
|
79
|
+
=== Modifying
|
80
|
+
|
81
|
+
==== Adding Worksheets
|
82
|
+
workbook.worksheets << Worksheet.new('Sheet2')
|
83
|
+
|
84
|
+
==== Adding Cells
|
85
|
+
workbook.worksheets[0].add_cell(0,0,'A1') #sets A1 to string "A1"
|
86
|
+
workbook.worksheets[0].add_cell(0,1,'','A1') #sets B1 to value of A1
|
87
|
+
|
88
|
+
workbook.worksheets[0].add_cell_obj(Cell.new(1,0,'blah')) #sets A2 to 'blah'
|
89
|
+
|
90
|
+
==== Changing Cells
|
91
|
+
workbook.worksheets[0][0][0].change_contents("", workbook.worksheets[0][0][0].formula) #sets A1 to empty string, preserves formula
|
92
|
+
|
93
|
+
==== Changing Fonts
|
94
|
+
workbook.worksheets[0].sheet_data[0][0].change_font_bold(true) #sets A1 to bold
|
95
|
+
workbook.worksheets[0].change_row_font_italics(0,true) #makes first row italicized
|
96
|
+
workbook.worksheets[0].change_column_font_name(0,'Courier') #makes first column have font Courier
|
97
|
+
|
98
|
+
==== Changing Fills
|
99
|
+
workbook.worksheets[0].sheet_data[0][0].change_fill('0ba53d') #sets A1 to have fill #0ba53d
|
100
|
+
workbook.worksheets[0].change_row_fill(0, '0ba53d') #sets first row to have fill #0ba53d
|
101
|
+
workbook.worksheets[0].change_column_fill(0, '0ba53d') #sets first column to have fill #0ba53d
|
102
|
+
|
103
|
+
==== Changing Borders
|
104
|
+
# Possible weights: hairline, thin, medium, thick
|
105
|
+
# Possible "directions": top, bottom, left, right, diagonal
|
106
|
+
workbook.worksheets[0].sheet_data[0][0].change_border_top('thin') #sets A1 to have a top, thin border
|
107
|
+
workbook.worksheets[0].change_row_border_left(0, 'hairline') #sets first row to have a left, hairline border
|
108
|
+
workbook.worksheets[0].change_column_border_diagonal(0, 'medium') #sets first column to have diagonal, medium border
|
109
|
+
|
110
|
+
==== Changing Alignment
|
111
|
+
===== Horizontal
|
112
|
+
center, distributed, justify, left, right
|
113
|
+
workbook.worksheets[0].sheet_data[0][0].change_horizontal_alignment('center') #sets A1 to be centered
|
114
|
+
workbook.worksheets[0].change_row_horizontal_alignment(0,'justify') #sets first row to be justified
|
115
|
+
workbook.worksheets[0].change_column_horizontal_alignment(0,'right'), #sets first column to be right-aligned
|
116
|
+
|
117
|
+
===== Vertical
|
118
|
+
bottom, center, distributed, top
|
119
|
+
workbook.worksheets[0].sheet_data[0][0].change_vertical_alignment('bottom') #sets A1 to be bottom aligned
|
120
|
+
workbook.worksheets[0].change_row_vertical_alignment(0,'distributed') #sets first row to be distributed vertically
|
121
|
+
workbook.worksheets[0].change_column_vertical_alignment(0,'top') #sets first column to be top aligned
|
122
|
+
|
123
|
+
==== Changing Row Height
|
124
|
+
workbook.worksheets[0].change_row_height(0,30) #sets first row to be of height 30
|
125
|
+
|
126
|
+
==== Changing Column Width
|
127
|
+
workbook.worksheets[0].change_column_width(0,30) #sets first column to be of width 30
|
128
|
+
|
129
|
+
==== Merging Cells
|
130
|
+
workbook.worksheets[0].merge_cells(0,0,1,1) #merges A1:B2
|
131
|
+
|
132
|
+
==== Insert Row
|
133
|
+
This method will insert a row at specified index, pushing all rows below it down. It also copies styles from row above.
|
134
|
+
|
135
|
+
WARNING: Use of this method WILL break formulas referencing cells which have been moved, as the formulas do not adapt to the shifted rows
|
136
|
+
workbook.worksheets[0].insert_row(1)
|
137
|
+
|
138
|
+
==== Insert Column
|
139
|
+
This method will insert a column at specified index, pushing all columns to the right of it one to the right. It also copies styles from column to the left
|
140
|
+
|
141
|
+
WARNING: Use of this method WILL break formulas referencing cells which have been moved, as the formulas do not adapt to the shifted columns
|
142
|
+
workbook.worksheets[0].insert_column(1)
|
143
|
+
|
144
|
+
==== Delete Row
|
145
|
+
This method will delete a row at specified index, pushing all rows below it up.
|
146
|
+
|
147
|
+
WARNING: Use of this method WILL break formulas referencing cells which have been moved, as the formulas do not adapt to the shifted rows
|
148
|
+
workbook.worksheets[0].delete_row(1)
|
149
|
+
|
150
|
+
==== Delete Column
|
151
|
+
This method will delete a column at specified index, pushing all columns to the right of it left.
|
152
|
+
|
153
|
+
WARNING: Use of this method WILL break formulas referencing cells which have been moved, as the formulas do not adapt to the shifted columns
|
154
|
+
workbook.worksheets[0].delete_column(1)
|
155
|
+
|
156
|
+
==== Insert Cell
|
157
|
+
This method will insert a cell at specified position. It takes a :right or :down option, to shift cells either left or down upon inserting (nil means replacing the cell)
|
158
|
+
|
159
|
+
WARNING: Use of this method WILL break formulas referencing cells which have been moved, as the formulas do not adapt to the shifted cells
|
160
|
+
workbook.worksheets[0].insert_cell(0,0,"blah",formula=nil,:right) #inserts cell at A1, shifts cells in first row right
|
161
|
+
workbook.worksheets[0].insert_cell(0,0,"blah",formula=nil,:down) #inserts cell at A1, shifts cells in first column down
|
162
|
+
workbook.worksheets[0].insert_cell(0,0,"blah") #inserts cell at A1, shifts nothing
|
163
|
+
|
164
|
+
==== Delete Cell
|
165
|
+
This method will delete a cell at specified position. It takes a :left or :up option, to shift cells either up or left upon deletion (nil means simply deleting the cell contents)
|
166
|
+
|
167
|
+
WARNING: Use of this method WILL break formulas referencing cells which have been moved, as the formulas do not adapt to the shifted cells
|
168
|
+
workbook.worksheets[0].delete_cell(0,0,:left) #deletes A1, shifts contents of first row left
|
169
|
+
workbook.worksheets[0].delete_cell(0,0,:up) #deletes A1, shifts contents of first column up
|
170
|
+
workbook.worksheets[0].delete_cell(0,0) #deletes A1, does not shift cells
|
171
|
+
|
172
|
+
=== Writing
|
173
|
+
workbook.write("path/to/desired/Excel/file.xlsx")
|
174
|
+
|
175
|
+
|
176
|
+
=== Miscellaneous
|
177
|
+
Cell.convert_to_cell(0,0) == 'A1' #converts row and column index to Excel-style index
|
178
|
+
Parser.convert_to_index('A1') == [0,0] #converts Excel-style index to row and column index
|
179
|
+
|
180
|
+
== For more information
|
181
|
+
Take a look at the files in spec/lib/ for rspecs on most methods
|
182
|
+
|
183
|
+
== Contributing to rubyXL
|
184
|
+
|
185
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
186
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
187
|
+
* Fork the project
|
188
|
+
* Start a feature/bugfix branch
|
189
|
+
* Commit and push until you are happy with your contribution
|
190
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
191
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
192
|
+
|
193
|
+
== Copyright
|
194
|
+
|
195
|
+
Copyright (c) 2011 Vivek Bhagwat. See LICENSE.txt for
|
196
|
+
further details.
|
197
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "rubyXL"
|
18
|
+
gem.homepage = "http://github.com/gilt/rubyXL"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{rubyXL is a gem which allows the parsing, creation, and manipulation of Microsoft Excel (.xlsx/.xlsm) Documents}
|
21
|
+
gem.description = %Q{rubyXL is a gem which allows the parsing, creation, and manipulation of Microsoft Excel (.xlsx/.xlsm) Documents}
|
22
|
+
gem.email = "bhagwat.vivek@gmail.com"
|
23
|
+
gem.authors = ["Vivek Bhagwat"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
test.rcov_opts << '--exclude "gems/*"'
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "rubyXL #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.2.10
|
data/lib/.DS_Store
ADDED
Binary file
|
data/lib/rubyXL.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
$: << File.expand_path(File.dirname(__FILE__))
|
2
|
+
require File.expand_path(File.join(File.dirname(__FILE__),'rubyXL','workbook'))
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__),'rubyXL','private_class'))
|
4
|
+
require File.expand_path(File.join(File.dirname(__FILE__),'rubyXL','worksheet'))
|
5
|
+
require File.expand_path(File.join(File.dirname(__FILE__),'rubyXL','cell'))
|
6
|
+
require File.expand_path(File.join(File.dirname(__FILE__),'rubyXL','parser'))
|
7
|
+
require File.expand_path(File.join(File.dirname(__FILE__),'rubyXL','color'))
|
8
|
+
|
9
|
+
module RubyXL
|
10
|
+
end
|
data/lib/rubyXL/Hash.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'nokogiri'
|
3
|
+
# modified from http://stackoverflow.com/questions/1230741/convert-a-nokogiri-document-to-a-ruby-hash/1231297#1231297
|
4
|
+
|
5
|
+
module RubyXL
|
6
|
+
# class Hash
|
7
|
+
class Hash < ::Hash
|
8
|
+
def self.from_xml(xml_io)
|
9
|
+
begin
|
10
|
+
result = Nokogiri::XML(xml_io)
|
11
|
+
return { result.root.name.to_sym => xml_node_to_hash(result.root)}
|
12
|
+
rescue Exception => e
|
13
|
+
# raise your custom exception here
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.xml_node_to_hash(node)
|
18
|
+
# If we are at the root of the document, start the hash
|
19
|
+
if node.element?
|
20
|
+
result_hash = {}
|
21
|
+
if node.attributes != {}
|
22
|
+
result_hash[:attributes] = {}
|
23
|
+
node.attributes.keys.each do |key|
|
24
|
+
result_hash[:attributes][node.attributes[key].name.to_sym] = prepare(node.attributes[key].value)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
if node.children.size > 0
|
28
|
+
node.children.each do |child|
|
29
|
+
result = xml_node_to_hash(child)
|
30
|
+
|
31
|
+
if child.name == "text"
|
32
|
+
unless child.next_sibling || child.previous_sibling
|
33
|
+
return prepare(result)
|
34
|
+
end
|
35
|
+
elsif result_hash[child.name.to_sym]
|
36
|
+
if result_hash[child.name.to_sym].is_a?(Object::Array)
|
37
|
+
result_hash[child.name.to_sym] << prepare(result)
|
38
|
+
else
|
39
|
+
result_hash[child.name.to_sym] = [result_hash[child.name.to_sym]] << prepare(result)
|
40
|
+
end
|
41
|
+
else
|
42
|
+
result_hash[child.name.to_sym] = prepare(result)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
return result_hash
|
47
|
+
else
|
48
|
+
return result_hash
|
49
|
+
end
|
50
|
+
else
|
51
|
+
return prepare(node.content.to_s)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.prepare(data)
|
56
|
+
(data.class == String && data.to_i.to_s == data) ? data.to_i : data
|
57
|
+
end
|
58
|
+
end
|
59
|
+
# end
|
60
|
+
end
|
data/lib/rubyXL/cell.rb
ADDED
@@ -0,0 +1,461 @@
|
|
1
|
+
module RubyXL
|
2
|
+
class Cell < PrivateClass
|
3
|
+
|
4
|
+
attr_accessor :row, :column, :datatype, :style_index, :formula, :worksheet
|
5
|
+
attr_reader :workbook,:formula_attributes
|
6
|
+
|
7
|
+
def initialize(worksheet,row,column,value=nil,formula=nil,datatype='s',style_index=0, fmla_attr={})
|
8
|
+
@worksheet = worksheet
|
9
|
+
|
10
|
+
@workbook = worksheet.workbook
|
11
|
+
@row = row
|
12
|
+
@column = column
|
13
|
+
@datatype = datatype
|
14
|
+
@value = value
|
15
|
+
@formula=formula
|
16
|
+
@style_index = style_index
|
17
|
+
@formula_attributes = fmla_attr
|
18
|
+
end
|
19
|
+
|
20
|
+
def value(args = {})
|
21
|
+
raw_values = args.delete(:raw) || false
|
22
|
+
return @value if raw_values
|
23
|
+
|
24
|
+
if is_date?
|
25
|
+
return @workbook.num_to_date(@value)
|
26
|
+
else
|
27
|
+
return @value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def is_date?
|
32
|
+
if !@value.is_a?(String)
|
33
|
+
if @workbook.num_fmts_by_id
|
34
|
+
num_fmt_id = xf_id()[:numFmtId]
|
35
|
+
tmp_num_fmt = @workbook.num_fmts_by_id[num_fmt_id]
|
36
|
+
num_fmt = (tmp_num_fmt &&tmp_num_fmt[:attributes] && tmp_num_fmt[:attributes][:formatCode]) ? tmp_num_fmt[:attributes][:formatCode] : nil
|
37
|
+
if num_fmt && workbook.date_num_fmt?(num_fmt)
|
38
|
+
return true
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
return false
|
43
|
+
end
|
44
|
+
|
45
|
+
def is_date_format?(num_fmt)
|
46
|
+
skip_chars = ['$', '-', '+', '/', '(', ')', ':', ' ']
|
47
|
+
num_chars = ['0', '#', '?']
|
48
|
+
non_date_formats = ['0.00E+00', '##0.0E+0', 'General', 'GENERAL', 'general', '@']
|
49
|
+
date_chars = ['y','m','d','h','s']
|
50
|
+
|
51
|
+
state = 0
|
52
|
+
s = ''
|
53
|
+
num_fmt.split(//).each do |c|
|
54
|
+
if state == 0
|
55
|
+
if c == '"'
|
56
|
+
state = 1
|
57
|
+
elsif ['\\', '_', '*'].include?(c)
|
58
|
+
state = 2
|
59
|
+
elsif skip_chars.include?(c)
|
60
|
+
next
|
61
|
+
else
|
62
|
+
s << c
|
63
|
+
end
|
64
|
+
elsif state == 1
|
65
|
+
if c == '"'
|
66
|
+
state = 0
|
67
|
+
end
|
68
|
+
elsif state == 2
|
69
|
+
state = 0
|
70
|
+
end
|
71
|
+
end
|
72
|
+
s.gsub!(/\[[^\]]*\]/, '')
|
73
|
+
if non_date_formats.include?(s)
|
74
|
+
return false
|
75
|
+
end
|
76
|
+
separator = ';'
|
77
|
+
got_sep = 0
|
78
|
+
date_count = 0
|
79
|
+
num_count = 0
|
80
|
+
s.split(//).each do |c|
|
81
|
+
if date_chars.include?(c)
|
82
|
+
date_count += 1
|
83
|
+
elsif num_chars.include?(c)
|
84
|
+
num_count += 1
|
85
|
+
elsif c == separator
|
86
|
+
got_sep = 1
|
87
|
+
end
|
88
|
+
end
|
89
|
+
if date_count > 0 && num_count == 0
|
90
|
+
return true
|
91
|
+
elsif num_count > 0 && date_count == 0
|
92
|
+
return false
|
93
|
+
elsif date_count
|
94
|
+
# ambiguous result
|
95
|
+
elsif got_sep == 0
|
96
|
+
# constant result
|
97
|
+
end
|
98
|
+
return date_count > num_count
|
99
|
+
end
|
100
|
+
|
101
|
+
# changes fill color of cell
|
102
|
+
def change_fill(rgb='ffffff')
|
103
|
+
validate_worksheet
|
104
|
+
Color.validate_color(rgb)
|
105
|
+
@style_index = modify_fill(@workbook, @style_index,rgb)
|
106
|
+
end
|
107
|
+
|
108
|
+
# Changes font name of cell
|
109
|
+
def change_font_name(font_name='Verdana')
|
110
|
+
validate_worksheet
|
111
|
+
# Get copy of font object with modified name
|
112
|
+
font = deep_copy(workbook.fonts[font_id().to_s][:font])
|
113
|
+
font[:name][:attributes][:val] = font_name.to_s
|
114
|
+
# Update font and xf array
|
115
|
+
change_font(font)
|
116
|
+
end
|
117
|
+
|
118
|
+
# Changes font size of cell
|
119
|
+
def change_font_size(font_size=10)
|
120
|
+
validate_worksheet
|
121
|
+
if font_size.is_a?(Integer) || font_size.is_a?(Float)
|
122
|
+
# Get copy of font object with modified size
|
123
|
+
font = deep_copy(workbook.fonts[font_id().to_s][:font])
|
124
|
+
font[:sz][:attributes][:val] = font_size
|
125
|
+
# Update font and xf array
|
126
|
+
change_font(font)
|
127
|
+
else
|
128
|
+
raise 'Argument must be a number'
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
# Changes font color of cell
|
133
|
+
def change_font_color(font_color='000000')
|
134
|
+
validate_worksheet
|
135
|
+
#if arg is a color name, convert to integer
|
136
|
+
Color.validate_color(font_color)
|
137
|
+
# Get copy of font object with modified color
|
138
|
+
font = deep_copy(workbook.fonts[font_id().to_s][:font])
|
139
|
+
font = modify_font_color(font, font_color.to_s)
|
140
|
+
# Update font and xf array
|
141
|
+
change_font(font)
|
142
|
+
end
|
143
|
+
|
144
|
+
# Changes font italics settings of cell
|
145
|
+
def change_font_italics(italicized=false)
|
146
|
+
validate_worksheet
|
147
|
+
# Get copy of font object with modified italics settings
|
148
|
+
font = deep_copy(workbook.fonts[font_id().to_s][:font])
|
149
|
+
font = modify_font_italics(font, italicized)
|
150
|
+
# Update font and xf array
|
151
|
+
change_font(font)
|
152
|
+
end
|
153
|
+
|
154
|
+
# Changes font bold settings of cell
|
155
|
+
def change_font_bold(bolded=false)
|
156
|
+
validate_worksheet
|
157
|
+
# Get copy of font object with modified bold settings
|
158
|
+
font = deep_copy(workbook.fonts[font_id().to_s][:font])
|
159
|
+
font = modify_font_bold(font, bolded)
|
160
|
+
# Update font and xf array
|
161
|
+
change_font(font)
|
162
|
+
end
|
163
|
+
|
164
|
+
# Changes font underline settings of cell
|
165
|
+
def change_font_underline(underlined=false)
|
166
|
+
validate_worksheet
|
167
|
+
# Get copy of font object with modified underline settings
|
168
|
+
font = deep_copy(workbook.fonts[font_id().to_s][:font])
|
169
|
+
font = modify_font_underline(font, underlined)
|
170
|
+
# Update font and xf array
|
171
|
+
change_font(font)
|
172
|
+
end
|
173
|
+
|
174
|
+
# Changes font strikethrough settings of cell
|
175
|
+
def change_font_strikethrough(struckthrough=false)
|
176
|
+
validate_worksheet
|
177
|
+
# Get copy of font object with modified strikethrough settings
|
178
|
+
font = deep_copy(workbook.fonts[font_id().to_s][:font])
|
179
|
+
font = modify_font_strikethrough(font, struckthrough)
|
180
|
+
# Update font and xf array
|
181
|
+
change_font(font)
|
182
|
+
end
|
183
|
+
|
184
|
+
# Helper method to update the font array and xf array
|
185
|
+
def change_font(font)
|
186
|
+
# Modify font array and retrieve new font id
|
187
|
+
font_id = modify_font(@workbook, font, font_id())
|
188
|
+
# Get copy of xf object with modified font id
|
189
|
+
xf = deep_copy(xf_id())
|
190
|
+
xf[:fontId] = Integer(font_id.to_i)
|
191
|
+
# Modify xf array and retrieve new xf id
|
192
|
+
@style_index = modify_xf(@workbook, xf)
|
193
|
+
end
|
194
|
+
|
195
|
+
# changes horizontal alignment of cell
|
196
|
+
def change_horizontal_alignment(alignment='center')
|
197
|
+
validate_worksheet
|
198
|
+
validate_horizontal_alignment(alignment)
|
199
|
+
@style_index = modify_alignment(@workbook,@style_index,true,alignment)
|
200
|
+
end
|
201
|
+
|
202
|
+
# changes vertical alignment of cell
|
203
|
+
def change_vertical_alignment(alignment='center')
|
204
|
+
validate_worksheet
|
205
|
+
validate_vertical_alignment(alignment)
|
206
|
+
@style_index = modify_alignment(@workbook,@style_index,false,alignment)
|
207
|
+
end
|
208
|
+
|
209
|
+
# changes top border of cell
|
210
|
+
def change_border_top(weight='thin')
|
211
|
+
change_border(:top, weight)
|
212
|
+
end
|
213
|
+
|
214
|
+
# changes left border of cell
|
215
|
+
def change_border_left(weight='thin')
|
216
|
+
change_border(:left, weight)
|
217
|
+
end
|
218
|
+
|
219
|
+
# changes right border of cell
|
220
|
+
def change_border_right(weight='thin')
|
221
|
+
change_border(:right, weight)
|
222
|
+
end
|
223
|
+
|
224
|
+
# changes bottom border of cell
|
225
|
+
def change_border_bottom(weight='thin')
|
226
|
+
change_border(:bottom, weight)
|
227
|
+
end
|
228
|
+
|
229
|
+
# changes diagonal border of cell
|
230
|
+
def change_border_diagonal(weight='thin')
|
231
|
+
change_border(:diagonal, weight)
|
232
|
+
end
|
233
|
+
|
234
|
+
# changes contents of cell, with formula option
|
235
|
+
def change_contents(data, formula=nil)
|
236
|
+
validate_worksheet
|
237
|
+
@datatype='str'
|
238
|
+
if data.is_a?(Date) || data.is_a?(DateTime)
|
239
|
+
data = @workbook.date_to_num(data)
|
240
|
+
end
|
241
|
+
if (data.is_a?Integer) || (data.is_a?Float)
|
242
|
+
@datatype = ''
|
243
|
+
end
|
244
|
+
@value=data
|
245
|
+
@formula=formula
|
246
|
+
end
|
247
|
+
|
248
|
+
# returns if font is italicized
|
249
|
+
def is_italicized()
|
250
|
+
validate_worksheet
|
251
|
+
if @workbook.fonts[font_id()][:font][:i].nil?
|
252
|
+
false
|
253
|
+
else
|
254
|
+
true
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
# returns if font is bolded
|
259
|
+
def is_bolded()
|
260
|
+
validate_worksheet
|
261
|
+
if @workbook.fonts[font_id()][:font][:b].nil?
|
262
|
+
false
|
263
|
+
else
|
264
|
+
true
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
# returns if font is underlined
|
269
|
+
def is_underlined()
|
270
|
+
validate_worksheet
|
271
|
+
xf = @workbook.get_style_attributes(@workbook.get_style(@style_index))
|
272
|
+
if @workbook.fonts[font_id()][:font][:u].nil?
|
273
|
+
false
|
274
|
+
else
|
275
|
+
true
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
# returns if font has a strike through it
|
280
|
+
def is_struckthrough()
|
281
|
+
validate_worksheet
|
282
|
+
xf = @workbook.get_style_attributes(@workbook.get_style(@style_index))
|
283
|
+
if @workbook.fonts[font_id()][:font][:strike].nil?
|
284
|
+
false
|
285
|
+
else
|
286
|
+
true
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
# returns cell's font name
|
291
|
+
def font_name()
|
292
|
+
validate_worksheet
|
293
|
+
@workbook.fonts[font_id()][:font][:name][:attributes][:val]
|
294
|
+
end
|
295
|
+
|
296
|
+
# returns cell's font size
|
297
|
+
def font_size()
|
298
|
+
validate_worksheet
|
299
|
+
return @workbook.fonts[font_id()][:font][:sz][:attributes][:val]
|
300
|
+
end
|
301
|
+
|
302
|
+
# returns cell's font color
|
303
|
+
def font_color()
|
304
|
+
validate_worksheet
|
305
|
+
if @workbook.fonts[font_id()][:font][:color].nil?
|
306
|
+
'000000' #black
|
307
|
+
else
|
308
|
+
@workbook.fonts[font_id()][:font][:color][:attributes][:rgb]
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
# returns cell's fill color
|
313
|
+
def fill_color()
|
314
|
+
validate_worksheet
|
315
|
+
xf = @workbook.get_style_attributes(@workbook.get_style(@style_index))
|
316
|
+
return @workbook.get_fill_color(xf)
|
317
|
+
end
|
318
|
+
|
319
|
+
# returns cell's horizontal alignment
|
320
|
+
def horizontal_alignment()
|
321
|
+
validate_worksheet
|
322
|
+
xf_obj = @workbook.get_style(@style_index)
|
323
|
+
if xf_obj[:alignment].nil? || xf_obj[:alignment][:attributes].nil?
|
324
|
+
return nil
|
325
|
+
end
|
326
|
+
xf_obj[:alignment][:attributes][:horizontal].to_s
|
327
|
+
end
|
328
|
+
|
329
|
+
# returns cell's vertical alignment
|
330
|
+
def vertical_alignment()
|
331
|
+
validate_worksheet
|
332
|
+
xf_obj = @workbook.get_style(@style_index)
|
333
|
+
if xf_obj[:alignment].nil? || xf_obj[:alignment][:attributes].nil?
|
334
|
+
return nil
|
335
|
+
end
|
336
|
+
xf_obj[:alignment][:attributes][:vertical].to_s
|
337
|
+
end
|
338
|
+
|
339
|
+
# returns cell's top border
|
340
|
+
def border_top()
|
341
|
+
return get_border(:top)
|
342
|
+
end
|
343
|
+
|
344
|
+
# returns cell's left border
|
345
|
+
def border_left()
|
346
|
+
return get_border(:left)
|
347
|
+
end
|
348
|
+
|
349
|
+
# returns cell's right border
|
350
|
+
def border_right()
|
351
|
+
return get_border(:right)
|
352
|
+
end
|
353
|
+
|
354
|
+
# returns cell's bottom border
|
355
|
+
def border_bottom()
|
356
|
+
return get_border(:bottom)
|
357
|
+
end
|
358
|
+
|
359
|
+
# returns cell's diagonal border
|
360
|
+
def border_diagonal()
|
361
|
+
return get_border(:diagonal)
|
362
|
+
end
|
363
|
+
|
364
|
+
# returns Excel-style cell string from matrix indices
|
365
|
+
def Cell.convert_to_cell(row=0,col=0)
|
366
|
+
row_string = (row + 1).to_s #+1 for 0 indexing
|
367
|
+
col_string = ''
|
368
|
+
|
369
|
+
if row < 0 || col < 0
|
370
|
+
raise 'Invalid input: cannot convert negative numbers'
|
371
|
+
end
|
372
|
+
|
373
|
+
unless col == 0
|
374
|
+
col_length = 1+Integer(Math.log(col) / Math.log(26)) #opposite of 26**
|
375
|
+
else
|
376
|
+
col_length = 1
|
377
|
+
end
|
378
|
+
|
379
|
+
1.upto(col_length) do |i|
|
380
|
+
|
381
|
+
#for the last digit, 0 should mean A. easy way to do this.
|
382
|
+
if i == col_length
|
383
|
+
col+=1
|
384
|
+
end
|
385
|
+
|
386
|
+
if col >= 26**(col_length-i)
|
387
|
+
int_val = col / 26**(col_length-i) #+1 for 0 indexing
|
388
|
+
int_val += 64 #converts 1 to A, etc.
|
389
|
+
|
390
|
+
col_string += int_val.chr
|
391
|
+
|
392
|
+
#intval multiplier decrements by placeholder, essentially
|
393
|
+
#a B subtracts more than an A this way.
|
394
|
+
col -= (int_val-64)*26**(col_length-i)
|
395
|
+
end
|
396
|
+
end
|
397
|
+
col_string+row_string
|
398
|
+
end
|
399
|
+
|
400
|
+
def inspect
|
401
|
+
str = "(#{@row},#{@column}): #{@value}"
|
402
|
+
str += " =#{@formula}" if @formula
|
403
|
+
str += ", datatype = #{@datatype}, style_index = #{@style_index}"
|
404
|
+
return str
|
405
|
+
end
|
406
|
+
|
407
|
+
private
|
408
|
+
|
409
|
+
def change_border(direction, weight)
|
410
|
+
validate_worksheet
|
411
|
+
validate_border(weight)
|
412
|
+
@style_index = modify_border(@workbook,@style_index)
|
413
|
+
if @workbook.borders[border_id()][:border][direction][:attributes].nil?
|
414
|
+
@workbook.borders[border_id()][:border][direction][:attributes] = { :style => nil }
|
415
|
+
end
|
416
|
+
@workbook.borders[border_id()][:border][direction][:attributes][:style] = weight.to_s
|
417
|
+
end
|
418
|
+
|
419
|
+
def get_border(direction)
|
420
|
+
validate_worksheet
|
421
|
+
|
422
|
+
if @workbook.borders[border_id()][:border][direction][:attributes].nil?
|
423
|
+
return nil
|
424
|
+
end
|
425
|
+
return @workbook.borders[border_id()][:border][direction][:attributes][:style]
|
426
|
+
end
|
427
|
+
|
428
|
+
def validate_workbook()
|
429
|
+
unless @workbook.nil? || @workbook.worksheets.nil?
|
430
|
+
@workbook.worksheets.each do |sheet|
|
431
|
+
unless sheet.nil? || sheet.sheet_data.nil? || sheet.sheet_data[@row].nil?
|
432
|
+
if sheet.sheet_data[@row][@column] == self
|
433
|
+
return
|
434
|
+
end
|
435
|
+
end
|
436
|
+
end
|
437
|
+
end
|
438
|
+
raise "This cell #{self} is not in workbook #{@workbook}"
|
439
|
+
end
|
440
|
+
|
441
|
+
def validate_worksheet()
|
442
|
+
if !@worksheet.nil? && @worksheet[@row][@column] == self
|
443
|
+
return
|
444
|
+
else
|
445
|
+
raise "This cell #{self} is not in worksheet #{worksheet}"
|
446
|
+
end
|
447
|
+
end
|
448
|
+
|
449
|
+
def xf_id()
|
450
|
+
@workbook.get_style_attributes(@workbook.get_style(@style_index.to_s))
|
451
|
+
end
|
452
|
+
|
453
|
+
def border_id()
|
454
|
+
xf_id()[:borderId].to_s
|
455
|
+
end
|
456
|
+
|
457
|
+
def font_id()
|
458
|
+
xf_id()[:fontId].to_s
|
459
|
+
end
|
460
|
+
end
|
461
|
+
end
|