Skip to content

Commit

Permalink
Added locking attributes for pictures and some stubs for parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Randy Morgan committed Dec 2, 2011
1 parent 6439ce4 commit 3def8f8
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 15 deletions.
3 changes: 2 additions & 1 deletion examples/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@
end
p.serialize("example7.xlsx")
end

#Add an Image
if ARGV.size==0 || ARGV.include?("8")

p = Axlsx::Package.new
p.workbook.add_worksheet do |sheet|
img = File.expand_path('examples/image1.jpeg')
sheet.add_image(:image_src => img) do |image|
sheet.add_image(:image_src => img, :noSelect=>true, :noMove=>true) do |image|
image.width=720
image.height=666
image.start_at 2, 2
Expand Down
4 changes: 4 additions & 0 deletions lib/axlsx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
require 'axlsx/util/simple_typed_list.rb'
require 'axlsx/util/constants.rb'
require 'axlsx/util/validators.rb'

# to be included with parsable intitites.
#require 'axlsx/util/parser.rb'

require 'axlsx/stylesheet/styles.rb'

require 'axlsx/doc_props/app.rb'
Expand Down
2 changes: 1 addition & 1 deletion lib/axlsx/drawing/drawing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Axlsx
require 'axlsx/drawing/bar_3D_chart.rb'
require 'axlsx/drawing/line_3D_chart.rb'


require 'axlsx/drawing/picture_locking.rb'
require 'axlsx/drawing/pic.rb'

# A Drawing is a canvas for charts. Each worksheet has a single drawing that manages anchors.
Expand Down
7 changes: 5 additions & 2 deletions lib/axlsx/drawing/pic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Pic
# @return [OneCellAnchor]
attr_reader :anchor

# The picture locking attributes for this picture
attr_reader :picture_locking

# Creates a new Pic(ture) object
# @param [Anchor] anchor the anchor that holds this image
Expand All @@ -43,6 +45,7 @@ def initialize(anchor, options={})
end
start_at(*options[:start_at]) if options[:start_at]
yield self if block_given?
@picture_locking = PictureLocking.new(options)
end

def image_src=(v)
Expand Down Expand Up @@ -94,7 +97,7 @@ def width
def width=(v)
@anchor.width = v
end

# providing access to update the anchor's height attribute
# @param [Integer] v
# @see OneCellAnchor.width
Expand Down Expand Up @@ -126,7 +129,7 @@ def to_xml(xml)
xml.send('xdr:nvPicPr') {
xml.send('xdr:cNvPr', :id=>"2", :name=>name, :descr=>descr)
xml.send('xdr:cNvPicPr') {
xml.send('a:picLocks', :noChangeAspect=>1)
picture_locking.to_xml(xml)
}
}
xml.send('xdr:blipFill') {
Expand Down
21 changes: 14 additions & 7 deletions lib/axlsx/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ def initialize(options={})
yield self if block_given?
end

# Accepts a ruport table for serialization to xlsx
# @param [Table] table a ruport Table object
def ruport_table(table)
puts table
end

# The workbook this package will serialize or validate.
# @return [Workbook] If no workbook instance has been assigned with this package a new Workbook instance is returned.
# @raise ArgumentError if workbook parameter is not a Workbook instance.
Expand All @@ -39,6 +33,18 @@ def workbook
yield @workbook if block_given?
@workbook
end

#def self.parse(input, confirm_valid = false)
# p = Package.new
# z = Zip::ZipFile.open(input)
# p.workbook = Workbook.parse z.get_entry(WORKBOOK_PN)
# p
#end






# @see workbook
def workbook=(workbook) DataTypeValidator.validate "Package.workbook", Workbook, workbook; @workbook = workbook; end
Expand Down Expand Up @@ -76,7 +82,8 @@ def serialize(output, confirm_valid=false)
end
true
end



# Validate all parts of the package against xsd schema.
# @return [Array] An array of all validation errors found.
# @note This gem includes all schema from OfficeOpenXML-XMLSchema-Transitional.zip and OpenPackagingConventions-XMLSchema.zip
Expand Down
35 changes: 35 additions & 0 deletions lib/axlsx/util/parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Axlsx
# The Parser module mixes in a number of methods to help in generating a model from xml
module Parser
attr_accessor :parser_xml

def parse_string attr_name, xpath
send("#{attr_name.to_s}=", parse_value(xpath))
end

def parse_symbol attr_name, xpath
v = parse_value xpath
v = v.to_sym unless v.nil?
send("#{attr_name.to_s}=", v)
end

def parse_integer attr_name, xpath
v = parse_value xpath
v = v.to_i if v.respond_to?(:to_i)
send("#{attr_name.to_s}=", v)
end

def parse_float attr_name, xpath
v = parse_value xpath
v = v.to_f if v.respond_to?(:to_f)
send("#{attr_name.to_s}=", v)
end

def parse_value xpath
node = parser_xml.xpath(xpath)
return nil if node.empty?
node.text.strip
end

end
end
11 changes: 10 additions & 1 deletion lib/axlsx/workbook/workbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,16 @@ def styles

# Indicates if the epoc date for serialization should be 1904. If false, 1900 is used.
@@date1904 = false


# lets come back to this later when we are ready for parsing.
#def self.parse entry
# io = entry.get_input_stream
# w = self.new
# w.parser_xml = Nokogiri::XML(io.read)
# w.parse_string :date1904, "//xmlns:workbookPr/@date1904"
# w
#end

# Creates a new Workbook
# @option options [Boolean] date1904
def initialize(options={})
Expand Down
8 changes: 5 additions & 3 deletions test/tc_package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def setup
ws = @package.workbook.add_worksheet
chart = ws.add_chart Axlsx::Pie3DChart
chart.add_series :data=>[1,2,3], :labels=>["a", "b", "c"]
@fname = 'axlsx_test_serialization.xlsx'

end

def test_default_objects_are_created
Expand All @@ -20,10 +22,10 @@ def test_serialization
fname = 'axlsx_test_serialization.xlsx'
assert_nothing_raised do
begin
z= @package.serialize(fname)
zf = Zip::ZipFile.open(fname)
z= @package.serialize(@fname)
zf = Zip::ZipFile.open(@fname)
@package.send(:parts).each{ |part| zf.get_entry(part[:entry]) }
File.delete(fname)
File.delete(@fname)
rescue Errno::EACCES
puts "WARNING:: test_serialization requires write access."
end
Expand Down

0 comments on commit 3def8f8

Please sign in to comment.