class BerkeleyLibrary::Util::ODS::Spreadsheet

Public Instance Methods

add_table(name, protected: true) click to toggle source

Adds a table ('worksheet') to the spreadsheet.

@param name [String] the table name @param protected [Boolean] whether to protect the table @return [BerkeleyLibrary::Util::ODS::XML::Table::Table] a new table with the specified name

# File lib/berkeley_library/util/ods/spreadsheet.rb, line 22
def add_table(name, protected: true)
  content.document_content.add_table(name, protected: protected)
end
auto_styles() click to toggle source

Gets the document styles

@return [BerkeleyLibrary::Util::ODS::XML::Office::AutomaticStyles] the styles

# File lib/berkeley_library/util/ods/spreadsheet.rb, line 53
def auto_styles
  content.document_content.automatic_styles
end
content() click to toggle source

Returns the content document @return [XML::ContentDoc] the container root-level content document

# File lib/berkeley_library/util/ods/spreadsheet.rb, line 31
def content
  @content ||= XML::ContentDoc.new
end
manifest() click to toggle source

Returns the container manifest @return [XML::ManifestDoc] the container manifest document

# File lib/berkeley_library/util/ods/spreadsheet.rb, line 43
def manifest
  @manifest ||= XML::ManifestDoc.new.tap do |mf_doc|
    manifest = mf_doc.manifest
    manifest_docs.each { |doc| manifest.add_entry_for(doc) }
  end
end
styles() click to toggle source

Returns the container styles @return [XML::StylesDoc] the container root-level style document

# File lib/berkeley_library/util/ods/spreadsheet.rb, line 37
def styles
  @styles ||= XML::StylesDoc.new
end
write_exploded_to(dir) click to toggle source

Writes this spreadsheet as an exploded set of pretty-printed XML files. NOTE: OpenOffice itself and many other tools get confused by the extra text nodes in the pretty-printed files and won't read them properly; this method is mostly for debugging.

@return [Array<String>] a list of files written.

# File lib/berkeley_library/util/ods/spreadsheet.rb, line 112
def write_exploded_to(dir)
  raise ArgumentError, "Not a directory: #{dir.inspect}" unless File.directory?(dir)

  [].tap do |files_written|
    each_document do |doc|
      output_path = write_exploded(doc, dir)
      files_written << File.absolute_path(output_path)
      logger.debug("Wrote #{files_written.last}")
    end
  end
end
write_to(out = nil) click to toggle source

@overload write_to

Writes to a new string.
@return [String] a binary string containing the spreadsheet data.

@overload write_to(out)

Writes to the specified output stream.
@param out [IO] the output stream
@return[void]

@overload write_to(path)

Writes to the specified file. If `path` denotes a directory, the
spreadsheet will be written as exploded, pretty-printed XML.
@param path [String, Pathname] the path to the output file
@return[void]
@see BerkeleyLibrary::Util::ODS::Spreadsheet#write_exploded_to

noinspection RubyYardReturnMatch

# File lib/berkeley_library/util/ods/spreadsheet.rb, line 74
def write_to(out = nil)
  return write_to_string unless out
  return write_to_stream(out) if io_like?(out)
  return write_exploded_to(out) if File.directory?(out)

  write_to_file(out)
end
write_to_file(path) click to toggle source

Writes to the specified file. @param path [String, Pathname]

# File lib/berkeley_library/util/ods/spreadsheet.rb, line 102
def write_to_file(path)
  File.open(path, 'wb') { |f| write_to_stream(f) }
end
write_to_stream(out) click to toggle source

Writes to the specified output stream. @param out [IO]

# File lib/berkeley_library/util/ods/spreadsheet.rb, line 90
def write_to_stream(out)
  zip64_orig = Zip.write_zip64_support
  begin
    Zip.write_zip64_support = true
    write_zipfile(out)
  ensure
    Zip.write_zip64_support = zip64_orig
  end
end
write_to_string() click to toggle source

Writes to a new string.

# File lib/berkeley_library/util/ods/spreadsheet.rb, line 83
def write_to_string
  # noinspection RubyYardParamTypeMatch
  StringIO.new.tap { |out| write_to_stream(out) }.string
end

Private Instance Methods

each_document() { |manifest| ... } click to toggle source

Private methods

# File lib/berkeley_library/util/ods/spreadsheet.rb, line 129
def each_document(&block)
  yield manifest

  manifest_docs.each(&block)
end
io_like?(out) click to toggle source

Returns true if `out` is IO-like enough for {Zip::OutputStream}, false otherwise @return [Boolean] whether `out` can be passed to {Zip::OutputStream#write_buffer}

# File lib/berkeley_library/util/ods/spreadsheet.rb, line 141
def io_like?(out)
  %i[reopen rewind <<].all? { |m| out.respond_to?(m) }
end
manifest_docs() click to toggle source
# File lib/berkeley_library/util/ods/spreadsheet.rb, line 135
def manifest_docs
  [styles, content]
end
write_exploded(doc, dir) click to toggle source
# File lib/berkeley_library/util/ods/spreadsheet.rb, line 160
def write_exploded(doc, dir)
  output_path = File.join(dir, doc.path)
  FileUtils.mkdir_p(File.dirname(output_path))
  doc.to_xml(output_path, compact: false)
  output_path
end
write_zip_entry(doc, zip) click to toggle source
# File lib/berkeley_library/util/ods/spreadsheet.rb, line 155
def write_zip_entry(doc, zip)
  zip.put_next_entry(doc.path)
  doc.to_xml(zip)
end
write_zipfile(out) click to toggle source
# File lib/berkeley_library/util/ods/spreadsheet.rb, line 145
def write_zipfile(out)
  io = Zip::OutputStream.write_buffer(out) do |zip|
    each_document { |doc| write_zip_entry(doc, zip) }
  end
  # NOTE: Zip::OutputStream plays games with the stream and
  #   doesn't necessarily write everything unless flushed, see:
  #   https://github.com/rubyzip/rubyzip/issues/265
  io.flush
end