summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx/workbook/worksheet/sheet_pr.rb
blob: 0676170e268a84ac2772944fef6c10aed24218fe (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# frozen_string_literal: true

module Axlsx
  # The SheetPr class manages serialization of a worksheet's sheetPr element.
  class SheetPr
    include Axlsx::OptionsParser
    include Axlsx::Accessors
    include Axlsx::SerializedAttributes

    serializable_attributes :sync_horizontal,
                            :sync_vertical,
                            :transition_evaluation,
                            :transition_entry,
                            :published,
                            :filter_mode,
                            :enable_format_conditions_calculation,
                            :code_name,
                            :sync_ref

    # 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,
                          :transition_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
      @outline_pr = nil
      parse_options options
    end

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

    # The tab color of the sheet.
    # @return [Color]
    attr_reader :tab_color

    # 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(str)
      str << '>'
      tab_color.to_xml_string(str, 'tabColor') if tab_color
      outline_pr.to_xml_string(str) if @outline_pr
      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

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

    # @see tab_color
    def tab_color=(v)
      @tab_color = Color.new(rgb: v)
    end

    private

    def update_properties
      page_setup_pr.fit_to_page = worksheet.fit_to_page?
      unless worksheet.auto_filter.columns.empty?
        self.filter_mode = 1
        self.enable_format_conditions_calculation = 1
      end
    end
  end
end