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::COL_MAX, 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, #convert_date_time, #dash_types, delete_files, #fill_properties, #float_to_str, #layout_properties, #line_fill_properties, #line_properties, #palette_color, #pixels_to_points, #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_anchor, #write_auto_fill, #write_color, #write_comment_path, #write_div, #write_fill, #write_font, #write_stroke, #write_xml_declaration, #xl_cell_to_rowcol, #xl_col_to_name, #xl_range, #xl_range_formula, #xl_rowcol_to_cell, #xml_str

Constructor Details

#initialize(worksheet, *args) ⇒ Table


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/write_xlsx/package/table.rb', line 29

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

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

  # 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
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id


27
28
29
# File 'lib/write_xlsx/package/table.rb', line 27

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name


27
28
29
# File 'lib/write_xlsx/package/table.rb', line 27

def name
  @name
end

Instance Method Details

#add_the_table_columnsObject


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/write_xlsx/package/table.rb', line 71

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])

    overrite_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.


60
61
62
63
64
65
66
67
68
69
# File 'lib/write_xlsx/package/table.rb', line 60

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

#overrite_the_defaults_with_any_use_defined_values(col_id, col_data, col_num) ⇒ Object


88
89
90
91
92
93
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
# File 'lib/write_xlsx/package/table.rb', line 88

def overrite_the_defaults_with_any_use_defined_values(col_id, col_data, col_num)
  if @param[:columns]
    # Check if there are user defined values for this column.
    if user_data = @param[:columns][col_id]
      # Map user defined values to internal values.
      if user_data[:header] && !user_data[:header].empty?
        col_data.name = user_data[:header]
      end
      # 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.
      if user_data[:format]
        col_data.format = user_data[:format].get_dxf_index
      end

      # 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
end

#prepare(id) ⇒ Object


148
149
150
151
# File 'lib/write_xlsx/package/table.rb', line 148

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

#set_xml_writer(filename) ⇒ Object


53
54
55
# File 'lib/write_xlsx/package/table.rb', line 53

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

#write_the_cell_data_if_suppliedObject


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/write_xlsx/package/table.rb', line 130

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


124
125
126
127
128
# File 'lib/write_xlsx/package/table.rb', line 124

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)
  end
end