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

#initializeSheets


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

#chartsheetsObject


204
205
206
# File 'lib/write_xlsx/sheets.rb', line 204

def chartsheets
  self.select { |worksheet| worksheet.is_chartsheet? }
end

#index_by_name(sheetname) ⇒ Object


195
196
197
198
# File 'lib/write_xlsx/sheets.rb', line 195

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


187
188
189
# File 'lib/write_xlsx/sheets.rb', line 187

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

#tables_countObject


191
192
193
# File 'lib/write_xlsx/sheets.rb', line 191

def tables_count
  tables.count
end

#worksheetsObject


200
201
202
# File 'lib/write_xlsx/sheets.rb', line 200

def worksheets
  self.reject { |worksheet| worksheet.is_chartsheet? }
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


108
109
110
111
# File 'lib/write_xlsx/sheets.rb', line 108

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


88
89
90
91
92
93
94
95
# File 'lib/write_xlsx/sheets.rb', line 88

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


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
140
141
142
143
# File 'lib/write_xlsx/sheets.rb', line 113

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


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/write_xlsx/sheets.rb', line 166

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


97
98
99
100
101
102
103
104
105
106
# File 'lib/write_xlsx/sheets.rb', line 97

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_drawing_rels_files(package_dir, worksheet, index) ⇒ Object


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

def write_vml_drawing_rels_files(package_dir, worksheet, index)
  # Create the drawing .rels dir.
  dir = "#{package_dir}/xl/drawings/_rels"
  FileUtils.mkdir_p(dir)

  rels = Package::Relationships.new

  worksheet.vml_drawing_links.each do |drawing_data|
    rels.add_document_relationship(*drawing_data)
  end

  # Create the .rels file such as /xl/drawings/_rels/vmlDrawing1.vml.rels.
  rels.set_xml_writer("#{dir}/vmlDrawing#{index}.vml.rels")
  rels.assemble_xml_file
end

#write_vml_files(package_dir) ⇒ Object


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/write_xlsx/sheets.rb', line 59

def write_vml_files(package_dir)
  dir = "#{package_dir}/xl/drawings"
  index = 1
  self.each do |sheet|
    next if !sheet.has_vml? and !sheet.has_header_vml?
    FileUtils.mkdir_p(dir)

    if sheet.has_vml?
      vml = Package::Vml.new
      vml.set_xml_writer("#{dir}/vmlDrawing#{index}.vml")
      vml.assemble_xml_file(
                            sheet.vml_data_id, sheet.vml_shape_id,
                            sheet.sorted_comments, sheet.buttons_data
                            )
      index += 1
    end
    if sheet.has_header_vml?
      vml = Package::Vml.new
      vml.set_xml_writer("#{dir}/vmlDrawing#{index}.vml")
      vml.assemble_xml_file(
                            sheet.vml_header_id, sheet.vml_header_id * 1024,
                            [], [], sheet.header_images_data
                            )
      write_vml_drawing_rels_files(package_dir, sheet, index)
      index += 1
    end
  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


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

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