lib/fast_excel.rb in fast_excel-0.2.6 vs lib/fast_excel.rb in fast_excel-0.3.0
- old
+ new
@@ -11,16 +11,23 @@
def initialize(fml)
@fml = fml
end
end
+ class URL
+ attr_accessor :url
+ def initialize(url)
+ @url = url
+ end
+ end
+
DEF_COL_WIDTH = 8.43
def self.open(filename = nil, constant_memory: false, default_format: nil)
tmp_file = false
if filename
- if File.exist?(filename)
+ if File.exist?(filename) && File.size(filename) > 0
raise ArgumentError, "File '#{filename}' already exists. FastExcel can not open existing files, only create new files"
end
else
require 'tmpdir'
filename = "#{Dir.mktmpdir}/fast_excel.xlsx"
@@ -296,11 +303,11 @@
def set(values)
values.each do |key, value|
if respond_to?("#{key}=")
send("#{key}=", value)
elsif respond_to?("set_#{key}=")
- send("set_#{key}=", value)
+ send("set_#{key}=", value)
else
self[key] = value
end
end
@@ -342,29 +349,33 @@
bold = add_format
bold.set_bold
bold
end
+ alias_method :bold_format, :bold_cell_format
+
# "#,##0.00"
# "[$-409]m/d/yy h:mm AM/PM;@"
def number_format(pattern)
format = add_format
format.set_num_format(pattern)
format
end
def add_worksheet(sheetname = nil)
- sheetname = nil if sheetname == ""
-
- if !sheetname.nil? && @sheet_names.include?(sheetname)
- raise ArgumentError, "Worksheet name '#{sheetname}' is already in use"
+ if !sheetname.nil?
+ if sheetname.length > Libxlsxwriter::SHEETNAME_MAX
+ raise ArgumentError, "Worksheet name '#{sheetname}' exceeds Excel's limit of #{Libxlsxwriter::SHEETNAME_MAX} characters"
+ elsif @sheet_names.include?(sheetname)
+ raise ArgumentError, "Worksheet name '#{sheetname}' is already in use"
+ end
end
- @sheet_names << sheetname
- sheet = super
+ sheet = super(sheetname)
sheet.workbook = self
@sheets << sheet
+ @sheet_names << sheet[:name]
sheet
end
def get_worksheet_by_name(name)
sheet = super(name)
@@ -381,15 +392,15 @@
def read_string
close if @is_open
File.open(filename, 'rb', &:read)
ensure
- remove_tmp_file
+ remove_tmp_folder
end
- def remove_tmp_file
- File.delete(filename) if tmp_file
+ def remove_tmp_folder
+ FileUtils.remove_entry(File.dirname(filename)) if tmp_file
end
def constant_memory?
#FastExcel.print_ffi_obj(self[:options])
@constant_memory ||= self[:options][:constant_memory] != 0
@@ -443,12 +454,17 @@
write_datetime(row_number, cell_number, FastExcel.lxw_datetime(value.to_datetime), format)
elsif value.is_a?(Time)
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)
+ elsif value.is_a?(TrueClass) || value.is_a?(FalseClass)
+ write_boolean(row_number, cell_number, value ? 1 : 0, format)
+ elsif value.is_a?(FastExcel::Formula)
write_formula(row_number, cell_number, value.fml, format)
+ elsif value.is_a?(FastExcel::URL)
+ write_url(row_number, cell_number, value.url, format)
+ add_text_width(value.url, format, cell_number) if auto_width?
else
write_string(row_number, cell_number, value.to_s, format)
add_text_width(value, format, cell_number) if auto_width?
end
@@ -485,10 +501,14 @@
def append_row(values, formats = nil)
@last_row_number += 1
write_row(last_row_number, values, formats)
end
+ def <<(values)
+ append_row(values)
+ end
+
def last_row_number
@last_row_number
end
def set_column(start_col, end_col, width = nil, format = nil)
@@ -506,9 +526,13 @@
def set_columns_width(start_col, end_col, width)
start_col.upto(end_col) do |i|
set_column_width(i, width)
end
+ end
+
+ def enable_filters!(start_col: 0, end_col:)
+ autofilter(start_col, 0, @last_row_number, end_col)
end
def close
if auto_width?
@column_widths.each do |num, width|