Class: Writexlsx::Package::Table

Inherits:
Object
  • Object
show all
Includes:
Utility::CellReference, Utility::Common, Utility::Dimensions, Utility::XmlPrimitives
Defined in:
lib/write_xlsx/package/table.rb

Defined Under Namespace

Classes: ColumnData

Constant Summary

Constants included from Constants

Constants::COL_MAX, Constants::ROW_MAX, Constants::SHEETNAME_MAX, Constants::STR_MAX

Constants included from Utility::Common

Utility::Common::PERL_TRUE_VALUES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utility::XmlPrimitives

#r_id_attributes, #write_color, #write_xml_declaration, #xml_str

Methods included from Utility::Dimensions

#check_dimensions, #check_dimensions_and_update_max_min_values, #store_col_max_min_values, #store_row_max_min_values

Methods included from Utility::CellReference

#row_col_notation, #substitute_cellref, #xl_cell_to_rowcol, #xl_col_to_name, #xl_range, #xl_range_formula, #xl_rowcol_to_cell

Methods included from Utility::Common

#absolute_char, #check_parameter, #float_to_str, #ptrue?, #put_deprecate_message

Constructor Details

#initialize(worksheet, *args) ⇒ Table

Returns a new instance of Table.



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
# File 'lib/write_xlsx/package/table.rb', line 38

def initialize(worksheet, *args)
  @worksheet = worksheet
  @writer  = Package::XMLWriterSimple.new

  @row1, @row2, @col1, @col2, @param = handle_args(*args)
  @columns     = []
  @col_formats = []
  @seen_name   = {}

  # Set the data range rows (without the header and footer).
  @first_data_row = @row1
  @first_data_row += 1 if ptrue?(@param[:header_row])
  @last_data_row = @row2
  @last_data_row -= 1 if @param[:total_row]

  set_the_table_options
  set_the_table_style
  set_the_table_name
  set_the_table_and_autofilter_ranges
  set_the_autofilter_range

  add_the_table_columns
  write_the_cell_data_if_supplied
  write_any_columns_formulas_after_the_user_supplied_table_data
  store_filter_cell_positions
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



36
37
38
# File 'lib/write_xlsx/package/table.rb', line 36

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/write_xlsx/package/table.rb', line 36

def name
  @name
end

Instance Method Details

#add_the_table_columnsObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/write_xlsx/package/table.rb', line 83

def add_the_table_columns
  col_id = 0
  (@col1..@col2).each do |col_num|
    # Set up the default column data.
    col_data = Package::Table::ColumnData.new(col_id + 1, @param[:columns])

    overwrite_the_defaults_with_any_use_defined_values(col_id, col_data, col_num)

    # Store the column data.
    @columns << col_data

    write_the_column_headers_to_the_worksheet(col_num, col_data)

    col_id += 1
  end    # Table columns.
end

#assemble_xml_fileObject

Assemble and writes the XML file.



72
73
74
75
76
77
78
79
80
81
# File 'lib/write_xlsx/package/table.rb', line 72

def assemble_xml_file
  write_xml_declaration do
    # Write the table element.
    @writer.tag_elements('table', write_table_attributes) do
      write_auto_filter
      write_table_columns
      write_table_style_info
    end
  end
end

#overwrite_the_defaults_with_any_use_defined_values(col_id, col_data, col_num) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/write_xlsx/package/table.rb', line 100

def overwrite_the_defaults_with_any_use_defined_values(col_id, col_data, col_num)
  # Check if there are user defined values for this column.
  if @param[:columns] && (user_data = @param[:columns][col_id])
    # Map user defined values to internal values.
    col_data.name = user_data[:header] if user_data[:header] && !user_data[:header].empty?

    # Excel requires unique case insensitive header names.
    if @seen_name[col_data.name.downcase]
      raise "add_table() contains duplicate name: '#{col_data.name}'"
    else
      @seen_name[col_data.name.downcase] = true
    end

    # Get the header format if defined.
    col_data.name_format = user_data[:header_format]

    # Handle the column formula.
    handle_the_column_formula(
      col_data, col_num, user_data[:formula], user_data[:format]
    )

    # Handle the function for the total row.
    if user_data[:total_function]
      handle_the_function_for_the_table_row(
        @row2, col_data, col_num, user_data
      )
    elsif user_data[:total_string]
      total_label_only(
        @row2, col_num, col_data, user_data[:total_string], user_data[:format]
      )
    end

    # Get the dxf format index.
    col_data.format = user_data[:format].get_dxf_index if user_data[:format]

    # Store the column format for writing the cell data.
    # It doesn't matter if it is undefined.
    @col_formats[col_id] = user_data[:format]
  end
end

#prepare(id) ⇒ Object



192
193
194
195
# File 'lib/write_xlsx/package/table.rb', line 192

def prepare(id)
  @id = id
  @name ||= "Table#{id}"
end

#set_xml_writer(filename) ⇒ Object



65
66
67
# File 'lib/write_xlsx/package/table.rb', line 65

def set_xml_writer(filename)
  @writer.set_xml_writer(filename)
end

#store_filter_cell_positionsObject



184
185
186
187
188
189
190
# File 'lib/write_xlsx/package/table.rb', line 184

def store_filter_cell_positions
  if ptrue?(@param[:autofilter])
    (@col1..@col2).each do |col|
      @worksheet.filter_cells["#{@row1}:#{col}"] = 1
    end
  end
end

#write_any_columns_formulas_after_the_user_supplied_table_dataObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/write_xlsx/package/table.rb', line 167

def write_any_columns_formulas_after_the_user_supplied_table_data
  col_id = 0

  (@col1..@col2).each do |col|
    column_data = @columns[col_id]
    if ptrue?(column_data) && ptrue?(column_data.formula)
      formula_format = @col_formats[col_id]
      formula        = column_data.formula

      (@first_data_row..@last_data_row).each do |row|
        @worksheet.write_formula(row, col, formula, formula_format)
      end
    end
    col_id += 1
  end
end

#write_the_cell_data_if_suppliedObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/write_xlsx/package/table.rb', line 149

def write_the_cell_data_if_supplied
  return unless @param[:data]

  data = @param[:data]
  i = 0    # For indexing the row data.
  (@first_data_row..@last_data_row).each do |row|
    next unless data[i]

    j = 0    # For indexing the col data.
    (@col1..@col2).each do |col|
      token = data[i][j]
      @worksheet.write(row, col, token, @col_formats[j]) if token
      j += 1
    end
    i += 1
  end
end

#write_the_column_headers_to_the_worksheet(col_num, col_data) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/write_xlsx/package/table.rb', line 141

def write_the_column_headers_to_the_worksheet(col_num, col_data)
  if @param[:header_row] != 0
    @worksheet.write_string(
      @row1, col_num, col_data.name, col_data.name_format
    )
  end
end