axlsx 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +22 -0
- data/README.md +70 -0
- data/lib/axlsx.rb~ +67 -0
- data/lib/axlsx/content_type/content_type.rb~ +20 -0
- data/lib/axlsx/content_type/default.rb~ +32 -0
- data/lib/axlsx/content_type/override.rb~ +30 -0
- data/lib/axlsx/doc_props/app.rb~ +127 -0
- data/lib/axlsx/doc_props/core.rb~ +25 -0
- data/lib/axlsx/drawing/axis.rb~ +0 -0
- data/lib/axlsx/drawing/bar_3D_chart.rb~ +64 -0
- data/lib/axlsx/drawing/bar_series.rb~ +92 -0
- data/lib/axlsx/drawing/cat_axis.rb~ +32 -0
- data/lib/axlsx/drawing/chart.rb~ +0 -0
- data/lib/axlsx/drawing/drawing.rb~ +102 -0
- data/lib/axlsx/drawing/graphic_frame.rb~ +40 -0
- data/lib/axlsx/drawing/marker.rb~ +50 -0
- data/lib/axlsx/drawing/pie_3D_chart.rb~ +132 -0
- data/lib/axlsx/drawing/pie_series.rb~ +0 -0
- data/lib/axlsx/drawing/scaling.rb~ +0 -0
- data/lib/axlsx/drawing/series.rb~ +114 -0
- data/lib/axlsx/drawing/title.rb~ +69 -0
- data/lib/axlsx/drawing/two_cell_anchor.rb~ +70 -0
- data/lib/axlsx/drawing/val_axis.rb~ +34 -0
- data/lib/axlsx/drawing/view_3D.rb~ +21 -0
- data/lib/axlsx/package.rb +3 -3
- data/lib/axlsx/package.rb~ +181 -0
- data/lib/axlsx/rels/relationship.rb~ +18 -0
- data/lib/axlsx/rels/relationships.rb~ +23 -0
- data/lib/axlsx/stylesheet/border.rb~ +24 -0
- data/lib/axlsx/stylesheet/border_pr.rb~ +64 -0
- data/lib/axlsx/stylesheet/cell_alignment.rb~ +93 -0
- data/lib/axlsx/stylesheet/cell_protection.rb~ +16 -0
- data/lib/axlsx/stylesheet/cell_style.rb~ +61 -0
- data/lib/axlsx/stylesheet/color.rb~ +56 -0
- data/lib/axlsx/stylesheet/fill.rb~ +31 -0
- data/lib/axlsx/stylesheet/font.rb~ +33 -0
- data/lib/axlsx/stylesheet/gradient_fill.rb~ +70 -0
- data/lib/axlsx/stylesheet/gradient_stop.rb~ +15 -0
- data/lib/axlsx/stylesheet/num_fmt.rb~ +60 -0
- data/lib/axlsx/stylesheet/pattern_fill.rb~ +63 -0
- data/lib/axlsx/stylesheet/styles.rb~ +279 -0
- data/lib/axlsx/stylesheet/table_style.rb~ +43 -0
- data/lib/axlsx/stylesheet/table_style_element.rb~ +66 -0
- data/lib/axlsx/stylesheet/table_styles.rb~ +36 -0
- data/lib/axlsx/stylesheet/xf.rb~ +37 -0
- data/lib/axlsx/util/constants.rb +1 -1
- data/lib/axlsx/util/constants.rb~ +187 -0
- data/lib/axlsx/util/monkey_patches_for_true_zip_stream.rb~ +61 -0
- data/lib/axlsx/util/simple_typed_list.rb~ +79 -0
- data/lib/axlsx/util/validators.rb +3 -3
- data/lib/axlsx/util/validators.rb~ +132 -0
- data/lib/axlsx/util/xml_escape.rb~ +6 -0
- data/lib/axlsx/workbook/workbook.rb +1 -0
- data/lib/axlsx/workbook/workbook.rb~ +130 -0
- data/lib/axlsx/workbook/worksheet/cell.rb~ +185 -0
- data/lib/axlsx/workbook/worksheet/row.rb +4 -4
- data/lib/axlsx/workbook/worksheet/row.rb~ +92 -0
- data/lib/axlsx/workbook/worksheet/worksheet.rb +3 -4
- data/lib/axlsx/workbook/worksheet/worksheet.rb~ +194 -0
- data/lib/schema/dc.xsd~ +118 -0
- data/lib/schema/dcterms.xsd~ +331 -0
- data/lib/schema/opc-coreProperties.xsd~ +50 -0
- data/test/drawing/tc_bar_3D_chart.rb +1 -1
- data/test/drawing/tc_bar_3D_chart.rb~ +62 -0
- data/test/drawing/tc_chart.rb +1 -1
- data/test/drawing/tc_chart.rb~ +1 -0
- data/test/drawing/tc_pie_3D_chart.rb +1 -1
- data/test/drawing/tc_pie_3D_chart.rb~ +3 -28
- data/test/drawing/tc_title.rb +1 -1
- data/test/drawing/tc_title.rb~ +16 -19
- data/test/drawing/tc_two_cell_anchor.rb +1 -1
- data/test/drawing/tc_two_cell_anchor.rb~ +16 -14
- data/test/tc_package.rb +1 -1
- data/test/workbook/worksheet/tc_row.rb +1 -1
- data/test/workbook/worksheet/tc_row.rb~ +30 -0
- data/test/workbook/worksheet/tc_worksheet.rb +3 -3
- data/test/workbook/worksheet/tc_worksheet.rb~ +85 -0
- metadata +84 -102
File without changes
|
File without changes
|
@@ -0,0 +1,114 @@
|
|
1
|
+
module Axlsx
|
2
|
+
# A Series defines the title, data and labels for chart data.
|
3
|
+
# @note The recommended way to manage series is to use Chart#add_series
|
4
|
+
# @see Worksheet#add_chart
|
5
|
+
# @see Chart#add_series
|
6
|
+
class Series
|
7
|
+
|
8
|
+
# The series title.
|
9
|
+
# @return [String]
|
10
|
+
attr_accessor :title
|
11
|
+
|
12
|
+
# The chart that owns this series
|
13
|
+
# @return [Chart]
|
14
|
+
attr_reader :chart
|
15
|
+
|
16
|
+
# The data for this series.
|
17
|
+
# @return [Array, SimpleTypedList]
|
18
|
+
attr_reader :data
|
19
|
+
|
20
|
+
|
21
|
+
# The index of this series in the chart's series.
|
22
|
+
# @return [Integer]
|
23
|
+
attr_reader :index
|
24
|
+
|
25
|
+
# The labels for this series.
|
26
|
+
# @return [Array, SimpleTypedList]
|
27
|
+
attr_reader :labels
|
28
|
+
|
29
|
+
|
30
|
+
# Creates a new series
|
31
|
+
# @option options [Array, SimpleTypedList] data
|
32
|
+
# @option options [Array, SimpleTypedList] labels
|
33
|
+
# @option options [String] title
|
34
|
+
# @param [Chart] chart
|
35
|
+
def initialize(chart, options={})
|
36
|
+
self.chart = chart
|
37
|
+
@chart.series << self
|
38
|
+
self.data = options[:data] || []
|
39
|
+
self.labels = options[:labels] || []
|
40
|
+
@title = options[:title] || ''
|
41
|
+
end
|
42
|
+
|
43
|
+
# Serializes the series
|
44
|
+
# @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
|
45
|
+
# @return [String]
|
46
|
+
def to_xml(xml)
|
47
|
+
xml.send('c:ser') {
|
48
|
+
xml.send('c:idx', :val=>index)
|
49
|
+
xml.send('c:order', :val=>index)
|
50
|
+
xml.send('c:tx') {
|
51
|
+
xml.send('c:v', self.title)
|
52
|
+
}
|
53
|
+
|
54
|
+
if !labels.empty?
|
55
|
+
xml.send('c:cat') {
|
56
|
+
xml.send('c:strRef') {
|
57
|
+
xml.send('c:f', range(labels))
|
58
|
+
xml.send('c:strCache') {
|
59
|
+
xml.send('c:ptCount', :val=>labels.size)
|
60
|
+
labels.each_with_index do |cell, index|
|
61
|
+
v = cell.is_a?(Cell) ? cell.value : cell
|
62
|
+
xml.send('c:pt', :idx=>index) {
|
63
|
+
xml.send('c:v', v)
|
64
|
+
}
|
65
|
+
end
|
66
|
+
}
|
67
|
+
}
|
68
|
+
}
|
69
|
+
end
|
70
|
+
xml.send('c:val') {
|
71
|
+
xml.send('c:numRef') {
|
72
|
+
xml.send('c:f', range(data))
|
73
|
+
xml.send('c:numCache') {
|
74
|
+
xml.send('c:formatCode', 'General')
|
75
|
+
xml.send('c:ptCount', :val=>data.size)
|
76
|
+
data.each_with_index do |cell, index|
|
77
|
+
v = cell.is_a?(Cell) ? cell.value : cell
|
78
|
+
xml.send('c:pt', :idx=>index) {
|
79
|
+
xml.send('c:v', v)
|
80
|
+
}
|
81
|
+
end
|
82
|
+
}
|
83
|
+
}
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
def index
|
90
|
+
@chart.series.index(self)
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
# determines the cell range for the items provided
|
96
|
+
def range(items)
|
97
|
+
return "" unless items.first.is_a? Cell
|
98
|
+
|
99
|
+
"#{items.first.row.worksheet.name}!" +
|
100
|
+
"#{items.first.r_abs}:#{items.last.r_abs}"
|
101
|
+
end
|
102
|
+
|
103
|
+
|
104
|
+
# assigns the data for this series
|
105
|
+
def data=(v) DataTypeValidator.validate "Series.data", [Array, SimpleTypedList], v; @data = v; end
|
106
|
+
|
107
|
+
# assigns the labels for this series
|
108
|
+
def labels=(v) DataTypeValidator.validate "Series.labels", [Array, SimpleTypedList], v; @labels = v; end
|
109
|
+
|
110
|
+
# assigns the chart for this series
|
111
|
+
def chart=(v) DataTypeValidator.validate "Series.chart", [Chart, Pie3DChart], v; @chart = v; end
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Axlsx
|
2
|
+
# A Title stores information about the title of a chart
|
3
|
+
class Title
|
4
|
+
|
5
|
+
# The text to be shown. Setting this property directly with a string will remove the cell reference.
|
6
|
+
# @return [String]
|
7
|
+
attr_accessor :text
|
8
|
+
|
9
|
+
# The cell that holds the text for the title. Setting this property will automatically update the text attribute.
|
10
|
+
# @return [Cell]
|
11
|
+
attr_accessor :cell
|
12
|
+
|
13
|
+
# Creates a new Title object
|
14
|
+
# @param [String, Cell] title The cell or string to be used for the chart's title
|
15
|
+
def initialize(title="")
|
16
|
+
self.cell = title if title.is_a?(Cell)
|
17
|
+
self.text = title.to_s unless title.is_a?(Cell)
|
18
|
+
end
|
19
|
+
|
20
|
+
def text=(v)
|
21
|
+
DataTypeValidator.validate 'Title.text', String, v
|
22
|
+
@text = v
|
23
|
+
@cell = nil
|
24
|
+
v
|
25
|
+
end
|
26
|
+
|
27
|
+
def cell=(v)
|
28
|
+
DataTypeValidator.validate 'Title.text', Cell, v
|
29
|
+
@cell = v
|
30
|
+
@text = v.value.to_s
|
31
|
+
v
|
32
|
+
end
|
33
|
+
|
34
|
+
# Not implemented at this time.
|
35
|
+
#def tx=(v) DataTypeValidator.validate 'Title.tx', Tx, v; @tx=v; end
|
36
|
+
#def layout=(v) DataTypeValidator.validate 'Title.layout', Layout, v; @layout = v; end
|
37
|
+
#def overlay=(v) Axlsx::validate_boolean v; @overlay=v; end
|
38
|
+
#def spPr=(v) DataTypeValidator.validate 'Title.spPr', SpPr, v; @spPr = v; end
|
39
|
+
|
40
|
+
# Serializes the chart title
|
41
|
+
# @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
|
42
|
+
# @return [String]
|
43
|
+
def to_xml(xml)
|
44
|
+
xml.send('c:title') {
|
45
|
+
xml.send('c:tx') {
|
46
|
+
xml.send('c:strRef') {
|
47
|
+
xml.send('c:f', range)
|
48
|
+
xml.send('c:strCache') {
|
49
|
+
xml.send('c:ptCount', :val=>1)
|
50
|
+
xml.send('c:pt', :idx=>0) {
|
51
|
+
xml.send('c:v', @text)
|
52
|
+
}
|
53
|
+
}
|
54
|
+
}
|
55
|
+
}
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
# returns the excel style abslute reference for the title when title is a Cell object
|
62
|
+
# @return [String]
|
63
|
+
def range
|
64
|
+
return "" unless @data.is_a?(Cell)
|
65
|
+
"#{@data.row.worksheet.name}!#{data.row.r_abs}"
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module Axlsx
|
2
|
+
# This class details the anchor points for drawings.
|
3
|
+
# @note The recommended way to manage drawings and charts is Worksheet#add_chart. Anchors are specified by the :start_at and :end_at options to that method.
|
4
|
+
# @see Worksheet#add_chart
|
5
|
+
class TwoCellAnchor
|
6
|
+
|
7
|
+
# A marker that defines the from cell anchor. The default from column and row are 0 and 0 respectively
|
8
|
+
# @return [Marker]
|
9
|
+
attr_reader :from
|
10
|
+
# A marker that returns the to cell anchor. The default to column and row are 5 and 10 respectively
|
11
|
+
# @return [Marker]
|
12
|
+
attr_reader :to
|
13
|
+
|
14
|
+
# The frame for your chart
|
15
|
+
# @return [GraphicFrame]
|
16
|
+
attr_reader :graphic_frame
|
17
|
+
|
18
|
+
# Creates a new TwoCellAnchor object
|
19
|
+
# @param [Drawing] drawing
|
20
|
+
# @param [Chart] chart
|
21
|
+
# @option options [Array] start_at
|
22
|
+
# @option options [Array] end_at
|
23
|
+
def initialize(drawing, chart, options)
|
24
|
+
raise ArgumentError, "TwoCellAnchor.chart must be a Chart" unless chart.is_a? Chart
|
25
|
+
@from, @to = Marker.new, Marker.new(:col => 5, :row=>10)
|
26
|
+
@graphic_frame = GraphicFrame.new(chart)
|
27
|
+
# BUG? what happens if :start_at is not defined?
|
28
|
+
self.start_at(options[:start_at][0], options[:start_at][1]) if options[:start_at].is_a?(Array)
|
29
|
+
self.end_at(options[:end_at][0], options[:end_at][1]) if options[:end_at].is_a?(Array)
|
30
|
+
drawing.anchors << self
|
31
|
+
end
|
32
|
+
|
33
|
+
# This is a short cut method to set the start anchor position
|
34
|
+
# @param [Integer] x The column
|
35
|
+
# @param [Integer] y The row
|
36
|
+
# @return [Marker]
|
37
|
+
def start_at(x, y)
|
38
|
+
@from.col = x
|
39
|
+
@from.row = y
|
40
|
+
@from
|
41
|
+
end
|
42
|
+
|
43
|
+
# This is a short cut method to set the end anchor position
|
44
|
+
# @param [Integer] x The column
|
45
|
+
# @param [Integer] y The row
|
46
|
+
# @return [Marker]
|
47
|
+
def end_at(x, y)
|
48
|
+
@to.col = x
|
49
|
+
@to.row = y
|
50
|
+
@to
|
51
|
+
end
|
52
|
+
|
53
|
+
# Serializes the two cell anchor
|
54
|
+
# @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
|
55
|
+
# @return [String]
|
56
|
+
def to_xml(xml)
|
57
|
+
#build it for now, break it down later!
|
58
|
+
xml.send('xdr:twoCellAnchor') {
|
59
|
+
xml.send('xdr:from') {
|
60
|
+
from.to_xml(xml)
|
61
|
+
}
|
62
|
+
xml.send('xdr:to') {
|
63
|
+
to.to_xml(xml)
|
64
|
+
}
|
65
|
+
@graphic_frame.to_xml(xml)
|
66
|
+
xml.send('xdr:clientData')
|
67
|
+
}
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Axlsx
|
2
|
+
# the ValAxis class defines a chart value axis.
|
3
|
+
class ValAxis < Axis
|
4
|
+
|
5
|
+
# This element specifies whether the value axis crosses the category axis between categories.
|
6
|
+
# must be one of [:between, :midCat]
|
7
|
+
# @return [Symbol]
|
8
|
+
attr_accessor :crossesBetween
|
9
|
+
|
10
|
+
# Creates a new ValAxis object
|
11
|
+
# @param [Integer] axId the id of this axis
|
12
|
+
# @param [Integer] crossAx the id of the perpendicular axis
|
13
|
+
# @option options [Symbol] axPos
|
14
|
+
# @option options [Symbol] crosses
|
15
|
+
# @option options [Symbol] tickLblPos
|
16
|
+
# @option options [Symbol] crossesBetween
|
17
|
+
def initialize(axId, crossAx, options={})
|
18
|
+
super(axId, crossAx, options)
|
19
|
+
@crossesBetween = :between
|
20
|
+
end
|
21
|
+
|
22
|
+
# Serializes the value axis
|
23
|
+
# @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
|
24
|
+
# @return [String]
|
25
|
+
def to_xml(xml)
|
26
|
+
xml.send('c:valAx') {
|
27
|
+
super(xml)
|
28
|
+
xml.send('c:axId', :val=>@axId)
|
29
|
+
@scaling.to_xml(xml)
|
30
|
+
xml.send('c:crossesBetween', :val=>@crossesBetween)
|
31
|
+
}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Axlsx
|
2
|
+
|
3
|
+
|
4
|
+
attr_accessor :rotX
|
5
|
+
attr_accessor :hPercent
|
6
|
+
attr_accessor :rotY
|
7
|
+
attr_accessor :depthPercent
|
8
|
+
attr_accessor :rAngAx
|
9
|
+
attr_accessor :perspective
|
10
|
+
|
11
|
+
def initialize(options={})
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
def rotX=(v) DataTypeValidator.validate "#{self.class}.rotX", [Integer, Fixnum], v, lambda {|v| v >= -90 && v <= 90 }; @rotX = v; end
|
16
|
+
|
17
|
+
def to_xml(xml)
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/lib/axlsx/package.rb
CHANGED
@@ -24,12 +24,11 @@ module Axlsx
|
|
24
24
|
#
|
25
25
|
# @param [Hash] options A hash that you can use to specify the author and workbook for this package.
|
26
26
|
# @option options [String] :author The author of the document
|
27
|
-
# @option options [Workbook] :workbook The workbook associated with this package.
|
28
27
|
# @example Package.new :author => 'you!', :workbook => Workbook.new
|
29
28
|
def initialize(options={})
|
30
29
|
@core, @app = Core.new, App.new
|
31
30
|
@core.creator = options[:author] || @core.creator
|
32
|
-
self
|
31
|
+
yield self if block_given?
|
33
32
|
end
|
34
33
|
|
35
34
|
def workbook=(workbook) DataTypeValidator.validate "Package.workbook", Workbook, workbook; @workbook = workbook; end
|
@@ -55,7 +54,8 @@ module Axlsx
|
|
55
54
|
# # You will find a file called test.xlsx
|
56
55
|
def serialize(output, confirm_valid=false)
|
57
56
|
return false unless !confirm_valid || self.validate.empty?
|
58
|
-
|
57
|
+
f = File.new(output, "w")
|
58
|
+
Zip::ZipOutputStream.open(f.path) do |zip|
|
59
59
|
parts.each{ |part| zip.put_next_entry(part[:entry]); zip.puts(part[:doc]) }
|
60
60
|
end
|
61
61
|
true
|
@@ -0,0 +1,181 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# Create Office Open XML Spreadsheets (xlsx) with safe and full control over cell styles, automatically resized column widths and 3D pie charts.
|
3
|
+
module Axlsx
|
4
|
+
# Package is responsible for managing all the bits and peices that Open Office XML requires to make a valid
|
5
|
+
# xlsx document including valdation and serialization.
|
6
|
+
class Package
|
7
|
+
|
8
|
+
# The workbook this package will serialize or validate.
|
9
|
+
# @attribute
|
10
|
+
# @return [Workbook] If no workbook instance has been assigned with this package a new Workbook instance is returned.
|
11
|
+
# @raise ArgumentError if workbook parameter is not a Workbook instance.
|
12
|
+
# @note As there are multiple ways to instantiate a workbook for the package,
|
13
|
+
# here are a few examples:
|
14
|
+
# # assign directly during package instanciation
|
15
|
+
# wb = Package.new(:workbook => Workbook.new).workbook
|
16
|
+
#
|
17
|
+
# # get a fresh workbook automatically from the package
|
18
|
+
# wb = Pacakge.new().workbook
|
19
|
+
# # # set the workbook after creating the package
|
20
|
+
# wb = Package.new().workbook = Workbook.new
|
21
|
+
attr_accessor :workbook
|
22
|
+
|
23
|
+
# Initializes your package
|
24
|
+
#
|
25
|
+
# @param [Hash] options A hash that you can use to specify the author and workbook for this package.
|
26
|
+
# @option options [String] :author The author of the document
|
27
|
+
# @option options [Workbook] :workbook The workbook associated with this package.
|
28
|
+
# @example Package.new :author => 'you!', :workbook => Workbook.new
|
29
|
+
def initialize(options={})
|
30
|
+
@core, @app = Core.new, App.new
|
31
|
+
@core.creator = options[:author] || @core.creator
|
32
|
+
self.workbook= options[:workbook] if options[:workbook]
|
33
|
+
end
|
34
|
+
|
35
|
+
def workbook=(workbook) DataTypeValidator.validate "Package.workbook", Workbook, workbook; @workbook = workbook; end
|
36
|
+
|
37
|
+
def workbook
|
38
|
+
@workbook || @workbook = Workbook.new
|
39
|
+
end
|
40
|
+
|
41
|
+
# Serialize your workbook to disk as an xlsx document.
|
42
|
+
#
|
43
|
+
# @param [File] output The file you want to serialize your package to
|
44
|
+
# @param [Boolean] confirm_valid Validate the package prior to serialization.
|
45
|
+
# @return [Boolean] False if confirm_valid and validation errors exist. True if the package was serialized
|
46
|
+
# @note A tremendous amount of effort has gone into ensuring that you cannot create invalid xlsx documents.
|
47
|
+
# confirm_valid should be used in the rare case that you cannot open the serialized file.
|
48
|
+
# @see Package#validate
|
49
|
+
# @example
|
50
|
+
# # This is how easy it is to create a valid xlsx file. Of course you might want to add a sheet or two, and maybe some data, styles and charts.
|
51
|
+
# # Take a look at the README for an example of how to do it!
|
52
|
+
# f = File.open('test.xlsx', 'w')
|
53
|
+
# Package.new.serialize(f)
|
54
|
+
#
|
55
|
+
# # You will find a file called test.xlsx
|
56
|
+
def serialize(output, confirm_valid=false)
|
57
|
+
return false unless !confirm_valid || self.validate.empty?
|
58
|
+
Zip::ZipOutputStream.open(output.path) do |zip|
|
59
|
+
parts.each{ |part| zip.put_next_entry(part[:entry]); zip.puts(part[:doc]) }
|
60
|
+
end
|
61
|
+
true
|
62
|
+
end
|
63
|
+
|
64
|
+
# Validate all parts of the package against xsd schema.
|
65
|
+
# @return [Array] An array of all validation errors found.
|
66
|
+
# @note This gem includes all schema from OfficeOpenXML-XMLSchema-Transitional.zip and OpenPackagingConventions-XMLSchema.zip
|
67
|
+
# as per ECMA-376, Third edition. opc schema require an internet connection to import remote schema from dublin core for dc,
|
68
|
+
# dcterms and xml namespaces. Those remote schema are included in this gem, and the original files have been altered to
|
69
|
+
# refer to the local versions.
|
70
|
+
#
|
71
|
+
# If by chance you are able to creat a package that does not validate it indicates that the internal
|
72
|
+
# validation is not robust enough and needs to be improved. Please report your errors to the gem author.
|
73
|
+
# @see http://www.ecma-international.org/publications/standards/Ecma-376.htm
|
74
|
+
# @example
|
75
|
+
# # The following will output any error messages found in serialization.
|
76
|
+
# p = Axlsx::Package.new
|
77
|
+
# # ... code to create sheets, charts, styles etc.
|
78
|
+
# p.validate.each { |error| puts error.message }
|
79
|
+
def validate
|
80
|
+
errors = []
|
81
|
+
parts.each { |part| errors.concat validate_single_doc(part[:schema], part[:doc]) }
|
82
|
+
errors
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
# The parts of a package
|
88
|
+
# @return [Array] An array of hashes that define the entry, document and schema for each part of the package.
|
89
|
+
# @private
|
90
|
+
def parts
|
91
|
+
@parts = [
|
92
|
+
{:entry => RELS_PN, :doc => relationships.to_xml, :schema => RELS_XSD},
|
93
|
+
{:entry => CORE_PN, :doc => @core.to_xml, :schema => CORE_XSD},
|
94
|
+
{:entry => APP_PN, :doc => @app.to_xml, :schema => APP_XSD},
|
95
|
+
{:entry => WORKBOOK_RELS_PN, :doc => workbook.relationships.to_xml, :schema => RELS_XSD},
|
96
|
+
{:entry => WORKBOOK_PN, :doc => workbook.to_xml, :schema => SML_XSD},
|
97
|
+
{:entry => CONTENT_TYPES_PN, :doc => content_types.to_xml, :schema => CONTENT_TYPES_XSD},
|
98
|
+
{:entry => "xl/#{STYLES_PN}", :doc => workbook.styles.to_xml, :schema => SML_XSD}
|
99
|
+
]
|
100
|
+
workbook.drawings.each do |drawing|
|
101
|
+
@parts << {:entry => "xl/#{drawing.rels_pn}", :doc => drawing.relationships.to_xml, :schema => RELS_XSD}
|
102
|
+
@parts << {:entry => "xl/#{drawing.pn}", :doc => drawing.to_xml, :schema => DRAWING_XSD}
|
103
|
+
end
|
104
|
+
|
105
|
+
workbook.charts.each do |chart|
|
106
|
+
@parts << {:entry => "xl/#{chart.pn}", :doc => chart.to_xml, :schema => DRAWING_XSD}
|
107
|
+
end
|
108
|
+
|
109
|
+
workbook.worksheets.each do |sheet|
|
110
|
+
@parts << {:entry => "xl/#{sheet.rels_pn}", :doc => sheet.relationships.to_xml, :schema => RELS_XSD}
|
111
|
+
@parts << {:entry => "xl/#{sheet.pn}", :doc => sheet.to_xml, :schema => SML_XSD}
|
112
|
+
end
|
113
|
+
@parts
|
114
|
+
end
|
115
|
+
|
116
|
+
# Performs xsd validation for a signle document
|
117
|
+
#
|
118
|
+
# @param [String] schema path to the xsd schema to be used in validation.
|
119
|
+
# @param [String] doc The xml text to be validated
|
120
|
+
# @return [Array] An array of all validation errors encountered.
|
121
|
+
# @private
|
122
|
+
def validate_single_doc(schema, doc)
|
123
|
+
schema = Nokogiri::XML::Schema(File.open(schema))
|
124
|
+
doc = Nokogiri::XML(doc)
|
125
|
+
|
126
|
+
errors = []
|
127
|
+
schema.validate(doc).each do |error|
|
128
|
+
errors << error
|
129
|
+
end
|
130
|
+
errors
|
131
|
+
end
|
132
|
+
|
133
|
+
# Appends override objects for drawings, charts, and sheets as they exist in your workbook to the default content types.
|
134
|
+
# @return [ContentType]
|
135
|
+
# @private
|
136
|
+
def content_types
|
137
|
+
c_types = base_content_types
|
138
|
+
workbook.drawings.each do |drawing|
|
139
|
+
c_types << Axlsx::Override.new(:PartName => "/xl/#{drawing.pn}",
|
140
|
+
:ContentType => DRAWING_CT)
|
141
|
+
end
|
142
|
+
workbook.charts.each do |chart|
|
143
|
+
c_types << Axlsx::Override.new(:PartName => "/xl/#{chart.pn}",
|
144
|
+
:ContentType => CHART_CT)
|
145
|
+
end
|
146
|
+
workbook.worksheets.each do |sheet|
|
147
|
+
c_types << Axlsx::Override.new(:PartName => "/xl/#{sheet.pn}",
|
148
|
+
:ContentType => WORKSHEET_CT)
|
149
|
+
end
|
150
|
+
c_types
|
151
|
+
end
|
152
|
+
|
153
|
+
# Creates the minimum content types for generating a valid xlsx document.
|
154
|
+
# @return [ContentType]
|
155
|
+
# @private
|
156
|
+
def base_content_types
|
157
|
+
c_types = ContentType.new()
|
158
|
+
c_types << Default.new(:ContentType => RELS_CT, :Extension => RELS_EX)
|
159
|
+
c_types << Default.new(:Extension => XML_EX, :ContentType => XML_CT)
|
160
|
+
c_types << Override.new(:PartName => "/#{APP_PN}", :ContentType => APP_CT)
|
161
|
+
c_types << Override.new(:PartName => "/#{CORE_PN}", :ContentType => CORE_CT)
|
162
|
+
c_types << Override.new(:PartName => "/xl/#{STYLES_PN}", :ContentType => STYLES_CT)
|
163
|
+
c_types << Axlsx::Override.new(:PartName => "/#{WORKBOOK_PN}", :ContentType => WORKBOOK_CT)
|
164
|
+
c_types.lock
|
165
|
+
c_types
|
166
|
+
end
|
167
|
+
|
168
|
+
# Creates the relationships required for a valid xlsx document
|
169
|
+
# @return [Relationships]
|
170
|
+
# @private
|
171
|
+
def relationships
|
172
|
+
rels = Axlsx::Relationships.new
|
173
|
+
rels << Relationship.new(WORKBOOK_R, WORKBOOK_PN)
|
174
|
+
rels << Relationship.new(CORE_R, CORE_PN)
|
175
|
+
rels << Relationship.new(APP_R, APP_PN)
|
176
|
+
rels.lock
|
177
|
+
rels
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|