lib/roo/generic_spreadsheet.rb in roo-1.9.3 vs lib/roo/generic_spreadsheet.rb in roo-1.9.4

- old
+ new

@@ -1,22 +1,38 @@ # encoding: utf-8 require 'matrix' -require 'rubygems' -require 'builder' # Base class for all other types of spreadsheets class GenericSpreadsheet attr_reader :default_sheet # sets the line with attribute names (default: 1) attr_accessor :header_line - def initialize + protected + + # Helper function for development + def fremdrechner? #nodoc + eigener = [ + 'C:\Users\thopre', + 'c:/Users/thopre', + '/c/Users/thopre', + '/home/tp', + ].include? ENV['HOME'] + # if eigener + # puts "fremdrechner? ==> false" + # else + # puts "fremdrechner? ==> true" + # end + ! eigener end - # set the working sheet in the document + public + + # sets the working sheet in the document + # 'sheet' can be a number (0 = first sheet) or the name of a sheet. def default_sheet=(sheet) if sheet.kind_of? Fixnum if sheet >= 0 and sheet <= sheets.length sheet = self.sheets[sheet-1] else @@ -298,14 +314,15 @@ result end # reopens and read a spreadsheet document def reload + # von Abfrage der Klasse direkt auf .to_s == '..' umgestellt ds = @default_sheet - initialize(@filename) if self.class == Openoffice or - self.class == Excel - initialize(@spreadsheetkey,@user,@password) if self.class == Google + initialize(@filename) if self.class.to_s == 'Openoffice' or + self.class.to_s == 'Excel' + initialize(@spreadsheetkey,@user,@password) if self.class.to_s == 'Google' self.default_sheet = ds #@first_row = @last_row = @first_column = @last_column = nil end # true if cell is empty @@ -320,11 +337,17 @@ end # recursively removes the current temporary directory # this is only needed if you work with zipped files or files via the web def remove_tmp + #to do + # "remove_tmp wieder aktivieren" + #end + #return + #$log.debug("remove_tmp('#{@tmpdir}')") if File.exists?(@tmpdir) + #$log.debug("#{@tmpdir} exists") FileUtils::rm_r(@tmpdir) end end # Returns information of the spreadsheet document and all sheets within @@ -350,34 +373,33 @@ } result end def to_xml - xml_document = '' - xml = Builder::XmlMarkup.new(:target => xml_document, :indent => 2) - xml.instruct! :xml, :version =>"1.0", :encoding => "utf-8" - xml.spreadsheet { - self.sheets.each do |sheet| - self.default_sheet = sheet - xml.sheet(:name => sheet) { |x| - if first_row and last_row and first_column and last_column - # sonst gibt es Fehler bei leeren Blaettern - first_row.upto(last_row) do |row| - first_column.upto(last_column) do |col| - unless empty?(row,col) - x.cell(cell(row,col), - :row =>row, - :column => col, - :type => celltype(row,col)) + builder = Nokogiri::XML::Builder.new do |xml| + xml.spreadsheet { + self.sheets.each do |sheet| + self.default_sheet = sheet + xml.sheet(:name => sheet) { |x| + if first_row and last_row and first_column and last_column + # sonst gibt es Fehler bei leeren Blaettern + first_row.upto(last_row) do |row| + first_column.upto(last_column) do |col| + unless empty?(row,col) + x.cell(cell(row,col), + :row =>row, + :column => col, + :type => celltype(row,col)) + end end end end - end - } - end - } - xml_document + } + end + } + end + return builder.to_xml end # when a method like spreadsheet.a42 is called # convert it to a call of spreadsheet.cell('a',42) def method_missing(m, *args) @@ -389,22 +411,13 @@ if args.size > 0 return cell(row,col,args[0]) else return cell(row,col) end - # else - # geht noch nicht, weil label unterhalb (in Openoffice) dieser Klasse - # es definiert ist - # p "Label #{m} angesprochen?" - # row,col,sheet = label('anton') - # # row,col,sheet = label(m) - # p "row: #{row}" - # p "col: #{col}" - # p "sheet: #{sheet}" - # return cell(row,col) + else + super end - raise ArgumentError, "Method #{m} missing. Args: #{args}" end protected def file_type_check(filename, ext, name) @@ -468,42 +481,9 @@ col = GenericSpreadsheet.letter_to_number(col) end return row,col end - # def open_from_uri(uri) - # require 'open-uri' ; - # tempfilename = File.join(@tmpdir, File.basename(uri)) - # f = File.open(tempfilename,"wb") - # begin - # open(uri) do |net| - # f.write(net.read) - # end - # rescue - # raise "could not open #{uri}" - # end - # f.close - # File.join(@tmpdir, File.basename(uri)) - # end - - # OpenURI::HTTPError - # def open_from_uri(uri) - # require 'open-uri' - # #existiert URL? - # r = Net::HTTP.get_response(URI.parse(uri)) - # raise "URL nicht verfuegbar" unless r.is_a? Net::HTTPOK - # tempfilename = File.join(@tmpdir, File.basename(uri)) - # f = File.open(tempfilename,"wb") - # open(uri) do |net| - # f.write(net.read) - # end - # # rescue - # # raise "could not open #{uri}" - # # end - # f.close - # File.join(@tmpdir, File.basename(uri)) - # end - def open_from_uri(uri) require 'open-uri' response = '' begin open(uri, "User-Agent" => "Ruby/#{RUBY_VERSION}") { |net|