File: column.rb

package info (click to toggle)
ruby-spreadsheet 1.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,964 kB
  • sloc: ruby: 6,943; makefile: 10
file content (71 lines) | stat: -rw-r--r-- 2,371 bytes parent folder | download | duplicates (5)
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
require 'spreadsheet/datatypes'

module Spreadsheet
  ##
  # The Column class. Encapsulates column-formatting and width, and provides a
  # means to iterate over all cells in a column.
  #
  # Useful Attributes:
  # #width::          The width in characters (in respect to the '0' character
  #                   of the Worksheet's default Font). Float values are
  #                   permitted, for Excel the available Precision is at 1/256
  #                   characters.
  # #default_format:: The default Format for cells in this column (applied if
  #                   there is no explicit Cell Format and no default Row format
  #                   for the Cell).
  # #hidden::         The Column is hidden.
  # #collapsed::      The Column is collapsed.
  # #outline_level::  Outline level of the column.
  class Column
    class << self
      def updater *keys
        keys.each do |key|
          unless instance_methods.include? "unupdated_#{key}="
            alias_method :"unupdated_#{key}=", :"#{key}="
            define_method "#{key}=" do |value|
              send "unupdated_#{key}=", value
              @worksheet.column_updated @idx, self if @worksheet
              value
            end
          end
        end
      end
    end
    include Datatypes
    include Enumerable
    attr_accessor :width, :worksheet
    attr_reader :default_format, :idx
    boolean :hidden, :collapsed
    enum :outline_level, 0, Integer
    updater :collapsed, :hidden, :outline_level, :width
    def initialize idx, format, opts={}
      @worksheet = nil
      @idx = idx
      opts[:width] ||= 10
      opts.each do |key, value|
        self.send "#{key}=", value
      end
      self.default_format = format
    end
    ##
    # Set the default Format for Cells in this Column.
    def default_format= format
      @worksheet.add_format format if @worksheet
      @default_format = format
      @worksheet.column_updated @idx, self if @worksheet
      format
    end
    ##
    # Iterate over all cells in this column.
    def each
      @worksheet.each do |row|
        yield row[idx]
      end
    end
    def == other # :nodoc:
      other.is_a?(Column) && default_format == other.default_format \
        && width == other.width && hidden == other.hidden \
        && collapsed == other.collapsed && outline_level == other.outline_level
    end
  end
end