Class: Writexlsx::Sheets

Inherits:
Array
  • Object
show all
Includes:
Utility
Defined in:
lib/write_xlsx/sheets.rb

Constant Summary collapse

BASE_NAME =

:nodoc:

{ :sheet => 'Sheet', :chart => 'Chart'}

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, #dash_types, delete_files, #fill_properties, #float_to_str, #layout_properties, #line_fill_properties, #line_properties, #palette_color, #pixels_to_points, #ptrue?, #put_deprecate_message, #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

#initializeSheets

Returns a new instance of Sheets.



11
12
13
# File 'lib/write_xlsx/sheets.rb', line 11

def initialize
  super([])
end

Instance Method Details

#chartname_countObject



23
24
25
# File 'lib/write_xlsx/sheets.rb', line 23

def chartname_count
  chartsheet_count
end

#chartsheet_countObject



15
16
17
# File 'lib/write_xlsx/sheets.rb', line 15

def chartsheet_count
  chartsheets.count
end

#index_by_name(sheetname) ⇒ Object



162
163
164
165
# File 'lib/write_xlsx/sheets.rb', line 162

def index_by_name(sheetname)
  name = sheetname.sub(/^'/,'').sub(/'$/,'')
  self.collect { |sheet| sheet.name }.index(name)
end

#make_and_check_sheet_chart_name(type, name) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/write_xlsx/sheets.rb', line 27

def make_and_check_sheet_chart_name(type, name)
  count = sheet_chart_count(type)
  name = "#{BASE_NAME[type]}#{count+1}" unless ptrue?(name)

  check_valid_sheetname(name)
  name
end

#sheetname_countObject



19
20
21
# File 'lib/write_xlsx/sheets.rb', line 19

def sheetname_count
  self.count - chartname_count
end

#tablesObject



154
155
156
# File 'lib/write_xlsx/sheets.rb', line 154

def tables
  self.inject([]) { |tables, sheet| tables + sheet.tables }.flatten
end

#tables_countObject



158
159
160
# File 'lib/write_xlsx/sheets.rb', line 158

def tables_count
  tables.count
end

#write_chartsheet_files(package_dir) ⇒ Object



52
53
54
55
56
57
# File 'lib/write_xlsx/sheets.rb', line 52

def write_chartsheet_files(package_dir)
  dir = "#{package_dir}/xl/chartsheets"
  chartsheets.each_with_index do |sheet, index|
    write_sheet_files(dir, sheet, index)
  end
end

#write_chartsheet_rels_files(package_dir) ⇒ Object



91
92
93
94
# File 'lib/write_xlsx/sheets.rb', line 91

def write_chartsheet_rels_files(package_dir)
  write_sheet_rels_files_base(chartsheets, "#{package_dir}/xl/chartsheets/_rels",
                        'sheet')
end

#write_comment_files(package_dir) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/write_xlsx/sheets.rb', line 71

def write_comment_files(package_dir)
  self.select { |sheet| sheet.has_comments? }.
    each_with_index do |sheet, index|
    FileUtils.mkdir_p("#{package_dir}/xl/drawings")
    sheet.comments.set_xml_writer("#{package_dir}/xl/comments#{index+1}.xml")
    sheet.comments.assemble_xml_file
  end
end

#write_drawing_rels_files(package_dir) ⇒ Object



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
# File 'lib/write_xlsx/sheets.rb', line 96

def write_drawing_rels_files(package_dir)
  # write_rels_files_base(
  #                       self.reject { |sheet| sheet.drawing_links[0].empty? },
  #                       "#{package_dir}/xl/drawings/_rels",

  #                       )
  dir = "#{package_dir}/xl/drawings/_rels"

  index = 0
  self.each do |sheet|
    if !sheet.drawing_links[0].empty? || sheet.has_shapes?
      index += 1
    end

    next if sheet.drawing_links[0].empty?

    FileUtils.mkdir_p(dir)

    rels = Package::Relationships.new

    sheet.drawing_links.each do |drawing_datas|
      drawing_datas.each do |drawing_data|
        rels.add_document_relationship(*drawing_data)
      end
    end

    # Create the .rels file such as /xl/drawings/_rels/sheet1.xml.rels.
    rels.set_xml_writer("#{dir}/drawing#{index}.xml.rels")
    rels.assemble_xml_file
  end
end

#write_sheet_rels_files_base(sheets, dir, body) ⇒ Object



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

def write_sheet_rels_files_base(sheets, dir, body)
  sheets.each_with_index do |sheet, index|

    next if sheet.external_links.empty?

    FileUtils.mkdir_p(dir)

    rels = Package::Relationships.new

    sheet.external_links.each do |link_datas|
      link_datas.each do |link_data|
        rels.add_worksheet_relationship(*link_data)
      end
    end

    # Create the .rels file such as /xl/worksheets/_rels/sheet1.xml.rels.
    rels.set_xml_writer("#{dir}/#{body}#{index+1}.xml.rels")
    rels.assemble_xml_file
  end
end

#write_sheets(writer) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/write_xlsx/sheets.rb', line 35

def write_sheets(writer)
  writer.tag_elements('sheets') do
    id_num = 1
    self.each do |sheet|
      write_sheet(writer, sheet, id_num)
      id_num += 1
    end
  end
end

#write_table_files(package_dir) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/write_xlsx/sheets.rb', line 80

def write_table_files(package_dir)
  unless tables.empty?
    dir = "#{package_dir}/xl/tables"
    FileUtils.mkdir_p(dir)
    tables.each_with_index do |table, index|
      table.set_xml_writer("#{dir}/table#{index+1}.xml")
      table.assemble_xml_file
    end
  end
end

#write_vml_files(package_dir) ⇒ Object



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

def write_vml_files(package_dir)
  dir = "#{package_dir}/xl/drawings"
  self.select { |sheet| sheet.has_vml? }.
    each_with_index do |sheet, index|
    FileUtils.mkdir_p(dir)

    vml = Package::Vml.new
    vml.set_xml_writer("#{dir}/vmlDrawing#{index+1}.vml")
    vml.assemble_xml_file(sheet)
  end
end

#write_worksheet_files(package_dir) ⇒ Object



45
46
47
48
49
50
# File 'lib/write_xlsx/sheets.rb', line 45

def write_worksheet_files(package_dir)
  dir = "#{package_dir}/xl/worksheets"
  worksheets.each_with_index do |sheet, index|
    write_sheet_files(dir, sheet, index)
  end
end

#write_worksheet_rels_files(package_dir) ⇒ Object



128
129
130
131
# File 'lib/write_xlsx/sheets.rb', line 128

def write_worksheet_rels_files(package_dir)
  write_sheet_rels_files_base(worksheets, "#{package_dir}/xl/worksheets/_rels",
                        'sheet')
end