require_relative 'spec_helper' describe Workbook do workbook_xml =< Sheet2!$A$2:$A$3 Sheet2!$A$3 'First sheet'!$A$1 Sheet2!$A$2 END workbook_relationship_xml =< END simple_worksheet_xml =< IF(A2="Hello","hello",Sheet2!B4) hello 0 END simple_worksheet_ruby =< Hello END workbook_ruby =<"sheet1", "Sheet2"=>"sheet2", "Sheet3"=>"sheet3"} @workbook_tables = {} end def one_and2; sheet2.a('a2','a3'); end def reference_2; sheet2.a3; end def reference_one; sheet2.a2; end end Dir[File.join(File.dirname(__FILE__),"sheets/","sheet*.rb")].each {|f| Spreadsheet.autoload(File.basename(f,".rb").capitalize,f)} END workbook_no_indirects_ruby =< workbook_xml, '/usr/local/excel/xl/_rels/workbook.xml.rels' => workbook_relationship_xml, '/usr/local/excel/xl/worksheets/sheet1.xml' => simple_worksheet_xml, '/usr/local/excel/xl/worksheets/sheet2.xml' => simple_worksheet_xml, '/usr/local/excel/xl/worksheets/sheet3.xml' => simple_worksheet_xml, '/usr/local/excel/xl/sharedStrings.xml' => shared_strings_xml, '/usr/local/excel/xl/worksheets/_rels/sheet1.xml.rels' => '', '/usr/local/excel/xl/worksheets/_rels/sheet2.xml.rels' => '', '/usr/local/excel/xl/worksheets/_rels/sheet3.xml.rels' => '' }.each do |filename,xml| File.should_receive(:open).with(filename).and_yield(StringIO.new(xml)) end File.stub!(:exist?).and_return(true) workbook = Workbook.new('/usr/local/excel/xl/workbook.xml') workbook.worksheets['sheet1'].to_ruby.should == simple_worksheet_ruby SheetNames.instance['First sheet'].should == 'sheet1' workbook.to_ruby.should == workbook_no_indirects_ruby workbook.indirects_used = true workbook.to_ruby.should == workbook_ruby end end describe Worksheet, "in pruning mode" do pruning_workbook_xml =< Inputs!$A$3 END pruning_workbook_relationships_xml =< END pruning_shared_strings_xml =< ResultInputNot in resultIn resultInputsDoesn't depend on an input END pruning_output_sheet_xml =< 0Calcs!A11211Inputs!A199Calcs!C9In resultCalcs!C13Doesn't depend on an input END pruning_calc_sheet_xml =< SUM(A2:A7)121122Inputs!A1*10990Inputs!A1994C6101054INDIRECT("'"&C8&"'!In_result")In resultINDIRECT("'"&C8&"'!A2")Not in resultD13Doesn't depend on an input5 END pruning_input_sheet_xml =< 9923 END pruning_calc_sheet_ruby_no_prune =< pruning_workbook_xml, '/usr/local/excel/xl/_rels/workbook.xml.rels' => pruning_workbook_relationships_xml, '/usr/local/excel/xl/worksheets/sheet1.xml' => pruning_output_sheet_xml, '/usr/local/excel/xl/worksheets/sheet2.xml' => pruning_calc_sheet_xml, '/usr/local/excel/xl/worksheets/sheet3.xml' => pruning_input_sheet_xml, '/usr/local/excel/xl/sharedStrings.xml' => pruning_shared_strings_xml, '/usr/local/excel/xl/worksheets/_rels/sheet1.xml.rels' => '', '/usr/local/excel/xl/worksheets/_rels/sheet2.xml.rels' => '', '/usr/local/excel/xl/worksheets/_rels/sheet3.xml.rels' => '' }.each do |filename,xml| File.should_receive(:open).with(filename).and_yield(StringIO.new(xml)) end File.stub!(:exist?).and_return(true) @workbook = Workbook.new('/usr/local/excel/xl/workbook.xml') end it "should work normally when no output sheets are identified" do @workbook.total_cells.should == 24 @workbook.worksheets['sheet2'].to_ruby.should == pruning_calc_sheet_ruby_no_prune end it "should prune any cells that aren't needed for the output sheet calculations when output sheets have been specified" do @workbook.prune_cells_not_needed_for_output_sheets('Outputs') @workbook.worksheets['sheet2'].to_ruby.should == pruning_calc_sheet_ruby_output_prune end it "should convert cells to values where they don't depend on inputs, and then prune" do @workbook.prune_cells_not_needed_for_output_sheets('Outputs') @workbook.convert_cells_to_values_when_independent_of_input_sheets('Inputs') @workbook.worksheets['sheet2'].to_ruby.should == pruning_calc_sheet_ruby_input_and_output_prune end end