lib/axlsx.rb in axlsx-2.0.1 vs lib/axlsx.rb in axlsx-2.1.0.pre
- old
+ new
@@ -8,10 +8,11 @@
require 'axlsx/util/accessors.rb'
require 'axlsx/util/serialized_attributes'
require 'axlsx/util/options_parser'
# to be included with parsable intitites.
#require 'axlsx/util/parser.rb'
+require 'axlsx/util/string'
require 'axlsx/stylesheet/styles.rb'
require 'axlsx/doc_props/app.rb'
require 'axlsx/doc_props/core.rb'
@@ -51,11 +52,11 @@
def self.cell_range(cells, absolute=true)
return "" unless cells.first.is_a? Cell
cells = sort_cells(cells)
reference = "#{cells.first.reference(absolute)}:#{cells.last.reference(absolute)}"
if absolute
- escaped_name = cells.first.row.worksheet.name.gsub "'", "''"
+ escaped_name = cells.first.row.worksheet.name.gsub ''', "''"
"'#{escaped_name}'!#{reference}"
else
reference
end
end
@@ -63,11 +64,11 @@
# sorts the array of cells provided to start from the minimum x,y to
# the maximum x.y#
# @param [Array] cells
# @return [Array]
def self.sort_cells(cells)
- cells.sort { |x, y| [x.index, x.row.index] <=> [y.index, y.row.index] }
+ cells.sort { |x, y| [x.index, x.row.row_index] <=> [y.index, y.row.row_index] }
end
#global reference html entity encoding
# @return [HtmlEntities]
def self.coder
@@ -86,24 +87,25 @@
# converts the column index into alphabetical values.
# @note This follows the standard spreadsheet convention of naming columns A to Z, followed by AA to AZ etc.
# @return [String]
def self.col_ref(index)
- chars = []
+ chars = ''
while index >= 26 do
- chars << ((index % 26) + 65).chr
- index = (index / 26).to_i - 1
+ index, char = index.divmod(26)
+ chars.prepend((char + 65).chr)
+ index -= 1
end
- chars << (index + 65).chr
- chars.reverse.join
+ chars.prepend((index + 65).chr)
+ chars
end
# @return [String] The alpha(column)numeric(row) reference for this sell.
# @example Relative Cell Reference
# ws.rows.first.cells.first.r #=> "A1"
def self.cell_r(c_index, r_index)
- Axlsx::col_ref(c_index).to_s << (r_index+1).to_s
+ col_ref(c_index) << (r_index+1).to_s
end
# Creates an array of individual cell references based on an excel reference range.
# @param [String] range A cell range, for example A1:D5
# @return [Array]
@@ -111,11 +113,11 @@
range.match(/^(\w+?\d+)\:(\w+?\d+)$/)
start_col, start_row = name_to_indices($1)
end_col, end_row = name_to_indices($2)
(start_row..end_row).to_a.map do |row_num|
(start_col..end_col).to_a.map do |col_num|
- "#{col_ref(col_num)}#{row_num+1}"
+ cell_r(col_num, row_num)
end
end
end
# performs the increadible feat of changing snake_case to CamelCase
@@ -125,17 +127,29 @@
s = s.to_s
s = s.capitalize if all_caps
s.gsub(/_(.)/){ $1.upcase }
end
- # returns the provided string with all invalid control charaters
- # removed.
- # @param [String] str The sting to process
- # @return [String]
- def self.sanitize(str)
- str.gsub(CONTROL_CHAR_REGEX, '')
+ # returns the provided string with all invalid control charaters
+ # removed.
+ # @param [String] str The string to process
+ # @return [String]
+ def self.sanitize(str)
+ str.delete!(CONTROL_CHARS)
+ str
+ end
+
+ # If value is boolean return 1 or 0
+ # else return the value
+ # @param [Object] value The value to process
+ # @return [Object]
+ def self.booleanize(value)
+ if value == true || value == false
+ value ? 1 : 0
+ else
+ value
end
-
+ end
# Instructs the serializer to not try to escape cell value input.
# This will give you a huge speed bonus, but if you content has <, > or other xml character data
# the workbook will be invalid and excel will complain.
def self.trust_input