summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx/workbook/worksheet/sheet_pr.rb
blob: 79e10ca0b1df61515ef3e9eed4cbeae14442dfb3 (plain)
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
module Axlsx

  # The SheetPr class manages serialization fo a worksheet's sheetPr element.
  class SheetPr
 
    include Axlsx::Accessors

    # These attributes are all boolean so I'm doing a bit of a hand
    # waving magic show to set up the attriubte accessors
    boolean_attr_accessor :sync_horizontal,
                          :sync_vertical,
                          :transtion_evaluation,
                          :transition_entry,
                          :published,
                          :filter_mode,
                          :enable_format_conditions_calculation

    string_attr_accessor :code_name, :sync_ref

    # Creates a new SheetPr object
    # @param [Worksheet] worksheet The worksheet that owns this SheetPr object
    def initialize(worksheet, options={})
      raise ArgumentError, "you must provide a worksheet" unless worksheet.is_a?(Worksheet)
      @worksheet = worksheet
      options.each do |key, value|
        attr = "#{key}="  
        self.send(attr, value) if self.respond_to?(attr)
      end
    end

    # The worksheet these properties apply to!
    # @return [Worksheet]
    attr_reader :worksheet

    # Serialize the object
    # @param [String] str serialized output will be appended to this object if provided.
    # @return [String]
    def to_xml_string(str = '')
      update_properties
      str << "<sheetPr #{serialized_attributes}>"
      page_setup_pr.to_xml_string(str)
      str << "</sheetPr>"
    end

    # The PageSetUpPr for this sheet pr object
    # @return [PageSetUpPr]
    def page_setup_pr
      @page_setup_pr ||= PageSetUpPr.new
    end

    private

    def serialized_attributes(str = '')
      instance_values.each do |key, value| 
        unless %(worksheet page_setup_pr).include? key
          str << "#{Axlsx.camel(key, false)}='#{value}' " 
        end
      end
      str
    end

    def update_properties
      page_setup_pr.fit_to_page = worksheet.fit_to_page?
      if worksheet.auto_filter.columns.size > 0
        self.filter_mode = 1
        self.enable_format_conditions_calculation = 1
      end
    end
  end
end