lib/write_xlsx/workbook.rb in write_xlsx-0.77.2 vs lib/write_xlsx/workbook.rb in write_xlsx-0.78.0
- old
+ new
@@ -44,10 +44,11 @@
attr_reader :named_ranges # :nodoc:
attr_reader :doc_properties # :nodoc:
attr_reader :image_types, :images # :nodoc:
attr_reader :shared_strings # :nodoc:
attr_reader :vba_project # :nodoc:
+ attr_reader :excel2003_style # :nodoc:
#
# A new Excel workbook is created using the +new+ constructor
# which accepts either a filename or an IO object as a parameter.
# The following example creates a new Excel file based on a filename:
#
@@ -85,16 +86,18 @@
# And you can pass default_formats parameter like this:
#
# formats = { :font => 'Arial', :size => 10.5 }
# workbook = WriteXLSX.new('file.xlsx', formats)
#
- def initialize(file, default_formats = {})
+ def initialize(file, *option_params)
+ options, default_formats = process_workbook_options(*option_params)
@writer = Package::XMLWriterSimple.new
- @tempdir = File.join(Dir.tmpdir, Digest::MD5.hexdigest(Time.now.to_s))
+ @tempdir = options[:tempdir] ||
+ File.join(Dir.tmpdir, Digest::MD5.hexdigest(Time.now.to_s))
setup_filename(file)
- @date_1904 = false
+ @date_1904 = options[:date_1904] || false
@activesheet = 0
@firstsheet = 0
@selected = 0
@fileclosed = false
@worksheets = Sheets.new
@@ -108,24 +111,35 @@
@defined_names = []
@named_ranges = []
@custom_colors = []
@doc_properties = {}
@local_time = Time.now
- @optimization = 0
+ @optimization = options[:optimization] || 0
@x_window = 240
@y_window = 15
@window_width = 16095
@window_height = 9660
@tab_ratio = 500
+ @excel2003_style = options[:excel2003_style] || false
@table_count = 0
@image_types = {}
@images = []
# Structures for the shared strings data.
@shared_strings = Package::SharedStrings.new
- add_format(default_formats.merge(:xf_index => 0))
+ # Formula calculation default settings.
+ @calc_id = 124519
+ @calc_mode = 'auto'
+ @calc_on_load = true
+
+ if @excel2003_style
+ add_format(default_formats
+ .merge(:xf_index => 0, :font_family => 0, :font => 'Arial', :size => 10, :theme => -1))
+ else
+ add_format(default_formats.merge(:xf_index => 0))
+ end
set_color_palette
end
#
# The close method is used to close an Excel file.
@@ -402,11 +416,17 @@
# format2 = workbook.add_format # Set properties later
#
# See the {Format Class's rdoc}[Format.html] for more details about
# Format properties and how to set them.
#
- def add_format(properties = {})
+ def add_format(property_hash = {})
+ properties = {}
+ if @excel2003_style
+ properties.update(:font => 'Arial', :size => 10, :theme => -1)
+ end
+ properties.update(property_hash)
+
format = Format.new(@formats, properties)
@formats.formats.push(format) # Store format reference
format
@@ -812,10 +832,27 @@
def add_vba_project(vba_project)
@vba_project = vba_project
end
#
+ # set_calc_mode()
+ #
+ # Set the Excel caclcuation mode for the workbook.
+ #
+ def set_calc_mode(mode, calc_id = nil)
+ @calc_mode = mode || 'auto'
+
+ if mode == 'manual'
+ @calc_on_load = false
+ elsif mode == 'auto_except_tables'
+ @calc_mode = 'autoNoTable'
+ end
+
+ @calc_id = calc_id if calc_id
+ end
+
+ #
# Change the RGB components of the elements in the colour palette.
#
# The set_custom_color method can be used to override one of the built-in
# palette values with a more suitable colour.
#
@@ -1137,13 +1174,21 @@
end
@writer.empty_tag('workbookView', attributes)
end
def write_calc_pr #:nodoc:
- attributes = [
- ['calcId', 124519],
- ['fullCalcOnLoad', 1]
- ]
+ attributes = [ ['calcId', @calc_id] ]
+
+ case @calc_mode
+ when 'manual'
+ attributes << ['calcMode', 'manual']
+ attributes << ['calcOnSave', 0]
+ when 'autoNoTable'
+ attributes << ['calcMode', 'autoNoTable']
+ end
+
+ attributes << ['fullCalcOnLoad', 1] if @calc_on_load
+
@writer.empty_tag('calcPr', attributes)
end
def write_ext_lst #:nodoc:
@writer.tag_elements('extLst') { write_ext }