ruby-spreadsheet 0.6.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/.document +5 -0
  2. data/GUIDE.txt +267 -0
  3. data/Gemfile +12 -0
  4. data/Gemfile.lock +20 -0
  5. data/History.txt +307 -0
  6. data/LICENSE.txt +619 -0
  7. data/README.txt +91 -0
  8. data/Rakefile +53 -0
  9. data/VERSION +1 -0
  10. data/bin/xlsopcodes +18 -0
  11. data/lib/parseexcel.rb +27 -0
  12. data/lib/parseexcel/parseexcel.rb +75 -0
  13. data/lib/parseexcel/parser.rb +11 -0
  14. data/lib/spreadsheet.rb +79 -0
  15. data/lib/spreadsheet/column.rb +71 -0
  16. data/lib/spreadsheet/compatibility.rb +23 -0
  17. data/lib/spreadsheet/datatypes.rb +110 -0
  18. data/lib/spreadsheet/encodings.rb +46 -0
  19. data/lib/spreadsheet/excel.rb +88 -0
  20. data/lib/spreadsheet/excel/error.rb +26 -0
  21. data/lib/spreadsheet/excel/internals.rb +386 -0
  22. data/lib/spreadsheet/excel/internals/biff5.rb +17 -0
  23. data/lib/spreadsheet/excel/internals/biff8.rb +19 -0
  24. data/lib/spreadsheet/excel/offset.rb +41 -0
  25. data/lib/spreadsheet/excel/reader.rb +1173 -0
  26. data/lib/spreadsheet/excel/reader/biff5.rb +22 -0
  27. data/lib/spreadsheet/excel/reader/biff8.rb +193 -0
  28. data/lib/spreadsheet/excel/row.rb +92 -0
  29. data/lib/spreadsheet/excel/sst_entry.rb +46 -0
  30. data/lib/spreadsheet/excel/workbook.rb +80 -0
  31. data/lib/spreadsheet/excel/worksheet.rb +100 -0
  32. data/lib/spreadsheet/excel/writer.rb +1 -0
  33. data/lib/spreadsheet/excel/writer/biff8.rb +75 -0
  34. data/lib/spreadsheet/excel/writer/format.rb +253 -0
  35. data/lib/spreadsheet/excel/writer/workbook.rb +652 -0
  36. data/lib/spreadsheet/excel/writer/worksheet.rb +948 -0
  37. data/lib/spreadsheet/font.rb +92 -0
  38. data/lib/spreadsheet/format.rb +177 -0
  39. data/lib/spreadsheet/formula.rb +9 -0
  40. data/lib/spreadsheet/helpers.rb +11 -0
  41. data/lib/spreadsheet/link.rb +43 -0
  42. data/lib/spreadsheet/row.rb +132 -0
  43. data/lib/spreadsheet/workbook.rb +120 -0
  44. data/lib/spreadsheet/worksheet.rb +279 -0
  45. data/lib/spreadsheet/writer.rb +30 -0
  46. data/ruby-spreadsheet.gemspec +126 -0
  47. data/test/data/test_changes.xls +0 -0
  48. data/test/data/test_copy.xls +0 -0
  49. data/test/data/test_datetime.xls +0 -0
  50. data/test/data/test_empty.xls +0 -0
  51. data/test/data/test_formula.xls +0 -0
  52. data/test/data/test_missing_row.xls +0 -0
  53. data/test/data/test_version_excel5.xls +0 -0
  54. data/test/data/test_version_excel95.xls +0 -0
  55. data/test/data/test_version_excel97.xls +0 -0
  56. data/test/excel/row.rb +35 -0
  57. data/test/excel/writer/worksheet.rb +23 -0
  58. data/test/font.rb +163 -0
  59. data/test/integration.rb +1281 -0
  60. data/test/row.rb +33 -0
  61. data/test/suite.rb +14 -0
  62. data/test/workbook.rb +21 -0
  63. data/test/worksheet.rb +80 -0
  64. metadata +203 -0
data/README.txt ADDED
@@ -0,0 +1,91 @@
1
+ Last Update: 08.12.2010 - Zeno Davatz
2
+
3
+
4
+ = Spreadsheet
5
+
6
+ http://spreadsheet.rubyforge.org
7
+
8
+ For a viewable directory of all recent changes, please see:
9
+
10
+ http://scm.ywesee.com/?p=spreadsheet/.git;a=summary
11
+
12
+ For Non-GPLv3 commercial licencing, please see:
13
+
14
+ http://www.spreadsheet.ch
15
+
16
+
17
+ == Description
18
+
19
+ The Spreadsheet Library is designed to read and write Spreadsheet Documents.
20
+ As of version 0.6.0, only Microsoft Excel compatible spreadsheets are
21
+ supported. Spreadsheet is a combination/complete rewrite of the
22
+ Spreadsheet::Excel Library by Daniel J. Berger and the ParseExcel Library by
23
+ Hannes Wyss. Spreadsheet can read, write and modify Spreadsheet Documents.
24
+
25
+
26
+ == What's new?
27
+
28
+ * Supported outline (grouping) functions
29
+ * Significantly improved memory-efficiency when reading large Excel Files
30
+ * Limited Spreadsheet modification support
31
+ * Improved handling of String Encodings
32
+
33
+
34
+ == Roadmap
35
+
36
+ 0.7.0:: Improved Format support/Styles
37
+ 0.7.1:: Document Modification: Formats/Styles
38
+ 0.8.0:: Formula Support
39
+ 0.8.1:: Document Modification: Formulas
40
+ 0.9.0:: Write-Support: BIFF5
41
+ 1.0.0:: Ruby 1.9 Support;
42
+ Remove backward compatibility code
43
+
44
+
45
+ == Dependencies
46
+
47
+ * ruby 1.8
48
+ * ruby-ole[http://code.google.com/p/ruby-ole/]
49
+
50
+
51
+ == Examples
52
+
53
+ Have a look at the GUIDE[link://files/GUIDE_txt.html].
54
+
55
+
56
+ == Installation
57
+
58
+ Using RubyGems[http://www.rubygems.org]:
59
+
60
+ * sudo gem install spreadsheet
61
+
62
+ If you don't like RubyGems[http://www.rubygems.org], let me know which
63
+ installation solution you prefer and I'll include it in the future.
64
+
65
+ If you can use 'rake' and 'hoe' library is also installed, you can
66
+ build a gem package as follows:
67
+
68
+ * rake gem
69
+
70
+ The gem package is built in pkg directory.
71
+
72
+
73
+ == Authors
74
+
75
+ Original Code:
76
+
77
+ Spreadsheet::Excel:
78
+ Copyright (c) 2005 by Daniel J. Berger (djberg96@gmail.com)
79
+
80
+ ParseExcel:
81
+ Copyright (c) 2003 by Hannes Wyss (hannes.wyss@gmail.com)
82
+
83
+ New Code:
84
+ Copyright (c) 2010 ywesee GmbH (mhatakeyama@ywesee.com, zdavatz@ywesee.com)
85
+
86
+
87
+ == License
88
+
89
+ This library is distributed under the GPLv3.
90
+ Please see the LICENSE[link://files/LICENSE_txt.html] file.
91
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "ruby-spreadsheet"
16
+ gem.homepage = "http://github.com/lda/ruby-spreadsheet"
17
+ gem.license = "GPLv3"
18
+ gem.summary = %Q{The Spreadsheet Library is designed to read and write Spreadsheet Documents}
19
+ gem.description = %Q{As of version 0.6.0, only Microsoft Excel compatible spreadsheets are supported}
20
+ gem.email = "lda@openteam.ru, mhatakeyama@ywesee.com, zdavatz@ywesee.com"
21
+ gem.authors = ["Dmitry Lihachev", "Masaomi Hatakeyama", "Zeno R.R. Davatz"]
22
+ gem.add_runtime_dependency 'ruby-ole', '> 1.2'
23
+ gem.executables << 'xlsopcodes'
24
+
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
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 = "ruby-spreadsheet #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.6.5
data/bin/xlsopcodes ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'spreadsheet'
4
+
5
+ source, target = ARGV
6
+
7
+ if source.nil?
8
+ puts "Usage: #{$0} <source> [<target>]"
9
+ exit -1
10
+ end
11
+
12
+ target = target ? File.open(target, 'w') : STDOUT
13
+
14
+ reader = Spreadsheet::Excel::Reader.new :print_opcodes => target
15
+ reader.setup File.open(source)
16
+
17
+ while tuple = reader.get_next_chunk
18
+ end
data/lib/parseexcel.rb ADDED
@@ -0,0 +1,27 @@
1
+ ### Spreadsheet - A Library for reading and writing Spreadsheet Documents.
2
+ #
3
+ # Copyright (C) 2008 Hannes Wyss
4
+ #
5
+ # This program is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ #
18
+ # Contact Information:
19
+ #
20
+ # E-Mail: hannes.wyss@gmail.com
21
+ # P-Mail: ywesee GmbH
22
+ # Hannes Wyss
23
+ # Winterthurerstrasse 52
24
+ # 8006 Zürich
25
+ ### Switzerland
26
+
27
+ require 'parseexcel/parseexcel'
@@ -0,0 +1,75 @@
1
+ require 'spreadsheet'
2
+
3
+ warn <<-EOS
4
+ [DEPRECATED] By requiring 'parseexcel', 'parseexcel/parseexcel' and/or
5
+ 'parseexcel/parser' you are loading a Compatibility layer which
6
+ provides a drop-in replacement for the ParseExcel library. This
7
+ code makes the reading of Spreadsheet documents less efficient and
8
+ will be removed in Spreadsheet version 1.0.0
9
+ EOS
10
+
11
+ module Spreadsheet
12
+ ##
13
+ # The ParseExcel module is provided as a drop-in replacement for the
14
+ # ParseExcel library. This code is deprecated and will be removed in
15
+ # Spreadsheet version 1.0.0
16
+ module ParseExcel
17
+ def ParseExcel.parse path
18
+ Spreadsheet.open path
19
+ end
20
+ class Worksheet
21
+ class Cell
22
+ attr_accessor :value, :kind, :numeric, :code, :book,
23
+ :format, :rich, :encoding, :annotation
24
+ def initialize value, format, row, idx
25
+ @format = format
26
+ @idx = idx
27
+ @row = row
28
+ @value = value
29
+ @encoding = Spreadsheet.client_encoding
30
+ end
31
+ def date
32
+ @row.date @idx
33
+ end
34
+ def datetime
35
+ @row.datetime @idx
36
+ end
37
+ def to_i
38
+ @value.to_i
39
+ end
40
+ def to_f
41
+ @value.to_f
42
+ end
43
+ def to_s(target_encoding=nil)
44
+ if(target_encoding)
45
+ begin
46
+ Iconv.new(target_encoding, @encoding).iconv(@value)
47
+ rescue
48
+ Iconv.new(target_encoding, 'ascii').iconv(@value.to_s)
49
+ end
50
+ else
51
+ @value.to_s
52
+ end
53
+ end
54
+ def type
55
+ if @format && (@format.date? || @format.time?)
56
+ :date
57
+ elsif @value.is_a?(Numeric)
58
+ :numeric
59
+ else
60
+ :text
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ module Excel
67
+ class Reader # :nodoc: all
68
+ def set_cell worksheet, row, column, xf, value=nil
69
+ cells = @current_row_block[row] ||= Row.new(nil, row)
70
+ cells.formats[column] = xf = @workbook.format(xf)
71
+ cells[column] = ParseExcel::Worksheet::Cell.new(value, xf, cells, column)
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,11 @@
1
+ require 'parseexcel'
2
+
3
+ module Spreadsheet
4
+ module ParseExcel # :nodoc: all
5
+ class Parser
6
+ def parse path
7
+ Spreadsheet.open path
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,79 @@
1
+ ### Spreadsheet - A Library for reading and writing Spreadsheet Documents.
2
+ #
3
+ # Copyright (C) 2008-2010 ywesee GmbH
4
+ #
5
+ # This program is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ #
18
+ # Contact Information:
19
+ #
20
+ # E-Mail: mhatakeyama@ywesee.com, zdavatz@ywesee.com
21
+ # P-Mail: ywesee GmbH
22
+ # Zeno R.R. Davatz
23
+ # Winterthurerstrasse 52
24
+ # 8006 Zürich
25
+ ### Switzerland
26
+
27
+ require 'spreadsheet/excel/workbook'
28
+ require 'spreadsheet/excel/reader'
29
+
30
+ # = Synopsis
31
+ # The Spreadsheet Library is designed to read and write Spreadsheet Documents.
32
+ # As of version 0.6.0, only Microsoft Excel compatible spreadsheets are
33
+ # supported.
34
+ #
35
+ # == Example
36
+ # require 'spreadsheet'
37
+ #
38
+ # book = Spreadsheet.open '/path/to/an/excel-file.xls'
39
+ # sheet = book.worksheet 0
40
+ # sheet.each do |row| puts row[0] end
41
+ module Spreadsheet
42
+
43
+ ##
44
+ # The version of Spreadsheet you are using.
45
+ VERSION = '0.6.5.0'
46
+
47
+ ##
48
+ # Default client Encoding. Change this value if your application uses a
49
+ # different Encoding:
50
+ # Spreadsheet.client_encoding = 'ISO-LATIN-1//TRANSLIT//IGNORE'
51
+ @client_encoding = 'UTF-8'
52
+
53
+ class << self
54
+
55
+ attr_accessor :client_encoding
56
+
57
+ ##
58
+ # Parses a Spreadsheet Document and returns a Workbook object. At present,
59
+ # only Excel-Documents can be read.
60
+ def open io_or_path, mode="rb+", &block
61
+ if io_or_path.respond_to? :seek
62
+ Excel::Workbook.open(io_or_path)
63
+ elsif block
64
+ File.open(io_or_path, mode) do |fh|
65
+ block.call open(fh)
66
+ end
67
+ else
68
+ open File.open(io_or_path, mode)
69
+ end
70
+ end
71
+
72
+ ##
73
+ # Returns a Writer object for the specified path. At present, only the
74
+ # Excel-Writer is available.
75
+ def writer io_or_path, type=Excel
76
+ Excel::Writer::Workbook.new io_or_path
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,71 @@
1
+ require 'spreadsheet/datatypes'
2
+
3
+ module Spreadsheet
4
+ ##
5
+ # The Column class. Encapsulates column-formatting and width, and provides a
6
+ # means to iterate over all cells in a column.
7
+ #
8
+ # Useful Attributes:
9
+ # #width:: The width in characters (in respect to the '0' character
10
+ # of the Worksheet's default Font). Float values are
11
+ # permitted, for Excel the available Precision is at 1/256
12
+ # characters.
13
+ # #default_format:: The default Format for cells in this column (applied if
14
+ # there is no explicit Cell Format and no default Row format
15
+ # for the Cell).
16
+ # #hidden:: The Column is hidden.
17
+ # #collapsed:: The Column is collapsed.
18
+ # #outline_level:: Outline level of the column.
19
+ class Column
20
+ class << self
21
+ def updater *keys
22
+ keys.each do |key|
23
+ unless instance_methods.include? "unupdated_#{key}="
24
+ alias_method :"unupdated_#{key}=", :"#{key}="
25
+ define_method "#{key}=" do |value|
26
+ send "unupdated_#{key}=", value
27
+ @worksheet.column_updated @idx, self if @worksheet
28
+ value
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ include Datatypes
35
+ include Enumerable
36
+ attr_accessor :width, :worksheet
37
+ attr_reader :default_format, :idx
38
+ boolean :hidden, :collapsed
39
+ enum :outline_level, 0, Integer
40
+ updater :collapsed, :hidden, :outline_level, :width
41
+ def initialize idx, format, opts={}
42
+ @worksheet = nil
43
+ @idx = idx
44
+ opts[:width] ||= 10
45
+ opts.each do |key, value|
46
+ self.send "#{key}=", value
47
+ end
48
+ self.default_format = format
49
+ end
50
+ ##
51
+ # Set the default Format for Cells in this Column.
52
+ def default_format= format
53
+ @worksheet.add_format format if @worksheet
54
+ @default_format = format
55
+ @worksheet.column_updated @idx, self if @worksheet
56
+ format
57
+ end
58
+ ##
59
+ # Iterate over all cells in this column.
60
+ def each
61
+ @worksheet.each do |row|
62
+ yield row[idx]
63
+ end
64
+ end
65
+ def == other # :nodoc:
66
+ other.is_a?(Column) && default_format == other.default_format \
67
+ && width == other.width && hidden == other.hidden \
68
+ && collapsed == other.collapsed && outline_level == other.outline_level
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,23 @@
1
+ module Spreadsheet
2
+ module Compatibility
3
+ ##
4
+ # One of the most incisive changes in terms of meta-programming in Ruby 1.9
5
+ # is the switch from representing instance-variable names as Strings to
6
+ # presenting them as Symbols. ivar_name provides compatibility.
7
+ if RUBY_VERSION >= '1.9'
8
+ def ivar_name symbol
9
+ :"@#{symbol}"
10
+ end
11
+ def method_name symbol
12
+ symbol.to_sym
13
+ end
14
+ else
15
+ def ivar_name symbol
16
+ "@#{symbol}"
17
+ end
18
+ def method_name symbol
19
+ symbol.to_s
20
+ end
21
+ end
22
+ end
23
+ end