Class: MinimalistODS

Inherits:
Object
  • Object
show all
Defined in:
lib/minimalist_ods.rb

Defined Under Namespace

Classes: FileClosed, FileNotClosed, InvalidParameter, InvalidRowLength, MinimalistOODSError, TableAlreadyOpened, TableNotOpened

Constant Summary collapse

MIMETYPE =
'application/vnd.oasis.opendocument.spreadsheet'
META_TEMPLATE =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<office:document-meta xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"\n                      xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n                      xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n                      xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"\n                      xmlns:ooo=\"http://openoffice.org/2004/office\"\n                      xmlns:grddl=\"http://www.w3.org/2003/g/data-view#\"\n                      grddl:transformation=\"http://docs.oasis-open.org/office/1.2/xslt/odf2rdf.xsl\"\n                      office:version=\"1.2\">\n  <office:meta>\n    <meta:generator>ARMO</meta:generator>\n    <meta:initial-creator>:CREATOR</meta:initial-creator>\n    <dc:creator>:CREATOR</dc:creator>\n    <meta:creation-date>:TIME</meta:creation-date>\n    <dc:date>:TIME</dc:date>\n    <meta:editing-cycles>1</meta:editing-cycles>\n  </office:meta>\n</office:document-meta>\n"
MANIFEST_TEMPLATE =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\">\n  <manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.spreadsheet\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>\n  <manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>\n  <manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>\n</manifest:manifest>\n"
CONTENT_HEADER =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<office:document-content xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\" xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\" xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\" xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\" xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\" xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\" xmlns:presentation=\"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0\" xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\" xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\" xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\" xmlns:math=\"http://www.w3.org/1998/Math/MathML\" xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\" xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\" xmlns:ooo=\"http://openoffice.org/2004/office\" xmlns:ooow=\"http://openoffice.org/2004/writer\" xmlns:oooc=\"http://openoffice.org/2004/calc\" xmlns:dom=\"http://www.w3.org/2001/xml-events\" xmlns:xforms=\"http://www.w3.org/2002/xforms\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:rpt=\"http://openoffice.org/2005/report\" xmlns:of=\"urn:oasis:names:tc:opendocument:xmlns:of:1.2\" xmlns:xhtml=\"http://www.w3.org/1999/xhtml\" xmlns:grddl=\"http://www.w3.org/2003/g/data-view#\" xmlns:tableooo=\"http://openoffice.org/2009/table\" xmlns:drawooo=\"http://openoffice.org/2010/draw\" xmlns:calcext=\"urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0\" xmlns:loext=\"urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0\" xmlns:field=\"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0\" xmlns:formx=\"urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0\" xmlns:css3t=\"http://www.w3.org/TR/css3-text/\" office:version=\"1.2\">\n  <office:scripts/>\n  <office:font-face-decls>\n    <style:font-face style:name=\"Liberation Sans\" svg:font-family=\"&apos;Liberation Sans&apos;\" style:font-family-generic=\"swiss\" style:font-pitch=\"variable\"/>\n    <style:font-face style:name=\"Mangal\" svg:font-family=\"Mangal\" style:font-family-generic=\"system\" style:font-pitch=\"variable\"/>\n    <style:font-face style:name=\"Microsoft YaHei\" svg:font-family=\"&apos;Microsoft YaHei&apos;\" style:font-family-generic=\"system\" style:font-pitch=\"variable\"/>\n    <style:font-face style:name=\"Segoe UI\" svg:font-family=\"&apos;Segoe UI&apos;\" style:font-family-generic=\"system\" style:font-pitch=\"variable\"/>\n    <style:font-face style:name=\"Tahoma\" svg:font-family=\"Tahoma\" style:font-family-generic=\"system\" style:font-pitch=\"variable\"/>\n  </office:font-face-decls>\n  <office:automatic-styles>\n    <style:style style:name=\"co1\" style:family=\"table-column\">\n      <style:table-column-properties fo:break-before=\"auto\" style:column-width=\"22.58mm\"/>\n    </style:style>\n    <style:style style:name=\"ro1\" style:family=\"table-row\">\n      <style:table-row-properties style:row-height=\"4.52mm\" fo:break-before=\"auto\" style:use-optimal-row-height=\"true\"/>\n    </style:style>\n    <style:style style:name=\"ta1\" style:family=\"table\" style:master-page-name=\"Default\">\n      <style:table-properties table:display=\"true\" style:writing-mode=\"lr-tb\"/>\n    </style:style>\n  </office:automatic-styles>\n  <office:body>\n    <office:spreadsheet>\n      <table:calculation-settings table:automatic-find-labels=\"false\"/>\n"
"      <table:named-expressions/>\n    </office:spreadsheet>\n  </office:body>\n</office:document-content>\n"
TABLE_TEMPLATE =
"<table:table table:name=\":NAME\" table:style-name=\"ta1\">\n  <table:table-column table:style-name=\"co1\" table:number-columns-repeated=\":COL_NUMBER\" table:default-cell-style-name=\"Default\"/>\n"
ROW_TEMPLATE =
"<table:table-row table:style-name=\"ro1\">\n  :CELLS\n</table:table-row>\n"
NUMERIC_CELL_TEMPLATE =
"<table:table-cell office:value-type=\"float\" office:value=\":VALUE\">\n  <text:p>:VALUE</text:p>\n</table:table-cell>\n"
TEXT_CELL_TEMPLATE =
"<table:table-cell office:value-type=\"string\" calcext:value-type=\"string\">\n  <text:p>:VALUE</text:p>\n</table:table-cell>\n"
TABLE_OPEN =
0
TABLE_CLOSED =
1
FILE_CLOSED =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(save_as = nil, creator = 'minimalist-ods') ⇒ MinimalistODS

Returns a new instance of MinimalistODS.



140
141
142
143
144
145
146
# File 'lib/minimalist_ods.rb', line 140

def initialize(save_as = nil, creator = 'minimalist-ods')
  @save_as = save_as
  @creator = creator
  @save_to_disk = !(save_as.instance_of? StringIO)

  init_content
end

Instance Attribute Details

#content_bufferObject (readonly)

Returns the value of attribute content_buffer.



138
139
140
# File 'lib/minimalist_ods.rb', line 138

def content_buffer
  @content_buffer
end

#creatorObject (readonly)

Returns the value of attribute creator.



138
139
140
# File 'lib/minimalist_ods.rb', line 138

def creator
  @creator
end

#save_asObject (readonly)

Returns the value of attribute save_as.



138
139
140
# File 'lib/minimalist_ods.rb', line 138

def save_as
  @save_as
end

#save_to_diskObject (readonly)

Returns the value of attribute save_to_disk.



138
139
140
# File 'lib/minimalist_ods.rb', line 138

def save_to_disk
  @save_to_disk
end

#zip_bufferObject (readonly)

Returns the value of attribute zip_buffer.



138
139
140
# File 'lib/minimalist_ods.rb', line 138

def zip_buffer
  @zip_buffer
end

Instance Method Details

#add_row(row) ⇒ Object

Raises:



160
161
162
163
164
165
# File 'lib/minimalist_ods.rb', line 160

def add_row(row)
  raise InvalidRowLength.new(@cols_number, row.size) if row.size != @cols_number

  cells = row.map { |cell| cell_to_xml(cell) }.join
  content_buffer.write(ROW_TEMPLATE.gsub(':CELLS', cells))
end

#close_fileObject

Raises:



174
175
176
177
178
179
180
181
# File 'lib/minimalist_ods.rb', line 174

def close_file
  raise TableAlreadyOpened if @status == TABLE_OPEN

  content_buffer.write()
  @status = FILE_CLOSED
  create_zip
  write_file!
end

#close_tableObject

Raises:



167
168
169
170
171
172
# File 'lib/minimalist_ods.rb', line 167

def close_table
  raise TableNotOpened if @status == TABLE_CLOSED

  content_buffer.write('</table:table>')
  @status = TABLE_CLOSED
end

#open_table(table_name, cols_number) ⇒ Object

Raises:



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/minimalist_ods.rb', line 148

def open_table(table_name, cols_number)
  raise FileClosed if @status == FILE_CLOSED
  raise TableAlreadyOpened if @status == TABLE_OPEN
  raise InvalidParameter, "Got invalid value `#{cols_number}' for table size" if cols_number.nil? || !cols_number.is_a?(Integer) || !cols_number.positive?

  @cols_number = cols_number

  table_header = TABLE_TEMPLATE.gsub(':NAME', table_name).gsub(':COL_NUMBER', cols_number.to_s)
  content_buffer.write(table_header)
  @status = TABLE_OPEN
end