lib/write_xlsx/utility.rb in write_xlsx-1.10.2 vs lib/write_xlsx/utility.rb in write_xlsx-1.11.0
- old
+ new
@@ -3,14 +3,32 @@
require 'write_xlsx/col_name'
module Writexlsx
module Utility
- ROW_MAX = 1048576 # :nodoc:
- COL_MAX = 16384 # :nodoc:
- STR_MAX = 32767 # :nodoc:
+ ROW_MAX = 1048576 # :nodoc:
+ COL_MAX = 16384 # :nodoc:
+ STR_MAX = 32767 # :nodoc:
SHEETNAME_MAX = 31 # :nodoc:
+ CHAR_WIDTHS = {
+ ' ' => 3, '!' => 5, '"' => 6, '#' => 7, '$' => 7, '%' => 11,
+ '&' => 10, "'" => 3, '(' => 5, ')' => 5, '*' => 7, '+' => 7,
+ ',' => 4, '-' => 5, '.' => 4, '/' => 6, '0' => 7, '1' => 7,
+ '2' => 7, '3' => 7, '4' => 7, '5' => 7, '6' => 7, '7' => 7,
+ '8' => 7, '9' => 7, ':' => 4, ';' => 4, '<' => 7, '=' => 7,
+ '>' => 7, '?' => 7, '@' => 13, 'A' => 9, 'B' => 8, 'C' => 8,
+ 'D' => 9, 'E' => 7, 'F' => 7, 'G' => 9, 'H' => 9, 'I' => 4,
+ 'J' => 5, 'K' => 8, 'L' => 6, 'M' => 12, 'N' => 10, 'O' => 10,
+ 'P' => 8, 'Q' => 10, 'R' => 8, 'S' => 7, 'T' => 7, 'U' => 9,
+ 'V' => 9, 'W' => 13, 'X' => 8, 'Y' => 7, 'Z' => 7, '[' => 5,
+ '\\' => 6, ']' => 5, '^' => 7, '_' => 7, '`' => 4, 'a' => 7,
+ 'b' => 8, 'c' => 6, 'd' => 8, 'e' => 8, 'f' => 5, 'g' => 7,
+ 'h' => 8, 'i' => 4, 'j' => 4, 'k' => 7, 'l' => 4, 'm' => 12,
+ 'n' => 8, 'o' => 8, 'p' => 8, 'q' => 8, 'r' => 5, 's' => 6,
+ 't' => 5, 'u' => 8, 'v' => 7, 'w' => 11, 'x' => 7, 'y' => 7,
+ 'z' => 6, '{' => 5, '|' => 7, '}' => 5, '~' => 7
+ }.freeze
#
# xl_rowcol_to_cell($row, col, row_absolute, col_absolute)
#
def xl_rowcol_to_cell(row, col, row_absolute = false, col_absolute = false)
@@ -83,10 +101,25 @@
"=#{sheetname}!#{range1}:#{range2}"
end
#
+ # xl_string_pixel_width($string)
+ #
+ # Get the pixel width of a string based on individual character widths taken
+ # from Excel. UTF8 characters are given a default width of 8.
+ #
+ # Note, Excel adds an additional 7 pixels padding to a cell.
+ #
+ def xl_string_pixel_width(string)
+ length = 0
+ string.to_s.split(//).each { |char| length += CHAR_WIDTHS[char] || 8 }
+
+ length
+ end
+
+ #
# Sheetnames used in references should be quoted if they contain any spaces,
# special characters or if the look like something that isn't a sheet name.
# TODO. We need to handle more special cases.
#
def quote_sheetname(sheetname) # :nodoc:
@@ -256,10 +289,10 @@
warn("Warning: calling deprecated method #{method}. This method will be removed in a future release.")
end
# Check for a cell reference in A1 notation and substitute row and column
def row_col_notation(row_or_a1) # :nodoc:
- substitute_cellref(row_or_a1) if row_or_a1.to_s =~ /^\D/
+ substitute_cellref(row_or_a1) if row_or_a1.respond_to?(:match) && row_or_a1.to_s =~ /^\D/
end
#
# Substitute an Excel cell reference in A1 notation for zero based row and
# column values in an argument list.