Class: Axlsx::Package
- Inherits:
-
Object
- Object
- Axlsx::Package
- Defined in:
- lib/axlsx/package.rb
Overview
Package is responsible for managing all the bits and peices that Open Office XML requires to make a valid xlsx document including valdation and serialization.
Instance Method Summary (collapse)
-
- (Package) initialize(options = {}) {|_self| ... }
constructor
Initializes your package.
-
- (Object) ruport_table(table)
Accepts a ruport table for serialization to xlsx.
-
- (Boolean) serialize(output, confirm_valid = false)
Serialize your workbook to disk as an xlsx document.
-
- (Array) validate
Validate all parts of the package against xsd schema.
-
- (Workbook) workbook {|@workbook| ... }
The workbook this package will serialize or validate.
- - (Object) workbook=(workbook)
Constructor Details
- (Package) initialize(options = {}) {|_self| ... }
Initializes your package
12 13 14 15 16 17 |
# File 'lib/axlsx/package.rb', line 12 def initialize(={}) @workbook = nil @core, @app = Core.new, App.new @core.creator = [:author] || @core.creator yield self if block_given? end |
Instance Method Details
- (Object) ruport_table(table)
Accepts a ruport table for serialization to xlsx
21 22 23 |
# File 'lib/axlsx/package.rb', line 21 def ruport_table(table) puts table end |
- (Boolean) serialize(output, confirm_valid = false)
A tremendous amount of effort has gone into ensuring that you cannot create invalid xlsx documents. confirm_valid should be used in the rare case that you cannot open the serialized file.
Serialize your workbook to disk as an xlsx document.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/axlsx/package.rb', line 61 def serialize(output, confirm_valid=false) return false unless !confirm_valid || self.validate.empty? p = parts Zip::ZipOutputStream.open(output) do |zip| p.each do |part| unless part[:doc].nil? zip.put_next_entry(part[:entry]); zip.puts(part[:doc]) end unless part[:path].nil? zip.put_next_entry(part[:entry]); # binread for 1.9.3 zip.write IO.respond_to?(:binread) ? IO.binread(part[:path]) : IO.read(part[:path]) end end end true end |
- (Array) validate
This gem includes all schema from OfficeOpenXML-XMLSchema-Transitional.zip and OpenPackagingConventions-XMLSchema.zip as per ECMA-376, Third edition. opc schema require an internet connection to import remote schema from dublin core for dc, dcterms and xml namespaces. Those remote schema are included in this gem, and the original files have been altered to refer to the local versions.
If by chance you are able to creat a package that does not validate it indicates that the internal validation is not robust enough and needs to be improved. Please report your errors to the gem author.
Validate all parts of the package against xsd schema.
95 96 97 98 99 |
# File 'lib/axlsx/package.rb', line 95 def validate errors = [] parts.each { |part| errors.concat validate_single_doc(part[:schema], part[:doc]) unless part[:schema].nil? } errors end |
- (Workbook) workbook {|@workbook| ... }
As there are multiple ways to instantiate a workbook for the package, here are a few examples:
# assign directly during package instanciation wb = Package.new(:workbook => Workbook.new).workbook # get a fresh workbook automatically from the package wb = Pacakge.new().workbook # # set the workbook after creating the package wb = Package.new().workbook = Workbook.new
The workbook this package will serialize or validate.
37 38 39 40 41 |
# File 'lib/axlsx/package.rb', line 37 def workbook @workbook || @workbook = Workbook.new yield @workbook if block_given? @workbook end |
- (Object) workbook=(workbook)
44 |
# File 'lib/axlsx/package.rb', line 44 def workbook=(workbook) DataTypeValidator.validate "Package.workbook", Workbook, workbook; @workbook = workbook end |