Class: Writexlsx::Package::ContentTypes

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

Constant Summary collapse

App_package =
'application/vnd.openxmlformats-package.'
App_document =
'application/vnd.openxmlformats-officedocument.'

Constants included from Utility

Utility::CHAR_WIDTHS, Utility::COL_MAX, Utility::PERL_TRUE_VALUES, 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, #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(workbook) ⇒ ContentTypes

Returns a new instance of ContentTypes.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/write_xlsx/package/content_types.rb', line 15

def initialize(workbook)
  @writer = Package::XMLWriterSimple.new
  @workbook  = workbook
  @defaults  = [
    ['rels', "#{App_package}relationships+xml"],
    ['xml', 'application/xml']
  ]
  @overrides = [
    ['/docProps/app.xml',    "#{App_document}extended-properties+xml"],
    ['/docProps/core.xml',   "#{App_package}core-properties+xml"],
    ['/xl/styles.xml',       "#{App_document}spreadsheetml.styles+xml"],
    ['/xl/theme/theme1.xml', "#{App_document}theme+xml"],
    ['/xl/workbook.xml',     "#{App_document}spreadsheetml.sheet.main+xml"]
  ]
end

Instance Method Details

#add_calc_chainObject

Add the calcChain link to the ContentTypes overrides.



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

def add_calc_chain
  add_override('/xl/calcChain.xml', "#{App_document}spreadsheetml.calcChain+xml")
end

#add_chart_name(name) ⇒ Object

Add the name of a chart to the ContentTypes overrides.



95
96
97
98
99
# File 'lib/write_xlsx/package/content_types.rb', line 95

def add_chart_name(name)
  chart_name = "/xl/charts/#{name}.xml"

  add_override(chart_name, "#{App_document}drawingml.chart+xml")
end

#add_chart_namesObject



88
89
90
# File 'lib/write_xlsx/package/content_types.rb', line 88

def add_chart_names
  (1..@workbook.charts.size).each { |i| add_chart_name("chart#{i}") }
end

#add_chartsheet_name(name) ⇒ Object

Add the name of a chartsheet to the ContentTypes overrides.



82
83
84
85
86
# File 'lib/write_xlsx/package/content_types.rb', line 82

def add_chartsheet_name(name)
  chartsheet_name = "/xl/chartsheets/#{name}.xml"

  add_override(chartsheet_name, "#{App_document}spreadsheetml.chartsheet+xml")
end

#add_chartsheet_namesObject



73
74
75
76
77
# File 'lib/write_xlsx/package/content_types.rb', line 73

def add_chartsheet_names
  @workbook.chartsheet_count.times do |index|
    add_chartsheet_name("sheet#{index + 1}")
  end
end

#add_comment_name(name) ⇒ Object

Add the name of a comment to the ContentTypes overrides.



132
133
134
135
136
# File 'lib/write_xlsx/package/content_types.rb', line 132

def add_comment_name(name)
  comment_name = "/xl/#{name}.xml"

  add_override(comment_name, "#{App_document}spreadsheetml.comments+xml")
end

#add_comment_namesObject



123
124
125
126
127
# File 'lib/write_xlsx/package/content_types.rb', line 123

def add_comment_names
  (1..@workbook.num_comment_files).each do |i|
    add_comment_name("comments#{i}")
  end
end

#add_custom_propertiesObject

Add the name of a table to the ContentTypes overrides.



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

def add_custom_properties
  custom = "/docProps/custom.xml"

  add_override(custom, "#{App_document}custom-properties+xml")
end

#add_default(part_name, content_type) ⇒ Object

Add elements to the ContentTypes defaults.



47
48
49
# File 'lib/write_xlsx/package/content_types.rb', line 47

def add_default(part_name, content_type)
  @defaults.push([part_name, content_type])
end

#add_drawing_name(name) ⇒ Object

Add the name of a drawing to the ContentTypes overrides.



110
111
112
113
114
# File 'lib/write_xlsx/package/content_types.rb', line 110

def add_drawing_name(name)
  drawing_name = "/xl/drawings/#{name}.xml"

  add_override(drawing_name, "#{App_document}drawing+xml")
end

#add_drawing_namesObject



101
102
103
104
105
# File 'lib/write_xlsx/package/content_types.rb', line 101

def add_drawing_names
  (1..@workbook.drawings.size).each do |i|
    add_drawing_name("drawing#{i}")
  end
end

#add_image_typesObject

Add the image default types.



155
156
157
158
159
# File 'lib/write_xlsx/package/content_types.rb', line 155

def add_image_types
  @workbook.image_types.each_key do |type|
    add_default(type, "image/#{type}")
  end
end

#add_metadataObject

Add the metadata file to the ContentTypes overrides.



195
196
197
198
199
200
# File 'lib/write_xlsx/package/content_types.rb', line 195

def 
  add_override(
    "/xl/metadata.xml",
    "#{App_document}spreadsheetml.sheetMetadata+xml"
  )
end

#add_override(part_name, content_type) ⇒ Object

Add elements to the ContentTypes overrides.



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

def add_override(part_name, content_type)
  @overrides.push([part_name, content_type])
end

#add_richvalueObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/write_xlsx/package/content_types.rb', line 202

def add_richvalue
  add_override(
    '/xl/richData/rdRichValueTypes.xml',
    'application/vnd.ms-excel.rdrichvaluetypes+xml'
  )

  add_override(
    '/xl/richData/rdrichvalue.xml',
    'application/vnd.ms-excel.rdrichvalue+xml'
  )

  add_override(
    '/xl/richData/rdrichvaluestructure.xml',
    'application/vnd.ms-excel.rdrichvaluestructure+xml'
  )

  add_override(
    '/xl/richData/richValueRel.xml',
    'application/vnd.ms-excel.richvaluerel+xml'
  )
end

#add_shared_stringsObject

Add the sharedStrings link to the ContentTypes overrides.



141
142
143
# File 'lib/write_xlsx/package/content_types.rb', line 141

def add_shared_strings
  add_override('/xl/sharedStrings.xml', "#{App_document}spreadsheetml.sharedStrings+xml")
end

#add_table_name(table_name) ⇒ Object

Add the name of a table to the ContentTypes overrides.



168
169
170
171
172
173
# File 'lib/write_xlsx/package/content_types.rb', line 168

def add_table_name(table_name)
  add_override(
    "/xl/tables/#{table_name}.xml",
    "#{App_document}spreadsheetml.table+xml"
  )
end

#add_table_names(table_count) ⇒ Object



161
162
163
# File 'lib/write_xlsx/package/content_types.rb', line 161

def add_table_names(table_count)
  (1..table_count).each { |i| add_table_name("table#{i}") }
end

#add_vba_projectObject

Add a vbaProject to the ContentTypes defaults.



178
179
180
181
# File 'lib/write_xlsx/package/content_types.rb', line 178

def add_vba_project
  change_the_workbook_xml_content_type_from_xlsx_to_xlsm
  add_default('bin', 'application/vnd.ms-office.vbaProject')
end

#add_vml_nameObject

Add the name of a VML drawing to the ContentTypes defaults.



119
120
121
# File 'lib/write_xlsx/package/content_types.rb', line 119

def add_vml_name
  add_default('vml', "#{App_document}vmlDrawing")
end

#add_worksheet_name(name) ⇒ Object

Add the name of a worksheet to the ContentTypes overrides.



67
68
69
70
71
# File 'lib/write_xlsx/package/content_types.rb', line 67

def add_worksheet_name(name)
  worksheet_name = "/xl/worksheets/#{name}.xml"

  add_override(worksheet_name, "#{App_document}spreadsheetml.worksheet+xml")
end

#add_worksheet_namesObject



58
59
60
61
62
# File 'lib/write_xlsx/package/content_types.rb', line 58

def add_worksheet_names
  @workbook.non_chartsheet_count.times do |index|
    add_worksheet_name("sheet#{index + 1}")
  end
end

#assemble_xml_fileObject



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

def assemble_xml_file
  write_xml_declaration do
    write_types do
      write_defaults
      write_overrides
    end
  end
end

#set_xml_writer(filename) ⇒ Object



31
32
33
# File 'lib/write_xlsx/package/content_types.rb', line 31

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