lib/write_xlsx/worksheet.rb in write_xlsx-0.75.0 vs lib/write_xlsx/worksheet.rb in write_xlsx-0.76.0
- old
+ new
@@ -297,13 +297,14 @@
@writer = Package::XMLWriterSimple.new
@workbook = workbook
@index = index
@name = name
- @colinfo = []
+ @colinfo = {}
@cell_data_table = {}
@excel_version = 2007
+ @palette = workbook.palette
@page_setup = PageSetup.new
@screen_gridlines = true
@show_zeros = true
@@ -333,11 +334,10 @@
@col_formats = {}
@last_shape_id = 1
@rel_count = 0
@hlink_count = 0
- @hlink_refs = []
@external_hyper_links = []
@external_drawing_links = []
@external_comment_links = []
@external_vml_links = []
@external_table_links = []
@@ -728,12 +728,12 @@
level = 0 if level < 0
level = 7 if level > 7
@outline_col_level = level if level > @outline_col_level
- # Store the column data.
- @colinfo << [firstcol, lastcol, width, format, hidden, level, collapsed]
+ # Store the column data based on the first column. Padded for sorting.
+ @colinfo[sprintf("%05d", firstcol)] = [firstcol, lastcol, width, format, hidden, level, collapsed]
# Store the column change to allow optimisations.
@col_size_changed = 1
# Store the col sizes for use when calculating image vertices taking
@@ -775,23 +775,23 @@
return if args.empty?
row_first, col_first, row_last, col_last = row_col_notation(args)
active_cell = xl_rowcol_to_cell(row_first, col_first)
- if row_last.nil? # Single cell selection.
- sqref = active_cell
- else # Range selection.
+ if row_last # Range selection.
# Swap last row/col for first row/col as necessary
row_first, row_last = row_last, row_first if row_first > row_last
col_first, col_last = col_last, col_first if col_first > col_last
# If the first and last cell are the same write a single cell.
if row_first == row_last && col_first == col_last
sqref = active_cell
else
sqref = xl_range(row_first, row_last, col_first, col_last)
end
+ else # Single cell selection.
+ sqref = active_cell
end
# Selection isn't set for cell A1.
return if sqref == 'A1'
@@ -939,11 +939,11 @@
# worksheet2.tab_color = 0x0C
#
# See the tab_colors.rb program in the examples directory of the distro.
#
def tab_color=(color)
- @tab_color = Colors.new.get_color(color)
+ @tab_color = Colors.new.color(color)
end
# This method is deprecated. use tab_color=().
def set_tab_color(color)
put_deprecate_message("#{self}.set_tab_color")
@@ -2287,11 +2287,11 @@
# Check that row and col are valid and store max and min values
check_dimensions(row, col)
store_row_col_max_min_values(row, col)
- store_data_to_table(BlankCellData.new(self, row, col, nil, xf))
+ store_data_to_table(BlankCellData.new(self, row, col, xf))
end
#
# :call-seq:
# write_formula(row, column, formula [ , format [ , value ] ] )
@@ -2468,11 +2468,11 @@
string.split(/(\$?[A-I]?[A-Z]\$?\d+)/)
end
#
# :call-seq:
- # write_url(row, column, url [ , format, label ] )
+ # write_url(row, column, url [ , format, label, tip ] )
#
# Write a hyperlink to a URL in the cell specified by +row+ and +column+.
# The hyperlink is comprised of two elements: the visible label and
# the invisible link. The visible label is the same as the link unless
# an alternative label is specified. The label parameter is optional.
@@ -2556,26 +2556,19 @@
# Check that row and col are valid and store max and min values
check_dimensions(row, col)
store_row_col_max_min_values(row, col)
- hyperlink = Hyperlink.factory(url, str)
- hyperlink.tip = tip
+ hyperlink = Hyperlink.factory(url, str, tip)
+ store_hyperlink(row, col, hyperlink)
- @hlink_count += 1
-
- if @hlink_count > 65_530
- raise "URL '#{hyperlink.url}' added but number of URLS is over Excel's limit of 65,530 URLS per worksheet."
+ if hyperlinks_count > 65_530
+ raise "URL '#{url}' added but number of URLS is over Excel's limit of 65,530 URLS per worksheet."
end
# Write the hyperlink string.
write_string(row, col, hyperlink.str, xf)
-
- # Store the hyperlink data in a separate structure.
- @hyperlinks ||= {}
- @hyperlinks[row] ||= {}
- @hyperlinks[row][col] = hyperlink
end
#
# :call-seq:
# write_date_time (row, col, date_string [ , format ] )
@@ -3044,19 +3037,18 @@
#
# Some additional outline properties can be set via the outline_settings()
# worksheet method, see above.
#
def set_row(*args)
+ return unless args[0]
row = args[0]
height = args[1] || @default_height
xf = args[2]
hidden = args[3] || 0
level = args[4] || 0
collapsed = args[5] || 0
- return if row.nil?
-
# Get the default row height.
default_height = @default_row_height
# Use min col in check_dimensions. Default to 0 if undefined.
min_col = @dim_colmin || 0
@@ -5690,23 +5682,16 @@
#
# Convert from an Excel internal colour index to a XML style #RRGGBB index
# based on the default or user defined values in the Workbook palette.
#
- def get_palette_color(index) #:nodoc:
+ def palette_color(index) #:nodoc:
if index =~ /^#([0-9A-F]{6})$/i
- return "FF#{$~[1]}"
+ "FF#{$1.upcase}"
+ else
+ "FF#{super(index)}"
end
-
- # Adjust the colour index.
- index -= 8
-
- # Palette is passed in from the Workbook class.
- rgb = @workbook.palette[index]
-
- # TODO Add the alpha part to the RGB.
- sprintf("FF%02X%02X%02X", *rgb[0, 3])
end
def buttons_data # :nodoc:
@buttons_array
end
@@ -5767,10 +5752,20 @@
@tables.size
end
private
+ def hyperlinks_count
+ @hyperlinks.keys.inject(0) { |s, n| s += @hyperlinks[n].keys.size }
+ end
+
+ def store_hyperlink(row, col, hyperlink)
+ @hyperlinks ||= {}
+ @hyperlinks[row] ||= {}
+ @hyperlinks[row][col] = hyperlink
+ end
+
def cell_format_of_rich_string(rich_strings)
# If the last arg is a format we use it as the cell format.
if rich_strings[-1].respond_to?(:xf_index)
rich_strings.pop
else
@@ -6524,11 +6519,11 @@
def write_cols #:nodoc:
# Exit unless some column have been formatted.
return if @colinfo.empty?
@writer.tag_elements('cols') do
- @colinfo.each {|col_info| write_col_info(col_info) }
+ @colinfo.keys.sort.each {|col| write_col_info(@colinfo[col]) }
end
end
#
# Write the <col> element.
@@ -6549,13 +6544,11 @@
custom_width = true
custom_width = false if width.nil? && hidden == 0
custom_width = false if width == 8.43
- if width.nil?
- width = hidden == 0 ? 8.43 : 0
- end
+ width = hidden == 0 ? 8.43 : 0 unless width
# Convert column width from user units to character width.
if width && width < 1
width =
((width * (MAX_DIGIT_WIDTH + PADDING) + 0.5).to_i / MAX_DIGIT_WIDTH.to_f * 256).to_i / 256.0
@@ -6674,11 +6667,11 @@
#
def write_panes #:nodoc:
return if @panes.empty?
if @panes[4] == 2
- write_split_panes(*(@panes))
+ write_split_panes
else
write_freeze_panes(*(@panes))
end
end
@@ -6722,11 +6715,12 @@
#
# Write the <pane> element for split panes.
#
# See also, implementers note for split_panes().
#
- def write_split_panes(row, col, top_row, left_col, type) #:nodoc:
+ def write_split_panes #:nodoc:
+ row, col, top_row, left_col = @panes
has_selection = false
y_split = row
x_split = col
# Move user cell selection to the panes.
@@ -7051,79 +7045,57 @@
# Process any sored hyperlinks in row/col order and write the <hyperlinks>
# element. The attributes are different for internal and external links.
#
def write_hyperlinks #:nodoc:
return unless @hyperlinks
-
+ hlink_attributes = []
@hyperlinks.keys.sort.each do |row_num|
# Sort the hyperlinks into column order.
col_nums = @hyperlinks[row_num].keys.sort
# Iterate over the columns.
col_nums.each do |col_num|
# Get the link data for this cell.
link = @hyperlinks[row_num][col_num]
# If the cell isn't a string then we have to add the url as
# the string to display
- if ptrue?(@cell_data_table) &&
+ if ptrue?(@cell_data_table) &&
ptrue?(@cell_data_table[row_num]) &&
ptrue?(@cell_data_table[row_num][col_num])
if @cell_data_table[row_num][col_num].display_url_string?
- link.display = link.url_str
+ link.display_on
end
end
- if link.link_type == 1
+ if link.respond_to?(:external_hyper_link)
# External link with rel file relationship.
@rel_count += 1
- @hlink_refs << [link, row_num, col_num, @rel_count]
# Links for use by the packager.
- @external_hyper_links << ['/hyperlink', link.url, 'External']
- else
- # Internal link with rel file relationship.
- @hlink_refs << [link, row_num, col_num]
+ @external_hyper_links << link.external_hyper_link
end
+ hlink_attributes << link.attributes(row_num, col_num, @rel_count)
end
end
- return if @hlink_refs.empty?
+ return if hlink_attributes.empty?
# Write the hyperlink elements.
@writer.tag_elements('hyperlinks') do
- @hlink_refs.each do |aref|
- case aref[0].link_type
- when 1
- write_hyperlink_external(*aref)
- when 2
- write_hyperlink_internal(*aref)
- end
+ hlink_attributes.each do |attributes|
+ @writer.empty_tag('hyperlink', attributes)
end
end
end
#
- # Write the <hyperlink> element for external links.
- #
- def write_hyperlink_external(link, row, col, id) # :nodoc:
- @writer.empty_tag('hyperlink', link.write_external_attributes(row, col, id))
- end
-
- #
- # Write the <hyperlink> element for internal links.
- #
- def write_hyperlink_internal(link, row, col) #:nodoc:
- @writer.empty_tag('hyperlink', link.write_internal_attributes(row, col))
- end
-
- #
# Write the <tabColor> element.
#
def write_tab_color #:nodoc:
return unless tab_color?
@writer.empty_tag('tabColor',
[
- ['rgb', get_palette_color(@tab_color)]
+ ['rgb', palette_color(@tab_color)]
])
end
#
# Write the <outlinePr> element.