Class: Writexlsx::Package::Table

Inherits:
Object
  • Object
show all
Includes:
Utility
Defined in:
lib/write_xlsx/package/table.rb

Defined Under Namespace

Classes: ColumnData

Constant Summary

Constants included from Utility

Utility::CHAR_WIDTHS, Utility::COL_MAX, Utility::PERL_TRUE_VALUES, Utility::ROW_MAX, Utility::SHEETNAME_MAX, Utility::STR_MAX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utility

#absolute_char, #check_dimensions, #check_dimensions_and_update_max_min_values, #check_parameter, #color, #convert_date_time, #convert_font_args, #dash_types, delete_files, #escape_url, #fill_properties, #float_to_str, #get_font_latin_attributes, #get_font_style_attributes, #get_image_properties, #layout_properties, #legend_properties, #line_fill_properties, #line_properties, #palette_color, #params_to_font, #pattern_properties, #pixels_to_points, #process_bmp, #process_gif, #process_jpg, #process_png, #process_workbook_options, #ptrue?, #put_deprecate_message, #quote_sheetname, #r_id_attributes, #row_col_notation, #shape_style_base, #store_col_max_min_values, #store_row_max_min_values, #substitute_cellref, #underline_attributes, #v_shape_attributes_base, #v_shape_style_base, #value_or_raise, #write_a_body_pr, #write_a_def_rpr, #write_a_end_para_rpr, #write_a_lst_style, #write_a_p_formula, #write_a_p_pr_formula, #write_a_solid_fill, #write_a_srgb_clr, #write_anchor, #write_auto_fill, #write_color, #write_comment_path, #write_def_rpr_r_pr_common, #write_div, #write_fill, #write_font, #write_stroke, #write_tx_pr, #write_xml_declaration, #xl_cell_to_rowcol, #xl_col_to_name, #xl_range, #xl_range_formula, #xl_rowcol_to_cell, #xl_string_pixel_width, #xml_str

Constructor Details

#initialize(worksheet, *args) ⇒ Table

Returns a new instance of Table.



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

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.



30
31
32
# File 'lib/write_xlsx/package/table.rb', line 30

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/write_xlsx/package/table.rb', line 30

def name
  @name
end

Instance Method Details

#add_the_table_columnsObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/write_xlsx/package/table.rb', line 77

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.



66
67
68
69
70
71
72
73
74
75
# File 'lib/write_xlsx/package/table.rb', line 66

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



94
95
96
97
98
99
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
# File 'lib/write_xlsx/package/table.rb', line 94

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



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

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

#set_xml_writer(filename) ⇒ Object



59
60
61
# File 'lib/write_xlsx/package/table.rb', line 59

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

#store_filter_cell_positionsObject



178
179
180
181
182
183
184
# File 'lib/write_xlsx/package/table.rb', line 178

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



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/write_xlsx/package/table.rb', line 161

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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/write_xlsx/package/table.rb', line 143

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



135
136
137
138
139
140
141
# File 'lib/write_xlsx/package/table.rb', line 135

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