diff options
| author | Johnny Shields <[email protected]> | 2023-04-13 00:05:25 +0900 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-04-13 00:05:25 +0900 |
| commit | 5f4cf9a12235697b21e666927ed7d08ef18bf15b (patch) | |
| tree | fe696d452e0b8131e4dd2369d6038407eb524f64 /lib/axlsx/workbook/worksheet/worksheet.rb | |
| parent | 467ac5cb9e03ff975929dd5bf473b75f7b6f1b6c (diff) | |
| parent | e548f377932207130cec4ac257a3907385d547d5 (diff) | |
| download | caxlsx-5f4cf9a12235697b21e666927ed7d08ef18bf15b.tar.gz caxlsx-5f4cf9a12235697b21e666927ed7d08ef18bf15b.zip | |
Merge branch 'master' into escape-formulas-improvement
Diffstat (limited to 'lib/axlsx/workbook/worksheet/worksheet.rb')
| -rw-r--r-- | lib/axlsx/workbook/worksheet/worksheet.rb | 62 |
1 files changed, 31 insertions, 31 deletions
diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index f50e6b94..38fb5254 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -1,9 +1,6 @@ -# encoding: UTF-8 - require_relative "border_creator" module Axlsx - # The Worksheet class represents a worksheet in the workbook. class Worksheet include Axlsx::OptionsParser @@ -19,7 +16,7 @@ module Axlsx # @option options [Boolean] show_gridlines Whether gridlines should be shown for this sheet. # @option options [Boolean] escape_formulas Whether formulas should be escaped by default. Can be overridden at a # row/cell level. - def initialize(wb, options={}) + def initialize(wb, options = {}) self.workbook = wb @sheet_protection = nil initialize_page_options(options) @@ -46,7 +43,7 @@ module Axlsx # The name of the worksheet # @return [String] def name - @name ||= "Sheet" + (index+1).to_s + @name ||= "Sheet" + (index + 1).to_s end # Whether to treat values starting with an equals sign as formulas or as literal strings. @@ -119,13 +116,13 @@ module Axlsx # The tables in this worksheet # @return [Array] of Table def tables - @tables ||= Tables.new self + @tables ||= Tables.new self end # The pivot tables in this worksheet # @return [Array] of Table def pivot_tables - @pivot_tables ||= PivotTables.new self + @pivot_tables ||= PivotTables.new self end # A collection of column breaks added to this worksheet @@ -189,10 +186,10 @@ module Axlsx # @see #page_setup def fit_to_page? return false unless Axlsx.instance_values_for(self).keys.include?('page_setup') + page_setup.fit_to_page? end - # Column info for the sheet # @return [SimpleTypedList] def column_info @@ -328,7 +325,7 @@ module Axlsx # @param [String] name def name=(name) validate_sheet_name name - @name=Axlsx::coder.encode(name) + @name = Axlsx::coder.encode(name) end # The auto filter range for the worksheet @@ -346,13 +343,13 @@ module Axlsx # The part name of this worksheet # @return [String] def pn - "#{WORKSHEET_PN % (index+1)}" + "#{WORKSHEET_PN % (index + 1)}" end # The relationship part name of this worksheet # @return [String] def rels_pn - "#{WORKSHEET_RELS_PN % (index+1)}" + "#{WORKSHEET_RELS_PN % (index + 1)}" end # The relationship id of this worksheet. @@ -429,7 +426,7 @@ module Axlsx # sign as formula (default) or as simple string. # Allowing user generated data to be interpreted as formulas can be dangerous # (see https://www.owasp.org/index.php/CSV_Injection for details). - def add_row(values=[], options={}) + def add_row(values = [], options = {}) options[:escape_formulas] = escape_formulas if options[:escape_formulas].nil? row = Row.new(self, values, options) update_column_info row, options.delete(:widths) @@ -450,7 +447,7 @@ module Axlsx # @see ConditionalFormattingRule#initialize # @see file:examples/example_conditional_formatting.rb def add_conditional_formatting(cells, rules) - cf = ConditionalFormatting.new( :sqref => cells ) + cf = ConditionalFormatting.new(:sqref => cells) cf.add_rules rules conditional_formattings << cf conditional_formattings @@ -471,7 +468,7 @@ module Axlsx # @param [Hash] options for the hyperlink # @see WorksheetHyperlink for a list of options # @return [WorksheetHyperlink] - def add_hyperlink(options={}) + def add_hyperlink(options = {}) hyperlinks.add(options) end @@ -488,33 +485,33 @@ module Axlsx # @see Bar3DChart # @see Line3DChart # @see README for examples - def add_chart(chart_type, options={}) + def add_chart(chart_type, options = {}) chart = worksheet_drawing.add_chart(chart_type, options) yield chart if block_given? chart end # needs documentation - def add_table(ref, options={}) + def add_table(ref, options = {}) tables << Table.new(ref, self, options) yield tables.last if block_given? tables.last end - def add_pivot_table(ref, range, options={}) + def add_pivot_table(ref, range, options = {}) pivot_tables << PivotTable.new(ref, range, self, options) yield pivot_tables.last if block_given? pivot_tables.last end # Shortcut to worsksheet_comments#add_comment - def add_comment(options={}) + def add_comment(options = {}) worksheet_comments.add_comment(options) end # Adds a media item to the worksheets drawing # @option [Hash] options options passed to drawing.add_image - def add_image(options={}) + def add_image(options = {}) image = worksheet_drawing.add_image(options) yield image if block_given? image @@ -528,10 +525,10 @@ module Axlsx def add_page_break(cell) DataTypeValidator.validate :worksheet_page_break, [String, Cell], cell column_index, row_index = if cell.is_a?(String) - Axlsx.name_to_indices(cell) - else - cell.pos - end + Axlsx.name_to_indices(cell) + else + cell.pos + end if column_index > 0 col_breaks.add_break(:id => column_index) end @@ -548,6 +545,7 @@ module Axlsx def column_widths(*widths) widths.each_with_index do |value, index| next if value == nil + Axlsx::validate_unsigned_numeric(value) unless value == nil find_or_create_column_info(index).width = value end @@ -561,7 +559,7 @@ module Axlsx # @note You can also specify the style for specific columns in the call to add_row by using an array for the :styles option # @see Worksheet#add_row # @see README.md for an example - def col_style(index, style, options={}) + def col_style(index, style, options = {}) offset = options.delete(:row_offset) || 0 cells = @rows[(offset..-1)].map { |row| row[index] }.flatten.compact cells.each { |cell| cell.style = style } @@ -575,7 +573,7 @@ module Axlsx # @note You can also specify the style in the add_row call # @see Worksheet#add_row # @see README.md for an example - def row_style(index, style, options={}) + def row_style(index, style, options = {}) offset = options.delete(:col_offset) || 0 cells = cols[(offset..-1)].map { |column| column[index] }.flatten.compact cells.each { |cell| cell.style = style } @@ -605,7 +603,7 @@ module Axlsx # Set the style for cells in a specific column # @param [String|Array] cell references # @param [Hash|Array|Symbol] border options - def add_border(cell_refs, options=nil) + def add_border(cell_refs, options = nil) if options.is_a?(Hash) border_edges = options[:edges] border_style = options[:style] @@ -628,7 +626,7 @@ module Axlsx end # Returns a sheet node serialization for this sheet in the workbook. - def to_sheet_node_xml_string(str='') + def to_sheet_node_xml_string(str = '') add_autofilter_defined_name_to_workbook str << '<sheet ' serialized_attributes str @@ -639,7 +637,7 @@ module Axlsx # Serializes the worksheet object to an xml string # This intentionally does not use nokogiri for performance reasons # @return [String] - def to_xml_string str='' + def to_xml_string str = '' add_autofilter_defined_name_to_workbook auto_filter.apply if auto_filter.range str << '<?xml version="1.0" encoding="UTF-8"?>' @@ -668,7 +666,7 @@ module Axlsx def [](cell_def) return rows[cell_def] if cell_def.is_a?(Integer) - parts = cell_def.split(':').map{ |part| name_to_cell part } + parts = cell_def.split(':').map { |part| name_to_cell part } if parts.size == 1 parts.first @@ -742,7 +740,7 @@ module Axlsx end def outline(collection, range, level = 1, collapsed = true) - range.each do |index| + range.each do |index| unless (item = collection[index]).nil? item.outline_level = level item.hidden = collapsed @@ -755,9 +753,11 @@ module Axlsx DataTypeValidator.validate :worksheet_name, String, name # ignore first character (BOM) after encoding to utf16 because Excel does so, too. raise ArgumentError, (ERR_SHEET_NAME_EMPTY) if name.empty? + character_length = name.encode("utf-16")[1..-1].encode("utf-16").bytesize / 2 raise ArgumentError, (ERR_SHEET_NAME_TOO_LONG % name) if character_length > WORKSHEET_MAX_NAME_LENGTH raise ArgumentError, (ERR_SHEET_NAME_CHARACTER_FORBIDDEN % name) if WORKSHEET_NAME_FORBIDDEN_CHARS.any? { |char| name.include? char } + name = Axlsx::coder.encode(name) sheet_names = @workbook.worksheets.reject { |s| s == self }.map { |s| s.name } raise ArgumentError, (ERR_DUPLICATE_SHEET_NAME % name) if sheet_names.include?(name) @@ -812,7 +812,6 @@ module Axlsx @merged_cells ||= MergedCells.new self end - # Helper method for parsingout the root node for worksheet # @return [String] def worksheet_node @@ -851,6 +850,7 @@ module Axlsx def add_autofilter_defined_name_to_workbook return if !auto_filter.range + workbook.add_defined_name auto_filter.defined_name, name: '_xlnm._FilterDatabase', local_sheet_id: index, hidden: 1 end end |
