File: parseexcel.rb

package info (click to toggle)
ruby-spreadsheet 1.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,980 kB
  • sloc: ruby: 6,939; makefile: 10
file content (83 lines) | stat: -rw-r--r-- 2,156 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require "spreadsheet"

warn <<~EOS
  [DEPRECATED] By requiring 'parseexcel', 'parseexcel/parseexcel' and/or
               'parseexcel/parser' you are loading a Compatibility layer which
               provides a drop-in replacement for the ParseExcel library. This
               code makes the reading of Spreadsheet documents less efficient and
               will be removed in Spreadsheet version 1.0.0
EOS

module Spreadsheet
  ##
  # The ParseExcel module is provided as a drop-in replacement for the
  # ParseExcel library. This code is deprecated and will be removed in
  # Spreadsheet version 1.0.0
  module ParseExcel
    def self.parse path
      Spreadsheet.open path
    end

    class Worksheet
      class Cell
        attr_accessor :value, :kind, :numeric, :code, :book,
          :format, :rich, :encoding, :annotation
        def initialize value, format, row, idx
          @format = format
          @idx = idx
          @row = row
          @value = value
          @encoding = Spreadsheet.client_encoding
        end

        def date
          @row.date @idx
        end

        def datetime
          @row.datetime @idx
        end

        def to_i
          @value.to_i
        end

        def to_f
          @value.to_f
        end

        def to_s(target_encoding = nil)
          if target_encoding
            begin
              Iconv.new(target_encoding, @encoding).iconv(@value)
            rescue
              Iconv.new(target_encoding, "ascii").iconv(@value.to_s)
            end
          else
            @value.to_s
          end
        end

        def type
          if @format && (@format.date? || @format.time?)
            :date
          elsif @value.is_a?(Numeric)
            :numeric
          else
            :text
          end
        end
      end
    end
  end

  module Excel
    class Reader # :nodoc: all
      def set_cell worksheet, row, column, xf, value = nil
        cells = @current_row_block[row] ||= Row.new(nil, row)
        cells.formats[column] = xf = @workbook.format(xf)
        cells[column] = ParseExcel::Worksheet::Cell.new(value, xf, cells, column)
      end
    end
  end
end