Sha256: 660922d275fc4e4ef207307d5a3d0eb0355848adb34232dbfc0d18966ec318a0

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

require "roo"
require "roo-xls"
require "digest/sha1"

module ChupaText
  module Decomposers
    class Spreadsheet < Decomposer
      registry.register("spreadsheet", self)

      TARGET_EXTENSIONS = ["ods", "xls", "xlsx", "xlsm", "xml"]

      TARGET_MIME_TYPES = [
        "application/vnd.oasis.opendocument.spreadsheet",
        "application/vnd.ms-excel",
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
      ]

      def target?(data)
        TARGET_EXTENSIONS.include?(data.extension) || TARGET_MIME_TYPES.include?(data.mime_type)
      end

      def decompose(data)
        book = Roo::Spreadsheet.open(data.uri)
        book.sheets.each do |sheet_name|
          sheet = book.sheet(sheet_name)
          body = sheet.to_csv
          text_data = TextData.new(body, source_data: data)
          text_data["name"] = sheet_name
          text_data["digest"] = Digest::SHA1.hexdigest(body)
          text_data["size"] = body.bytesize
          text_data["first-row"] = sheet.first_row
          text_data["last-row"] = sheet.last_row
          text_data["first-column"] = sheet.first_column && sheet.first_column_as_letter
          text_data["last-column"] = sheet.last_column && sheet.last_column_as_letter
          yield text_data
        end
        book.close
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chupa-text-decomposer-spreadsheet-1.0.1 lib/chupa-text/decomposers/spreadsheet.rb