summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--.travis.yml5
-rw-r--r--axlsx.gemspec1
-rw-r--r--examples/IMAGE1UP.JPEGbin82255 -> 0 bytes
-rw-r--r--examples/conditional_formatting/example_conditional_formatting.rb15
-rwxr-xr-xexamples/example.rb32
-rw-r--r--examples/image1_fake.jpg0
-rw-r--r--lib/axlsx.rb2
-rw-r--r--lib/axlsx/drawing/pic.rb6
-rw-r--r--lib/axlsx/util/mime_type_utils.rb11
-rw-r--r--lib/axlsx/workbook/worksheet/sheet_pr.rb14
-rw-r--r--test/drawing/tc_pic.rb23
-rw-r--r--test/util/tc_mime_type_utils.rb13
-rw-r--r--test/workbook/worksheet/tc_sheet_pr.rb30
13 files changed, 126 insertions, 26 deletions
diff --git a/.travis.yml b/.travis.yml
index 088ab1da..4e4b6a59 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -9,8 +9,9 @@ notifications:
rvm:
- 1.9.2
- 1.9.3
- - 2.0.0
- - 2.1.0
+ - 2.0
+ - 2.1
+ - 2.2
- rbx
- jruby-19mode
- ruby-head
diff --git a/axlsx.gemspec b/axlsx.gemspec
index 9fc19285..8e3cd9ca 100644
--- a/axlsx.gemspec
+++ b/axlsx.gemspec
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'nokogiri', '>= 1.4.1'
s.add_runtime_dependency 'rubyzip', '~> 1.1.7'
s.add_runtime_dependency "htmlentities", "~> 4.3.1"
+ s.add_runtime_dependency "mimemagic", "~> 0.3"
s.add_development_dependency 'yard'
s.add_development_dependency 'kramdown'
diff --git a/examples/IMAGE1UP.JPEG b/examples/IMAGE1UP.JPEG
deleted file mode 100644
index ce1d0c56..00000000
--- a/examples/IMAGE1UP.JPEG
+++ /dev/null
Binary files differ
diff --git a/examples/conditional_formatting/example_conditional_formatting.rb b/examples/conditional_formatting/example_conditional_formatting.rb
index 320ae9f1..37df52f0 100644
--- a/examples/conditional_formatting/example_conditional_formatting.rb
+++ b/examples/conditional_formatting/example_conditional_formatting.rb
@@ -9,6 +9,7 @@ book = p.workbook
# define your regular styles
percent = book.styles.add_style(:format_code => "0.00%", :border => Axlsx::STYLE_THIN_BORDER)
money = book.styles.add_style(:format_code => '0,000', :border => Axlsx::STYLE_THIN_BORDER)
+status = book.styles.add_style(:border => Axlsx::STYLE_THIN_BORDER)
# define the style for conditional formatting
profitable = book.styles.add_style( :fg_color => "428751", :type => :dxf )
@@ -71,4 +72,18 @@ book.add_worksheet(:name => "Icon Set") do |ws|
ws.add_conditional_formatting("B3:B100", { :type => :iconSet, :dxfId => profitable, :priority => 1, :icon_set => icon_set })
end
+book.add_worksheet(:name => "Contains Text") do |ws|
+ ws.add_row ["Previous Year Quarterly Profits (JPY)"]
+ ws.add_row ["Quarter", "Profit", "% of Total", "Status"]
+ offset = 3
+ rows = 20
+ offset.upto(rows + offset) do |i|
+ ws.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})", (10000*((rows/2-i) * (rows/2-i))) > 100000 ? "PROFIT" : "LOSS"], :style=>[nil, money, percent, status]
+ end
+
+# Apply conditional formatting to range D3:D100 in the worksheet
+ ws.add_conditional_formatting("D3:D100", { :type => :containsText, :operator => :equal, :text => "PROFIT", :dxfId => profitable, :priority => 1 })
+ ws.add_conditional_formatting("D3:D100", { :type => :containsText, :operator => :equal, :text => "LOSS", :dxfId => unprofitable, :priority => 1 })
+end
+
p.serialize('example_conditional_formatting.xlsx')
diff --git a/examples/example.rb b/examples/example.rb
index 15d34ed6..4d4c2630 100755
--- a/examples/example.rb
+++ b/examples/example.rb
@@ -51,6 +51,7 @@ examples << :no_autowidth
examples << :cached_formula
examples << :page_breaks
examples << :rich_text
+examples << :tab_color
p = Axlsx::Package.new
wb = p.workbook
@@ -676,7 +677,7 @@ end
## Book Views
#
-## Book views let you specify which sheet the show as active when the user opens the work book as well as a bunch of other
+## Book views let you specify which sheet the show as active when the user opens the work book as well as a bunch of other
## tuning values for the UI @see Axlsx::WorkbookView
## ```ruby
if examples.include? :book_view
@@ -700,6 +701,7 @@ end
if examples.include? :conditional_formatting
percent = wb.styles.add_style(:format_code => "0.00%", :border => Axlsx::STYLE_THIN_BORDER)
money = wb.styles.add_style(:format_code => '0,000', :border => Axlsx::STYLE_THIN_BORDER)
+ status = wb.styles.add_style(:border => Axlsx::STYLE_THIN_BORDER)
# define the style for conditional formatting
profitable = wb.styles.add_style( :fg_color => "FF428751", :type => :dxf )
@@ -763,6 +765,19 @@ if examples.include? :conditional_formatting
icon_set = Axlsx::IconSet.new
sheet.add_conditional_formatting("B3:B100", { :type => :iconSet, :dxfId => profitable, :priority => 1, :icon_set => icon_set })
end
+
+ wb.add_worksheet(:name => "Contains Text") do |sheet|
+ sheet.add_row ["Previous Year Quarterly Profits (JPY)"]
+ sheet.add_row ["Quarter", "Profit", "% of Total", "Status"]
+ offset = 3
+ rows = 20
+ offset.upto(rows + offset) do |i|
+ sheet.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})", (10000*((rows/2-i) * (rows/2-i))) > 100000 ? "PROFIT" : "LOSS"], :style=>[nil, money, percent, status]
+ end
+ # Apply conditional formatting to range D3:D100 in the worksheet to match words.
+ sheet.add_conditional_formatting("D3:D100", { :type => :containsText, :operator => :equal, :text => "PROFIT", :dxfId => profitable, :priority => 1 })
+ sheet.add_conditional_formatting("D3:D100", { :type => :containsText, :operator => :equal, :text => "LOSS", :dxfId => unprofitable, :priority => 1 })
+ end
end
# Page Breaks
@@ -843,3 +858,18 @@ if examples.include? :rich_text
p.serialize 'rich_text.xlsx'
end
#```
+
+##Change tab color of sheet
+
+#```ruby
+if examples.include? :tab_color
+ p = Axlsx::Package.new
+ p.use_shared_strings = true
+ wb = p.workbook
+ wb.add_worksheet(:name => "Change Tab Color") do |sheet|
+ sheet.add_row ["Check", "out", "the", "Tab Color", "below!"]
+ sheet.sheet_pr.tab_color = "FFFF6666"
+ end
+ p.serialize 'tab_color.xlsx'
+end
+##```
diff --git a/examples/image1_fake.jpg b/examples/image1_fake.jpg
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/examples/image1_fake.jpg
diff --git a/lib/axlsx.rb b/lib/axlsx.rb
index c6f01b19..c5d26c2b 100644
--- a/lib/axlsx.rb
+++ b/lib/axlsx.rb
@@ -1,6 +1,7 @@
# encoding: UTF-8
require 'htmlentities'
require 'axlsx/version.rb'
+require 'mimemagic'
require 'axlsx/util/simple_typed_list.rb'
require 'axlsx/util/constants.rb'
@@ -11,6 +12,7 @@ require 'axlsx/util/options_parser'
# to be included with parsable intitites.
#require 'axlsx/util/parser.rb'
require 'axlsx/util/string'
+require 'axlsx/util/mime_type_utils'
require 'axlsx/stylesheet/styles.rb'
diff --git a/lib/axlsx/drawing/pic.rb b/lib/axlsx/drawing/pic.rb
index 571e9047..a1e8483a 100644
--- a/lib/axlsx/drawing/pic.rb
+++ b/lib/axlsx/drawing/pic.rb
@@ -25,8 +25,8 @@ module Axlsx
@picture_locking = PictureLocking.new(options)
end
- # allowed file extenstions
- ALLOWED_EXTENSIONS = ['gif', 'jpeg', 'png', 'jpg']
+ # allowed mime types
+ ALLOWED_MIME_TYPES = %w(image/jpeg image/png image/gif)
# The name to use for this picture
# @return [String]
@@ -67,7 +67,7 @@ module Axlsx
def image_src=(v)
Axlsx::validate_string(v)
- RestrictionValidator.validate 'Pic.image_src', ALLOWED_EXTENSIONS, File.extname(v.downcase).delete('.')
+ RestrictionValidator.validate 'Pic.image_src', ALLOWED_MIME_TYPES, MimeTypeUtils.get_mime_type(v)
raise ArgumentError, "File does not exist" unless File.exist?(v)
@image_src = v
end
diff --git a/lib/axlsx/util/mime_type_utils.rb b/lib/axlsx/util/mime_type_utils.rb
new file mode 100644
index 00000000..3fe2dbbd
--- /dev/null
+++ b/lib/axlsx/util/mime_type_utils.rb
@@ -0,0 +1,11 @@
+module Axlsx
+ # This module defines some utils related with mime type detection
+ module MimeTypeUtils
+ # Detect a file mime type
+ # @param [String] v File path
+ # @return [String] File mime type
+ def self.get_mime_type(v)
+ MimeMagic.by_magic(File.open(v)).to_s
+ end
+ end
+end
diff --git a/lib/axlsx/workbook/worksheet/sheet_pr.rb b/lib/axlsx/workbook/worksheet/sheet_pr.rb
index f271419b..a299bc0f 100644
--- a/lib/axlsx/workbook/worksheet/sheet_pr.rb
+++ b/lib/axlsx/workbook/worksheet/sheet_pr.rb
@@ -8,7 +8,7 @@ module Axlsx
serializable_attributes :sync_horizontal,
:sync_vertical,
- :transtion_evaluation,
+ :transition_evaluation,
:transition_entry,
:published,
:filter_mode,
@@ -20,7 +20,7 @@ module Axlsx
# waving magic show to set up the attriubte accessors
boolean_attr_accessor :sync_horizontal,
:sync_vertical,
- :transtion_evaluation,
+ :transition_evaluation,
:transition_entry,
:published,
:filter_mode,
@@ -40,12 +40,17 @@ module Axlsx
# @return [Worksheet]
attr_reader :worksheet
+ # The tab color of the sheet.
+ # @return [Color]
+ attr_reader :tab_color
+
# Serialize the object
# @param [String] str serialized output will be appended to this object if provided.
# @return [String]
def to_xml_string(str = '')
update_properties
str << "<sheetPr #{serialized_attributes}>"
+ tab_color.to_xml_string(str, 'tabColor') if tab_color
page_setup_pr.to_xml_string(str)
str << "</sheetPr>"
end
@@ -56,6 +61,11 @@ module Axlsx
@page_setup_pr ||= PageSetUpPr.new
end
+ # @see tab_color
+ def tab_color=(v)
+ @tab_color ||= Color.new(:rgb => v)
+ end
+
private
def update_properties
diff --git a/test/drawing/tc_pic.rb b/test/drawing/tc_pic.rb
index 3cea49c0..2c58fcfd 100644
--- a/test/drawing/tc_pic.rb
+++ b/test/drawing/tc_pic.rb
@@ -5,8 +5,10 @@ class TestPic < Test::Unit::TestCase
def setup
@p = Axlsx::Package.new
ws = @p.workbook.add_worksheet
- @test_img = File.dirname(__FILE__) + "/../../examples/image1.jpeg"
- @test_img_up = File.dirname(__FILE__) + "/../../examples/IMAGE1UP.JPEG"
+ @test_img = @test_img_jpg = File.dirname(__FILE__) + "/../../examples/image1.jpeg"
+ @test_img_png = File.dirname(__FILE__) + "/../../examples/image1.png"
+ @test_img_gif = File.dirname(__FILE__) + "/../../examples/image1.gif"
+ @test_img_fake = File.dirname(__FILE__) + "/../../examples/image1_fake.jpg"
@image = ws.add_image :image_src => @test_img, :hyperlink => 'https://github.com/randym', :tooltip => "What's up doc?"
end
@@ -70,19 +72,12 @@ class TestPic < Test::Unit::TestCase
end
def test_image_src
- assert_raise(ArgumentError) { @image.image_src = 49 }
- assert_raise(ArgumentError) { @image.image_src = 'Unknown' }
assert_raise(ArgumentError) { @image.image_src = __FILE__ }
- assert_nothing_raised { @image.image_src = @test_img }
- assert_equal(@image.image_src, @test_img)
- end
-
- def test_image_src_downcase
- assert_nothing_raised { @image.image_src = @test_img_up }
- ct = @p.send(:content_types).detect do |t|
- t.respond_to?(:extension) && t.extension.downcase == @image.extname.downcase
- end
- assert_equal("image/jpeg", ct.content_type)
+ assert_raise(ArgumentError) { @image.image_src = @test_img_fake }
+ assert_nothing_raised { @image.image_src = @test_img_gif }
+ assert_nothing_raised { @image.image_src = @test_img_png }
+ assert_nothing_raised { @image.image_src = @test_img_jpg }
+ assert_equal(@image.image_src, @test_img_jpg)
end
def test_descr
diff --git a/test/util/tc_mime_type_utils.rb b/test/util/tc_mime_type_utils.rb
new file mode 100644
index 00000000..ff54ae0a
--- /dev/null
+++ b/test/util/tc_mime_type_utils.rb
@@ -0,0 +1,13 @@
+require 'tc_helper.rb'
+class TestMimeTypeUtils < Test::Unit::TestCase
+ def setup
+ @test_img = File.dirname(__FILE__) + "/../../examples/image1.jpeg"
+ end
+
+ def teardown
+ end
+
+ def test_mime_type_utils
+ assert_equal(Axlsx::MimeTypeUtils::get_mime_type(@test_img), 'image/jpeg')
+ end
+end
diff --git a/test/workbook/worksheet/tc_sheet_pr.rb b/test/workbook/worksheet/tc_sheet_pr.rb
index 43f2941d..be45438b 100644
--- a/test/workbook/worksheet/tc_sheet_pr.rb
+++ b/test/workbook/worksheet/tc_sheet_pr.rb
@@ -5,23 +5,45 @@ class TestSheetPr < Test::Unit::TestCase
def setup
worksheet = Axlsx::Package.new.workbook.add_worksheet
- @options = {
+ @options = {
:sync_horizontal => false,
:sync_vertical => false,
- :transtion_evaluation => true,
+ :transition_evaluation => true,
:transition_entry => true,
:published => false,
:filter_mode => true,
:enable_format_conditions_calculation => false,
:code_name => '007',
- :sync_ref => 'foo'
+ :sync_ref => 'foo',
+ :tab_color => 'FFFF6666'
}
@sheet_pr = Axlsx::SheetPr.new(worksheet, @options)
end
def test_initialization
@options.each do |key, value|
- assert_equal value, @sheet_pr.send(key)
+ if key==:tab_color
+ stored_value = @sheet_pr.send(key)
+ assert_equal Axlsx::Color, stored_value.class
+ assert_equal value, stored_value.rgb
+ else
+ assert_equal value, @sheet_pr.send(key)
+ end
end
end
+
+ def test_to_xml_string
+ doc = Nokogiri::XML(@sheet_pr.to_xml_string)
+ assert_equal(doc.xpath("//sheetPr[@syncHorizontal='0']").size, 1)
+ assert_equal(doc.xpath("//sheetPr[@syncVertical='0']").size, 1)
+ assert_equal(doc.xpath("//sheetPr[@transitionEvaluation='1']").size, 1)
+ assert_equal(doc.xpath("//sheetPr[@transitionEntry='1']").size, 1)
+ assert_equal(doc.xpath("//sheetPr[@published='0']").size, 1)
+ assert_equal(doc.xpath("//sheetPr[@filterMode='1']").size, 1)
+ assert_equal(doc.xpath("//sheetPr[@enableFormatConditionsCalculation='0']").size, 1)
+ assert_equal(doc.xpath("//sheetPr[@codeName='007']").size, 1)
+ assert_equal(doc.xpath("//sheetPr[@syncRef='foo']").size, 1)
+ assert_equal(doc.xpath("//sheetPr/tabColor[@rgb='FFFF6666']").size, 1)
+ assert_equal(doc.xpath("//sheetPr/pageSetUpPr[@fitToPage='0']").size, 1)
+ end
end