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 Attribute Summary (collapse)
-
- (Object) app
readonly
provides access to the app doc properties for this package see App.
-
- (Object) core
readonly
provides access to the core doc properties for the package see Core.
Instance Method Summary (collapse)
-
- (Object) encrypt(file_name, password)
Encrypt the package into a CFB using the password provided This is not ready yet.
-
- (Package) initialize(options = {}) {|_self| ... }
constructor
Initializes your package.
-
- (Boolean) serialize(output, confirm_valid = false)
Serialize your workbook to disk as an xlsx document.
-
- (StringIO|Boolean) to_stream(confirm_valid = false)
Serialize your workbook to a StringIO instance.
-
- (Object) use_autowidth=(v)
Shortcut to specify that the workbook should use autowidth.
-
- (Object) use_shared_strings
Shortcut to determine if the workbook is configured to use shared strings.
-
- (Object) use_shared_strings=(v)
Shortcut to specify that the workbook should use shared strings.
-
- (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
22 23 24 25 26 27 28 29 30 |
# File 'lib/axlsx/package.rb', line 22 def initialize(={}) @workbook = nil @core, @app = Core.new, App.new @core.creator = [:author] || @core.creator .each do |o| self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end yield self if block_given? end |
Instance Attribute Details
- (Object) app (readonly)
provides access to the app doc properties for this package see App
10 11 12 |
# File 'lib/axlsx/package.rb', line 10 def app @app end |
- (Object) core (readonly)
provides access to the core doc properties for the package see Core
14 15 16 |
# File 'lib/axlsx/package.rb', line 14 def core @core end |
Instance Method Details
- (Object) encrypt(file_name, password)
Encrypt the package into a CFB using the password provided This is not ready yet
119 120 121 122 123 |
# File 'lib/axlsx/package.rb', line 119 def encrypt(file_name, password) return false # moc = MsOffCrypto.new(file_name, password) # moc.save 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.
97 98 99 100 101 102 103 |
# File 'lib/axlsx/package.rb', line 97 def serialize(output, confirm_valid=false) return false unless !confirm_valid || self.validate.empty? Zip::ZipOutputStream.open(output) do |zip| write_parts(zip) end true end |
- (StringIO|Boolean) to_stream(confirm_valid = false)
Serialize your workbook to a StringIO instance
109 110 111 112 113 114 115 |
# File 'lib/axlsx/package.rb', line 109 def to_stream(confirm_valid=false) return false unless !confirm_valid || self.validate.empty? zip = write_parts(Zip::ZipOutputStream.new("streamed", true)) stream = zip.close_buffer stream.rewind stream end |
- (Object) use_autowidth=(v)
Shortcut to specify that the workbook should use autowidth
34 35 36 37 |
# File 'lib/axlsx/package.rb', line 34 def use_autowidth=(v) Axlsx::validate_boolean(v); workbook.use_autowidth = v end |
- (Object) use_shared_strings
Shortcut to determine if the workbook is configured to use shared strings
49 50 51 |
# File 'lib/axlsx/package.rb', line 49 def use_shared_strings workbook.use_shared_strings end |
- (Object) use_shared_strings=(v)
Shortcut to specify that the workbook should use shared strings
42 43 44 45 |
# File 'lib/axlsx/package.rb', line 42 def use_shared_strings=(v) Axlsx::validate_boolean(v); workbook.use_shared_strings = v 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.
140 141 142 143 144 145 146 |
# File 'lib/axlsx/package.rb', line 140 def validate errors = [] parts.each do |part| errors.concat validate_single_doc(part[:schema], part[:doc]) unless part[:schema].nil? end 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.
65 66 67 68 69 |
# File 'lib/axlsx/package.rb', line 65 def workbook @workbook || @workbook = Workbook.new yield @workbook if block_given? @workbook end |
- (Object) workbook=(workbook)
79 |
# File 'lib/axlsx/package.rb', line 79 def workbook=(workbook) DataTypeValidator.validate "Package.workbook", Workbook, workbook; @workbook = workbook; end |