lib/fast_excel.rb in fast_excel-0.2.5 vs lib/fast_excel.rb in fast_excel-0.2.6

- old
+ new

@@ -1,7 +1,11 @@ require_relative './fast_excel/binding' +require 'set' +# not used for now +#require_relative '../ext/fast_excel/text_width_ext' + module FastExcel class Formula attr_accessor :fml def initialize(fml) @@ -322,10 +326,11 @@ attr_accessor :tmp_file, :is_open, :filename def initialize(struct) @is_open = true @sheet_names = Set.new + @sheets = [] super(struct) end def add_format(options = nil) new_format = super() @@ -355,11 +360,11 @@ end @sheet_names << sheetname sheet = super sheet.workbook = self - + @sheets << sheet sheet end def get_worksheet_by_name(name) sheet = super(name) @@ -368,10 +373,11 @@ sheet end def close @is_open = false + @sheets.each(&:close) super end def read_string close if @is_open @@ -393,65 +399,125 @@ module WorksheetExt attr_accessor :workbook include AttributeHelper + def initialize(struct) + @is_open = true + @col_formats = {} + @last_row_number = -1 + super(struct) + end + def write_row(row_number, values, formats = nil) values.each_with_index do |value, index| format = if formats formats.is_a?(Array) ? formats[index] : formats end write_value(row_number, index, value, format) end end + def auto_width? + defined?(@auto_width) && @auto_width + end + + def auto_width=(v) + @auto_width = v + @column_widths = {} + end + + def calculated_column_widths + @column_widths || {} + end + def write_value(row_number, cell_number, value, format = nil) - if workbook.constant_memory? && row_number < last_row_number + if workbook.constant_memory? && row_number < @last_row_number raise ArgumentError, "Can not write to saved row in constant_memory mode (attempted row: #{row_number}, last saved row: #{last_row_number})" end if value.is_a?(Numeric) write_number(row_number, cell_number, value, format) elsif defined?(Date) && value.is_a?(Date) write_datetime(row_number, cell_number, FastExcel.lxw_datetime(value.to_datetime), format) elsif value.is_a?(Time) - write_datetime(row_number, cell_number, FastExcel.lxw_time(value), format) + write_number(row_number, cell_number, FastExcel.date_num(value), format) + elsif defined?(DateTime) && value.is_a?(DateTime) + write_number(row_number, cell_number, FastExcel.date_num(value), format) elsif value.is_a?(Formula) write_formula(row_number, cell_number, value.fml, format) else write_string(row_number, cell_number, value.to_s, format) + add_text_width(value, format, cell_number) if auto_width? end - @last_row_number = row_number > last_row_number ? row_number : last_row_number + @last_row_number = row_number > @last_row_number ? row_number : @last_row_number end + def add_text_width(value, format, cell_number) + font_size = 0 + if format + font_size = format.font_size + end + + if font_size == 0 + if @col_formats[cell_number] && @col_formats[cell_number].font_size + font_size = @col_formats[cell_number].font_size + end + end + + if font_size == 0 + font_size = workbook.default_format.font_size + end + + font_size = 13 if font_size == nil || font_size == 0 + + scale = 0.08 + new_width = (scale * font_size * value.to_s.length ) + @column_widths[cell_number] = if new_width > (@column_widths[cell_number] || 0) + new_width + else + @column_widths[cell_number] + end + end + def append_row(values, formats = nil) - increment_last_row_number! + @last_row_number += 1 write_row(last_row_number, values, formats) end def last_row_number - defined?(@last_row_number) ? @last_row_number : -1 + @last_row_number end - def increment_last_row_number! - @last_row_number = last_row_number + 1 - end + def set_column(start_col, end_col, width = nil, format = nil) + super(start_col, end_col, width || DEF_COL_WIDTH, format) - def set_column(start_col, end_col, width, format = nil) - super(start_col, end_col, width, format) + return unless format + start_col.upto(end_col) do |i| + @col_formats[i] = format + end end def set_column_width(col, width) - set_column(col, col, width, nil) + set_column(col, col, width, @col_formats[col]) end def set_columns_width(start_col, end_col, width) - set_column(start_col, end_col, width, nil) + start_col.upto(end_col) do |i| + set_column_width(i, width) + end end + def close + if auto_width? + @column_widths.each do |num, width| + set_column_width(num, width + 0.2) + end + end + end end module FormatExt include AttributeHelper