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

- old
+ new

@@ -11,20 +11,20 @@ DEF_COL_WIDTH = 8.43 def self.open(filename = nil, constant_memory: false, default_format: nil) tmp_file = false - unless filename + if filename + if File.exist?(filename) + 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" tmp_file = true end - unless filename - raise ArgumentError, "filename is required" - end - filename = filename.to_s if defined?(Pathname) && filename.is_a?(Pathname) workbook = if constant_memory opt = Libxlsxwriter::WorkbookOptions.new opt[:constant_memory] = 1 @@ -321,10 +321,11 @@ include AttributeHelper attr_accessor :tmp_file, :is_open, :filename def initialize(struct) @is_open = true + @sheet_names = Set.new super(struct) end def add_format(options = nil) new_format = super() @@ -345,11 +346,26 @@ 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" + end + @sheet_names << sheetname + sheet = super sheet.workbook = self + + sheet + end + + def get_worksheet_by_name(name) + sheet = super(name) + sheet.workbook = self + sheet end def close @is_open = false