Class: Axlsx::Package
- Inherits:
-
Object
- Object
- Axlsx::Package
- Includes:
- OptionsParser
- 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
-
#app ⇒ Object
readonly
provides access to the app doc properties for this package see App.
-
#core ⇒ Object
readonly
provides access to the core doc properties for the package see Core.
Instance Method Summary collapse
-
#encrypt(file_name, password) ⇒ Object
Encrypt the package into a CFB using the password provided This is not ready yet.
-
#initialize(options = {}) {|_self| ... } ⇒ Package
constructor
Initializes your package.
-
#serialize(output, confirm_valid = false) ⇒ Boolean
Serialize your workbook to disk as an xlsx document.
-
#to_stream(confirm_valid = false) ⇒ StringIO|Boolean
Serialize your workbook to a StringIO instance.
-
#use_autowidth=(v) ⇒ Object
Shortcut to specify that the workbook should use autowidth.
-
#use_shared_strings ⇒ Object
Shortcut to determine if the workbook is configured to use shared strings.
-
#use_shared_strings=(v) ⇒ Object
Shortcut to specify that the workbook should use shared strings.
-
#validate ⇒ Array
Validate all parts of the package against xsd schema.
-
#workbook {|@workbook| ... } ⇒ Workbook
The workbook this package will serialize or validate.
- #workbook=(workbook) ⇒ Object
Methods included from OptionsParser
Constructor Details
#initialize(options = {}) {|_self| ... } ⇒ Package
Initializes your package
23 24 25 26 27 28 29 30 |
# File 'lib/axlsx/package.rb', line 23 def initialize(={}) @workbook = nil @core, @app = Core.new, App.new @core.creator = [:author] || @core.creator @core.created = [:created_at] yield self if block_given? end |
Instance Attribute Details
#app ⇒ Object (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 |
#core ⇒ Object (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
#encrypt(file_name, password) ⇒ Object
Encrypt the package into a CFB using the password provided This is not ready yet
125 126 127 128 129 |
# File 'lib/axlsx/package.rb', line 125 def encrypt(file_name, password) return false # moc = MsOffCrypto.new(file_name, password) # moc.save end |
#serialize(output, confirm_valid = false) ⇒ Boolean
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.
101 102 103 104 105 106 107 108 |
# File 'lib/axlsx/package.rb', line 101 def serialize(output, confirm_valid=false) return false unless !confirm_valid || self.validate.empty? Relationship.clear_cached_instances Zip::OutputStream.open(output) do |zip| write_parts(zip) end true end |
#to_stream(confirm_valid = false) ⇒ StringIO|Boolean
Serialize your workbook to a StringIO instance
114 115 116 117 118 119 120 121 |
# File 'lib/axlsx/package.rb', line 114 def to_stream(confirm_valid=false) return false unless !confirm_valid || self.validate.empty? Relationship.clear_cached_instances zip = write_parts(Zip::OutputStream.new(StringIO.new, true)) stream = zip.close_buffer stream.rewind stream end |
#use_autowidth=(v) ⇒ Object
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 |
#use_shared_strings ⇒ Object
Shortcut to determine if the workbook is configured to use shared strings
43 44 45 |
# File 'lib/axlsx/package.rb', line 43 def use_shared_strings workbook.use_shared_strings end |
#use_shared_strings=(v) ⇒ Object
Shortcut to specify that the workbook should use shared strings
49 50 51 52 |
# File 'lib/axlsx/package.rb', line 49 def use_shared_strings=(v) Axlsx::validate_boolean(v); workbook.use_shared_strings = v end |
#validate ⇒ Array
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.
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/axlsx/package.rb', line 146 def validate errors = [] parts.each do |part| unless part[:schema].nil? if part[:doc].is_a? String errors.concat validate_single_doc(part[:schema], part[:doc]) else errors.concat validate_single_doc(part[:schema], part[:doc].to_xml_string) end end 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 |
#workbook=(workbook) ⇒ Object
79 |
# File 'lib/axlsx/package.rb', line 79 def workbook=(workbook) DataTypeValidator.validate :Package_workbook, Workbook, workbook; @workbook = workbook; end |