Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various Fixes #340

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/axlsx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ def self.camel(s="", all_caps = true)
s.gsub(/_(.)/){ $1.upcase }
end

# Does a quick escape (just the 3 main XML chars are escaped)
# @param [String] s The string to escape
# @return [String]
def self.quick_escape(s="")
s = s.to_s
s.gsub('"','&quot;').gsub("<", '&lt;').gsub(">", '&gt;')
end

# returns the provided string with all invalid control charaters
# removed.
# @param [String] str The string to process
Expand All @@ -137,7 +145,7 @@ 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
Expand All @@ -163,4 +171,8 @@ def self.trust_input
def self.trust_input=(trust_me)
@trust_input = trust_me
end

def self.to_emu_units(v)
v * 9525 # 914400 / 96
end
end
7 changes: 6 additions & 1 deletion lib/axlsx/drawing/marker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,14 @@ def coord(col, row=0)
# @param [String] str
# @return [String]
def to_xml_string(str = '')
overrides = {
:colOff => Axlsx.to_emu_units(colOff),
:rowOff => Axlsx.to_emu_units(rowOff)
}
[:col, :colOff, :row, :rowOff].each do |k|
str << ('<xdr:' << k.to_s << '>' << self.send(k).to_s << '</xdr:' << k.to_s << '>')
str << ('<xdr:' << k.to_s << '>' << (overrides[k] || self.send(k)).to_s << '</xdr:' << k.to_s << '>')
end
str
end
private

Expand Down
6 changes: 2 additions & 4 deletions lib/axlsx/drawing/one_cell_anchor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Axlsx
class OneCellAnchor

include Axlsx::OptionsParser

# Creates a new OneCellAnchor object and an Pic associated with it.
# @param [Drawing] drawing
# @option options [Array] start_at the col, row to start at
Expand Down Expand Up @@ -89,9 +89,7 @@ def to_xml_string(str = '')
# !{:cx=>[Integer], :cy=>[Integer]
# @return [Hash]
def ext
cy = @height * 914400 / 96
cx = @width * 914400 / 96
{:cy=>cy, :cx=>cx}
{:cy=>Axlsx.to_emu_units(@height), :cx=>Axlsx.to_emu_units(@width)}
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/axlsx/drawing/series_title.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def to_xml_string(str = '')
str << '<c:strCache>'
str << '<c:ptCount val="1"/>'
str << '<c:pt idx="0">'
str << ('<c:v>' << @text << '</c:v>')
str << ('<c:v>' << Axlsx.quick_escape(@text.to_s) << '</c:v>')
str << '</c:pt>'
str << '</c:strCache>'
str << '</c:strRef>'
Expand Down
4 changes: 3 additions & 1 deletion lib/axlsx/stylesheet/num_fmt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def formatCode=(v) Axlsx::validate_string v; @formatCode = v end
# @param [String] str
# @return [String]
def to_xml_string(str = '')
serialized_tag('numFmt', str)
str << '<numFmt '
instance_values.each{ |k,v| str << "#{Axlsx.camel(k, false)}=\"#{Axlsx.quick_escape(v)}\" " }
str << '/>'
end

end
Expand Down
7 changes: 4 additions & 3 deletions lib/axlsx/util/serialized_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def self.included(base)
# class methods applied to all includers
module ClassMethods

# This is the method to be used in inheriting classes to specify
# This is the method to be used in inheriting classes to specify
# which of the instance values are serializable
def serializable_attributes(*symbols)
@xml_attributes = symbols
Expand Down Expand Up @@ -43,7 +43,7 @@ def serialized_tag(tagname, str, additional_attributes = {}, &block)
end
end

# serializes the instance values of the defining object based on the
# serializes the instance values of the defining object based on the
# list of serializable attributes.
# @param [String] str The string instance to append this
# serialization to.
Expand All @@ -52,7 +52,8 @@ def serialized_tag(tagname, str, additional_attributes = {}, &block)
def serialized_attributes(str = '', additional_attributes = {})
attributes = declared_attributes.merge! additional_attributes
attributes.each do |key, value|
str << "#{Axlsx.camel(key, false)}=\"#{Axlsx.camel(Axlsx.booleanize(value), false)}\" "
new_value = Axlsx.quick_escape(Axlsx.camel(Axlsx.booleanize(value), false))
str << "#{Axlsx.camel(key, false)}=\"#{new_value}\" "
end
str
end
Expand Down
2 changes: 1 addition & 1 deletion lib/axlsx/workbook/worksheet/merged_cells.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def initialize(worksheet)
def add(cells)
self << if cells.is_a?(String)
cells
elsif cells.is_a?(Array)
elsif cells.is_a?(Array) || cells.is_a?(SimpleTypedList)
Axlsx::cell_range(cells, false)
end
end
Expand Down
64 changes: 33 additions & 31 deletions lib/axlsx/workbook/worksheet/sheet_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Axlsx
# @note The recommended way to manage the sheet view is via Worksheet#sheet_view
# @see Worksheet#sheet_view
class SheetView

include Axlsx::OptionsParser
include Axlsx::Accessors
include Axlsx::SerializedAttributes
Expand Down Expand Up @@ -36,7 +36,9 @@ def initialize(options={})
@right_to_left = @show_formulas = @show_outline_symbols = @show_white_space = @tab_selected = @window_protection = false
@default_grid_color = @show_grid_lines = @show_row_col_headers = @show_ruler = @show_zeros = true
@zoom_scale = 100
@zoom_scale_normal = @zoom_scale_page_layout_view = @zoom_scale_sheet_layout_view = @workbook_view_id = 0
# The ECMA specification is incorrect here, the Open XML Validator indicates a minimum of 10 for these values
@zoom_scale_normal = @zoom_scale_page_layout_view = @zoom_scale_sheet_layout_view = 100
@workbook_view_id = 0
@selections = {}
parse_options options
end
Expand Down Expand Up @@ -66,30 +68,30 @@ def pane
# @return [Hash]
attr_reader :selections

#
#
# Color Id
# Index to the color value for row/column
# text headings and gridlines. This is an
# 'index color value' (ICV) rather than
# text headings and gridlines. This is an
# 'index color value' (ICV) rather than
# rgb value.
# @see type
# @return [Integer]
# default nil
attr_reader :color_id

# Top Left Visible Cell
# Location of the top left visible cell Location
# Location of the top left visible cell Location
# of the top left visible cell in the bottom right
# pane (when in Left-to-Right mode).
# @see type
# @return [String]
# default nil
attr_reader :top_left_cell


# View Type
# Indicates the view type.
# Options are
# Options are
# * normal: Normal view
# * page_break_preview: Page break preview
# * page_layout: Page Layout View
Expand All @@ -99,62 +101,62 @@ def pane
attr_reader :view

# Workbook View Index
# Zero-based index of this workbook view, pointing
# Zero-based index of this workbook view, pointing
# to a workbookView element in the bookViews collection.
# @see type
# @return [Integer]
# @return [Integer]
# default 0
attr_reader :workbook_view_id

# Zoom Scale
# Window zoom magnification for current view
# Window zoom magnification for current view
# representing percent values. This attribute
# is restricted to values ranging from 10 to 400.
# is restricted to values ranging from 10 to 400.
# Horizontal & Vertical scale together.
# Current view can be Normal, Page Layout, or
# Current view can be Normal, Page Layout, or
# Page Break Preview.
# @see type
# @return [Integer]
# @return [Integer]
# default 100
attr_reader :zoom_scale


# Zoom Scale Normal View
# Zoom magnification to use when in normal view,
# representing percent values. This attribute is
# restricted to values ranging from 10 to 400.
# Zoom magnification to use when in normal view,
# representing percent values. This attribute is
# restricted to values ranging from 10 to 400.
# Horizontal & Vertical scale together.
# Applies for worksheets only; zero implies the
# Applies for worksheets only; zero implies the
# automatic setting.
# @see type
# @return [Integer]
# @return [Integer]
# default 0
attr_reader :zoom_scale_normal


# Zoom Scale Page Layout View
# Zoom magnification to use when in page layout
# view, representing percent values. This attribute
# is restricted to values ranging from 10 to 400.
# Zoom magnification to use when in page layout
# view, representing percent values. This attribute
# is restricted to values ranging from 10 to 400.
# Horizontal & Vertical scale together.
# Applies for worksheets only; zero implies
# Applies for worksheets only; zero implies
# the automatic setting.
# @see type
# @return [Integer]
# @return [Integer]
# default 0
attr_reader :zoom_scale_page_layout_view


# Zoom Scale Page Break Preview
# Zoom magnification to use when in page break
# preview, representing percent values. This
# attribute is restricted to values ranging
# Zoom magnification to use when in page break
# preview, representing percent values. This
# attribute is restricted to values ranging
# from 10 to 400. Horizontal & Vertical scale
# together.
# Applies for worksheet only; zero implies
# Applies for worksheet only; zero implies
# the automatic setting.
# @see type
# @return [Integer]
# @return [Integer]
# default 0
attr_reader :zoom_scale_sheet_layout_view

Expand All @@ -172,7 +174,7 @@ def color_id=(v); Axlsx::validate_unsigned_int(v); @color_id = v end
# @see top_left_cell
def top_left_cell=(v)
cell = (v.class == Axlsx::Cell ? v.r_abs : v)
Axlsx::validate_string(cell)
Axlsx::validate_string(cell)
@top_left_cell = cell
end

Expand Down
4 changes: 3 additions & 1 deletion lib/axlsx/workbook/worksheet/worksheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Worksheet
# definition of characters which are less than the maximum width of 0-9 in the default font for use in String#count.
# This is used for autowidth calculations
THIN_CHARS = '^.acfijklrstxzFIJL()-'.freeze

# Creates a new worksheet.
# @note the recommended way to manage worksheets is Workbook#add_worksheet
# @see Workbook#add_worksheet
Expand All @@ -24,6 +24,8 @@ def initialize(wb, options={})
parse_options options
@workbook.worksheets << self
@sheet_id = index + 1
# Autoset the name or else we'll get an error if we don't set it
name
yield self if block_given?
end

Expand Down
2 changes: 1 addition & 1 deletion test/content_type/tc_content_type.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# encoding: UTF-8
require 'tc_helper.rb'

class TestContentType < Test::Unit::TestCase
class TestContentType < Minitest::Unit::TestCase
def setup
@package = Axlsx::Package.new
@doc = Nokogiri::XML(@package.send(:content_types).to_xml_string)
Expand Down
4 changes: 2 additions & 2 deletions test/content_type/tc_default.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# encoding: UTF-8
require 'tc_helper.rb'

class TestDefault < Test::Unit::TestCase
class TestDefault < Minitest::Unit::TestCase

def test_content_type_restriction
assert_raise(ArgumentError, "raises argument error if invlalid ContentType is") { Axlsx::Default.new :ContentType=>"asdf" }
assert_raises(ArgumentError, "raises argument error if invlalid ContentType is") { Axlsx::Default.new :ContentType=>"asdf" }
end

def test_to_xml_string
Expand Down
4 changes: 2 additions & 2 deletions test/content_type/tc_override.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'tc_helper.rb'
class TestOverride < Test::Unit::TestCase
class TestOverride < Minitest::Unit::TestCase

def test_content_type_restriction
assert_raise(ArgumentError, "requires known content type") { Axlsx::Override.new :ContentType=>"asdf" }
assert_raises(ArgumentError, "requires known content type") { Axlsx::Override.new :ContentType=>"asdf" }
end

def test_to_xml
Expand Down
2 changes: 1 addition & 1 deletion test/doc_props/tc_app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'tc_helper.rb'

class TestApp < Test::Unit::TestCase
class TestApp < Minitest::Unit::TestCase
def setup
options = {
:'Template' => 'Foo.xlt',
Expand Down
2 changes: 1 addition & 1 deletion test/doc_props/tc_core.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'tc_helper.rb'

class TestCore < Test::Unit::TestCase
class TestCore < Minitest::Unit::TestCase

def setup
@core = Axlsx::Core.new
Expand Down
4 changes: 2 additions & 2 deletions test/drawing/tc_axes.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'tc_helper.rb'

class TestAxes < Test::Unit::TestCase
class TestAxes < Minitest::Unit::TestCase
def test_constructor_requires_cat_axis_first
assert_raise(ArgumentError) { Axlsx::Axes.new(:val_axis => Axlsx::ValAxis, :cat_axis => Axlsx::CatAxis) }
assert_raises(ArgumentError) { Axlsx::Axes.new(:val_axis => Axlsx::ValAxis, :cat_axis => Axlsx::CatAxis) }
assert_nothing_raised { Axlsx::Axes.new(:cat_axis => Axlsx::CatAxis, :val_axis => Axlsx::ValAxis) }
end
end
Loading