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::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

#initialize(workbook) ⇒ ContentTypes


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

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.


146
147
148
# File 'lib/write_xlsx/package/content_types.rb', line 146

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.


93
94
95
96
97
# File 'lib/write_xlsx/package/content_types.rb', line 93

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

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

#add_chart_namesObject


86
87
88
# File 'lib/write_xlsx/package/content_types.rb', line 86

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.


80
81
82
83
84
# File 'lib/write_xlsx/package/content_types.rb', line 80

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

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

#add_chartsheet_namesObject


71
72
73
74
75
# File 'lib/write_xlsx/package/content_types.rb', line 71

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.


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

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

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

#add_comment_namesObject


121
122
123
124
125
# File 'lib/write_xlsx/package/content_types.rb', line 121

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

#add_default(part_name, content_type) ⇒ Object

Add elements to the ContentTypes defaults.


45
46
47
# File 'lib/write_xlsx/package/content_types.rb', line 45

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.


108
109
110
111
112
# File 'lib/write_xlsx/package/content_types.rb', line 108

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

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

#add_drawing_namesObject


99
100
101
102
103
# File 'lib/write_xlsx/package/content_types.rb', line 99

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.


153
154
155
156
157
# File 'lib/write_xlsx/package/content_types.rb', line 153

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

#add_override(part_name, content_type) ⇒ Object

Add elements to the ContentTypes overrides.


52
53
54
# File 'lib/write_xlsx/package/content_types.rb', line 52

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

#add_shared_stringsObject

Add the sharedStrings link to the ContentTypes overrides.


139
140
141
# File 'lib/write_xlsx/package/content_types.rb', line 139

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.


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

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


159
160
161
# File 'lib/write_xlsx/package/content_types.rb', line 159

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.


176
177
178
179
# File 'lib/write_xlsx/package/content_types.rb', line 176

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.


117
118
119
# File 'lib/write_xlsx/package/content_types.rb', line 117

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.


65
66
67
68
69
# File 'lib/write_xlsx/package/content_types.rb', line 65

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

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

#add_worksheet_namesObject


56
57
58
59
60
# File 'lib/write_xlsx/package/content_types.rb', line 56

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

#assemble_xml_fileObject


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

def assemble_xml_file
  write_xml_declaration do
    write_types do
      write_defaults
      write_overrides
    end
  end
end

#set_xml_writer(filename) ⇒ Object


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

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