require_relative 'spec_helper' describe Worksheet do simple_worksheet_xml =< IF(A2="Hello","hello",Sheet2!B4)hello24 END simple_worksheet_ruby =< true) worksheet.name = 'sheet1' worksheet.to_ruby.should == simple_worksheet_ruby worksheet.to_test.should == simple_worksheet_test end named_reference_simple_worksheet_ruby =< true) worksheet.named_references['reference_one'] = 'sheet2.a1' worksheet.name = 'sheet1' worksheet.to_ruby.should == named_reference_simple_worksheet_ruby end shared_formula_worksheet_xml =< 1A1*22A2*248163264128256A9*2512 END shared_formula_worksheet_ruby =< true) worksheet.to_ruby.should == shared_formula_worksheet_ruby worksheet.to_test.should == shared_formula_worksheet_test end array_formula_worksheet_xml =< 1A1:A5122334455 END array_formula_worksheet_ruby =< true) worksheet.to_ruby.should == array_formula_worksheet_ruby worksheet.to_test.should == array_formula_worksheet_test end table_xml =<<-END FirstTable[[#This Row],[ColA]]+FirstTable[[#This Row],[ColC]] AVERAGE(FirstTable[ColB]) 3*FirstTable[[#This Row],[ColA]]
END table_worksheet_xml =< 0 1 2 FirstTable[[#Headers],[ColB]] ColB +MATCH("ColC",FirstTable[#Headers]) 3 1 FirstTable[[#This Row],[ColA]]+FirstTable[[#This Row],[ColC]] 4 3*FirstTable[[#This Row],[ColA]] 3 SUM(FirstTable[#This Row]) 8 MATCH(8,FirstTable[[#All],[ColB]]) 3 2 FirstTable[[#This Row],[ColA]]+FirstTable[[#This Row],[ColC]] 8 3*FirstTable[[#This Row],[ColA]] 6 FirstTable[[#This Row],[ColB]] 8 3 FirstTable[[#This Row],[ColA]]+FirstTable[[#This Row],[ColC]] 12 3*FirstTable[[#This Row],[ColA]] 9 3 AVERAGE(FirstTable[ColB]) 8 SUBTOTAL(103,FirstTable[ColC]) 3 SUM(FirstTable[ColA]) 6 FirstTable[[#Totals],[ColB]] 8 FirstTable[[#Totals],[ColC]] 3 END table_worksheet_shared_strings =< ColAColBColCTotal END table_worksheet_ruby =< true, :worksheets => { 'sheet1' => worksheet}) SharedStrings.instance.clear SharedStrings.instance.load_strings_from_xml(Nokogiri::XML(table_worksheet_shared_strings)) worksheet.name = 'sheet1' worksheet.to_ruby.should == table_worksheet_ruby worksheet.to_test.should == table_worksheet_test end table_worksheet_relationships =< END it 'should have a Worksheet#from_file method that also loads the tables' do { '/usr/local/excel/xl/worksheets/sheet1.xml' => table_worksheet_xml, '/usr/local/excel/xl/worksheets/_rels/sheet1.xml.rels' => table_worksheet_relationships, '/usr/local/excel/xl/tables/table1.xml' => table_xml }.each do |filename,xml| File.should_receive(:open).with(filename).and_yield(StringIO.new(xml)) end File.should_receive(:exist?).with('/usr/local/excel/xl/worksheets/_rels/sheet1.xml.rels').and_return(true) Table.tables.clear worksheet = Worksheet.from_file('/usr/local/excel/xl/worksheets/sheet1.xml') SharedStrings.instance.clear SharedStrings.instance.load_strings_from_xml(Nokogiri::XML(table_worksheet_shared_strings)) worksheet.workbook = mock(:workbook,:indirects_used => true, :worksheets => { 'sheet1' => worksheet}) worksheet.name = 'sheet1' worksheet.to_ruby.should == table_worksheet_ruby end end