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 Method Summary collapse

Methods included from Utility

#absolute_char, #check_dimensions, #check_dimensions_and_update_max_min_values, #check_parameter, #convert_date_time, delete_files, #float_to_str, #pixels_to_points, #ptrue?, #put_deprecate_message, #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, #write_anchor, #write_auto_fill, #write_color, #write_comment_path, #write_div, #write_fill, #write_font, #write_stroke, #xl_cell_to_rowcol, #xl_col_to_name, #xl_range, #xl_range_formula, #xl_rowcol_to_cell, #xml_str

Constructor Details

#initialize(worksheet, id, *args) ⇒ Table

attr_reader :id



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

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

  @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 Method Details

#add_the_table_columnsObject



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

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.



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

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

  # Close the XML writer object and filehandle.
  @writer.crlf
  @writer.close
end

#overrite_the_defaults_with_any_use_defined_values(col_id, col_data, col_num) ⇒ Object



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
123
124
125
126
127
128
# File 'lib/write_xlsx/package/table.rb', line 92

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[:total_function],
                                              user_data[:format]
                                              )
      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

#set_xml_writer(filename) ⇒ Object



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

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

#write_the_cell_data_if_suppliedObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/write_xlsx/package/table.rb', line 136

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



130
131
132
133
134
# File 'lib/write_xlsx/package/table.rb', line 130

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