robust_excel_ole 1.2 → 1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1155,14 +1155,14 @@ describe Book do
1155
1155
  book1 = Book.open(@simple_file1, :calculation => :manual)
1156
1156
  Book.unobtrusively(@simple_file1) do |book|
1157
1157
  end
1158
- book1.excel.Calculation.should == -4135
1158
+ book1.excel.Calculation.should == XlCalculationManual
1159
1159
  end
1160
1160
 
1161
1161
  it "should remain calculation automatic" do
1162
1162
  book1 = Book.open(@simple_file1, :calculation => :automatic)
1163
1163
  Book.unobtrusively(@simple_file1) do |book|
1164
1164
  end
1165
- book1.excel.Calculation.should == -4105
1165
+ book1.excel.Calculation.should == XlCalculationAutomatic
1166
1166
  end
1167
1167
 
1168
1168
  end
Binary file
Binary file
Binary file
data/spec/excel_spec.rb CHANGED
@@ -998,29 +998,20 @@ module RobustExcelOle
998
998
 
999
999
  it "should return two unsaved books" do
1000
1000
  book = Book.open(@simple_file)
1001
- sheet = book.sheet(1)
1002
- sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
1003
1001
  book2 = Book.open(@another_simple_file, :force_excel => :new)
1004
- sheet2 = book2.sheet(1)
1005
- sheet2[1,1] = sheet2[1,1].value == "foo" ? "bar" : "foo"
1002
+ open_books = [book, book2]
1003
+ begin
1004
+ open_books.each do |wb|
1005
+ sheet = wb.sheet(1)
1006
+ sheet[1,1] = (sheet[1,1].value == "foo") ? "bar" : "foo"
1007
+ end
1006
1008
  #Excel.unsaved_known_workbooks.should == [[book.ole_workbook], [book2.ole_workbook]]
1007
- unsaved_known_wbs = Excel.unsaved_known_workbooks
1008
- unsaved_known_wbs.size.should == 2
1009
- ole_wb_list1, ole_wb_list2 = unsaved_known_wbs
1010
- ole_wb_list1.each do |ole_wb_list|
1011
- ole_wb_list1.size.should == 1
1012
- ole_wb_list1.each do |ole_workbook|
1013
- ole_workbook.Fullname.tr('\\','/').should == @simple_file
1014
- end
1015
- end
1016
- ole_wb_list2.each do |ole_wb_list|
1017
- ole_wb_list2.size.should == 1
1018
- ole_wb_list2.each do |ole_workbook|
1019
- ole_workbook.Fullname.tr('\\','/').should == @another_simple_file
1020
- end
1009
+ unsaved_known_wbs = Excel.unsaved_known_workbooks
1010
+ unsaved_known_wbs.size.should == 2
1011
+ unsaved_known_wbs.flatten.map{|ole_wb| ole_wb.Fullname.tr('\\','/') }.sort.should == open_books.map{|b| b.filename}.sort
1012
+ ensure
1013
+ open_books.each {|wb| wb.close(:if_unsaved => :forget)}
1021
1014
  end
1022
- book2.close(:if_unsaved => :forget)
1023
- book.close(:if_unsaved => :forget)
1024
1015
  end
1025
1016
 
1026
1017
  end
@@ -1504,7 +1495,7 @@ module RobustExcelOle
1504
1495
  old_calculation_mode = @excel1.Calculation
1505
1496
  @excel1.with_calculation(:manual) do
1506
1497
  @excel1.calculation.should == :manual
1507
- @excel1.Calculation.should == -4135
1498
+ @excel1.Calculation.should == XlCalculationManual
1508
1499
  @excel1.CalculateBeforeSave.should be false
1509
1500
  book.Saved.should be true
1510
1501
  end
@@ -1512,7 +1503,7 @@ module RobustExcelOle
1512
1503
  @excel1.CalculateBeforeSave.should be false
1513
1504
  @excel1.with_calculation(:automatic) do
1514
1505
  @excel1.calculation.should == :automatic
1515
- @excel1.Calculation.should == -4105
1506
+ @excel1.Calculation.should == XlCalculationAutomatic
1516
1507
  @excel1.CalculateBeforeSave.should be false
1517
1508
  book.Saved.should be false
1518
1509
  end
@@ -1526,7 +1517,7 @@ module RobustExcelOle
1526
1517
  book.Windows(book.Name).Visible = true
1527
1518
  @excel1.calculation = :manual
1528
1519
  @excel1.calculation.should == :manual
1529
- @excel1.Calculation.should == -4135
1520
+ @excel1.Calculation.should == XlCalculationManual
1530
1521
  @excel1.CalculateBeforeSave.should be false
1531
1522
  book.Saved.should be true
1532
1523
  end
@@ -1536,7 +1527,7 @@ module RobustExcelOle
1536
1527
  book = Book.open(@simple_file, :visible => true)
1537
1528
  @excel1.calculation = :automatic
1538
1529
  @excel1.calculation.should == :automatic
1539
- @excel1.Calculation.should == -4105
1530
+ @excel1.Calculation.should == XlCalculationAutomatic
1540
1531
  @excel1.CalculateBeforeSave.should be false
1541
1532
  book.Saved.should be false
1542
1533
  end
@@ -1544,24 +1535,24 @@ module RobustExcelOle
1544
1535
  it "should set Calculation without workbooks" do
1545
1536
  @excel1 = Excel.new
1546
1537
  expect{
1547
- @excel1.Calculation = -4135
1538
+ @excel1.Calculation = XlCalculationManual
1548
1539
  }.to raise_error(WIN32OLERuntimeError)
1549
1540
  end
1550
1541
 
1551
1542
  it "should do Calculation to manual with workbook" do
1552
1543
  @excel1 = Excel.new
1553
1544
  b = Book.open(@simple_file)
1554
- @excel1.Calculation = -4135
1545
+ @excel1.Calculation = XlCalculationManual
1555
1546
  @excel1.calculation.should == :manual
1556
- @excel1.Calculation.should == -4135
1547
+ @excel1.Calculation.should == XlCalculationManual
1557
1548
  end
1558
1549
 
1559
1550
  it "should do Calculation to automatic with workbook" do
1560
1551
  @excel1 = Excel.new
1561
1552
  b = Book.open(@simple_file)
1562
- @excel1.Calculation = -4105
1553
+ @excel1.Calculation = XlCalculationAutomatic
1563
1554
  @excel1.calculation.should == :automatic
1564
- @excel1.Calculation.should == -4105
1555
+ @excel1.Calculation.should == XlCalculationAutomatic
1565
1556
  end
1566
1557
 
1567
1558
  end
@@ -1591,6 +1582,9 @@ module RobustExcelOle
1591
1582
 
1592
1583
  before do
1593
1584
  @excel = Excel.new(:reuse => false)
1585
+ book = Book.open(@simple_file)
1586
+ book.excel.calculation = :manual
1587
+ book.close(:if_unsaved => :save)
1594
1588
  end
1595
1589
 
1596
1590
  it "should set options in the Excel instance" do
@@ -1599,7 +1593,7 @@ module RobustExcelOle
1599
1593
  @excel.Visible.should be true
1600
1594
  @excel.ScreenUpdating.should be true
1601
1595
  book = Book.open(@simple_file)
1602
- @excel.Calculation.should == -4135
1596
+ @excel.Calculation.should == XlCalculationManual
1603
1597
  book.close
1604
1598
  end
1605
1599
 
@@ -1799,6 +1793,57 @@ module RobustExcelOle
1799
1793
  end
1800
1794
  end
1801
1795
 
1796
+ context "setting the name of a range" do
1797
+
1798
+ before do
1799
+ @book1 = Book.open(@dir + '/another_workbook.xls', :read_only => true, :visible => true)
1800
+ @book1.excel.displayalerts = false
1801
+ @excel1 = @book1.excel
1802
+ end
1803
+
1804
+ after do
1805
+ @book1.close
1806
+ end
1807
+
1808
+ it "should name an unnamed range with a giving address" do
1809
+ @excel1.add_name("foo",1,2)
1810
+ @excel1.Names.Item("foo").Name.should == "foo"
1811
+ @excel1.Names.Item("foo").Value.should == "=Sheet1!$B$1"
1812
+ end
1813
+
1814
+ it "should rename an already named range with a giving address" do
1815
+ @excel1.add_name("foo",1,1)
1816
+ @excel1.Names.Item("foo").Name.should == "foo"
1817
+ @excel1.Names.Item("foo").Value.should == "=Sheet1!$A$1"
1818
+ end
1819
+
1820
+ it "should raise an error" do
1821
+ expect{
1822
+ @excel1.add_name("foo", -2, 1)
1823
+ }.to raise_error(RangeNotEvaluatable, /cannot add name "foo" to cell with row -2 and column 1/)
1824
+ end
1825
+
1826
+ it "should rename a range" do
1827
+ @excel1.add_name("foo",1,1)
1828
+ @excel1.rename_range("foo","bar")
1829
+ @excel1.namevalue_glob("bar").should == "foo"
1830
+ end
1831
+
1832
+ it "should delete a name of a range" do
1833
+ @excel1.add_name("foo",1,1)
1834
+ @excel1.delete_name("foo")
1835
+ expect{
1836
+ @excel1.namevalue_glob("foo")
1837
+ }.to raise_error(NameNotFound, /name "foo"/)
1838
+ end
1839
+
1840
+ it "should add a name of a rectangular range" do
1841
+ @excel1.add_name("foo",1,1,3,4)
1842
+ @excel1["foo"].should == [["foo", "workbook", "sheet1", nil], ["foo", 1.0, 2.0, 4.0], ["matz", 3.0, 4.0, 4.0]]
1843
+ end
1844
+ end
1845
+
1846
+
1802
1847
  describe "nameval, set_nameval" do
1803
1848
 
1804
1849
  before do
@@ -1812,25 +1857,25 @@ module RobustExcelOle
1812
1857
  end
1813
1858
 
1814
1859
  it "should return value of a defined name" do
1815
- @excel1.nameval("firstcell").should == "foo"
1860
+ @excel1.namevalue_glob("firstcell").should == "foo"
1816
1861
  @excel1["firstcell"].should == "foo"
1817
1862
  end
1818
1863
 
1819
1864
  #it "should evaluate a formula" do
1820
- # @excel1.nameval("named_formula").should == 4
1865
+ # @excel1.namevalue_glob("named_formula").should == 4
1821
1866
  # @excel1["named_formula"].should == 4
1822
1867
  #end
1823
1868
 
1824
1869
  it "should raise an error if name not defined" do
1825
1870
  expect {
1826
- @excel1.nameval("foo")
1871
+ @excel1.namevalue_glob("foo")
1827
1872
  }.to raise_error(NameNotFound, /name "foo"/)
1828
1873
  expect {
1829
1874
  @excel1["foo"]
1830
1875
  }.to raise_error(NameNotFound, /name "foo"/)
1831
1876
  expect {
1832
1877
  excel2 = Excel.create
1833
- excel2.nameval("one")
1878
+ excel2.namevalue_glob("one")
1834
1879
  }.to raise_error(NameNotFound, /name "one"/)
1835
1880
  expect {
1836
1881
  excel3 = Excel.create(:visible => true)
@@ -1839,16 +1884,16 @@ module RobustExcelOle
1839
1884
  end
1840
1885
 
1841
1886
  it "should set a range to a value" do
1842
- @excel1.nameval("firstcell").should == "foo"
1843
- @excel1.set_nameval("firstcell","bar")
1844
- @excel1.nameval("firstcell").should == "bar"
1887
+ @excel1.namevalue_glob("firstcell").should == "foo"
1888
+ @excel1.set_namevalue_glob("firstcell","bar")
1889
+ @excel1.namevalue_glob("firstcell").should == "bar"
1845
1890
  @excel1["firstcell"] = "foo"
1846
- @excel1.nameval("firstcell").should == "foo"
1891
+ @excel1.namevalue_glob("firstcell").should == "foo"
1847
1892
  end
1848
1893
 
1849
1894
  it "should raise an error if name cannot be evaluated" do
1850
1895
  expect{
1851
- @excel1.set_nameval("foo", 1)
1896
+ @excel1.set_namevalue_glob("foo", 1)
1852
1897
  }.to raise_error(NameNotFound, /name "foo"/)
1853
1898
  expect{
1854
1899
  @excel1["foo"] = 1
@@ -1856,9 +1901,9 @@ module RobustExcelOle
1856
1901
  end
1857
1902
 
1858
1903
  it "should color the cell" do
1859
- @excel1.set_nameval("firstcell", "foo")
1904
+ @excel1.set_namevalue_glob("firstcell", "foo")
1860
1905
  @book1.Names.Item("firstcell").RefersToRange.Interior.ColorIndex.should == -4142
1861
- @excel1.set_nameval("firstcell", "foo", :color => 4)
1906
+ @excel1.set_namevalue_glob("firstcell", "foo", :color => 4)
1862
1907
  @book1.Names.Item("firstcell").RefersToRange.Interior.ColorIndex.should == 4
1863
1908
  @excel1["firstcell"].should == "foo"
1864
1909
  @excel1["firstcell"] = "foo"
@@ -1868,7 +1913,7 @@ module RobustExcelOle
1868
1913
 
1869
1914
  end
1870
1915
 
1871
- describe "rangeval, set_rangeval" do
1916
+ describe "namevalue, set_namevalue" do
1872
1917
 
1873
1918
  before do
1874
1919
  @book1 = Book.open(@dir + '/another_workbook.xls')
@@ -1880,49 +1925,49 @@ module RobustExcelOle
1880
1925
  end
1881
1926
 
1882
1927
  it "should return value of a locally defined name" do
1883
- @excel1.rangeval("firstcell").should == "foo"
1928
+ @excel1.namevalue("firstcell").should == "foo"
1884
1929
  end
1885
1930
 
1886
1931
  it "should return value of a defined name" do
1887
- @excel1.rangeval("new").should == "foo"
1888
- @excel1.rangeval("one").should == 1.0
1889
- @excel1.rangeval("four").should == [[1,2],[3,4]]
1890
- @excel1.rangeval("firstrow").should == [[1,2]]
1932
+ @excel1.namevalue("new").should == "foo"
1933
+ @excel1.namevalue("one").should == 1.0
1934
+ @excel1.namevalue("four").should == [[1,2],[3,4]]
1935
+ @excel1.namevalue("firstrow").should == [[1,2]]
1891
1936
  end
1892
1937
 
1893
1938
  it "should return default value if name not defined and default value is given" do
1894
- @excel1.rangeval("foo", :default => 2).should == 2
1939
+ @excel1.namevalue("foo", :default => 2).should == 2
1895
1940
  end
1896
1941
 
1897
1942
  it "should raise an error if name not defined for the sheet" do
1898
1943
  expect {
1899
- @excel1.rangeval("foo")
1944
+ @excel1.namevalue("foo")
1900
1945
  }.to raise_error(NameNotFound, /name "foo" not in/)
1901
1946
  expect {
1902
- @excel1.rangeval("named_formula")
1947
+ @excel1.namevalue("named_formula")
1903
1948
  }.to raise_error(NameNotFound, /name "named_formula" not in/)
1904
1949
  expect {
1905
1950
  excel2 = Excel.create
1906
- excel2.rangeval("one")
1951
+ excel2.namevalue("one")
1907
1952
  }.to raise_error(NameNotFound, /name "one" not in/)
1908
1953
  end
1909
1954
 
1910
1955
  it "should set a range to a value" do
1911
- @excel1.rangeval("firstcell").should == "foo"
1912
- @excel1.set_rangeval("firstcell","bar")
1913
- @excel1.rangeval("firstcell").should == "bar"
1956
+ @excel1.namevalue("firstcell").should == "foo"
1957
+ @excel1.set_namevalue("firstcell","bar")
1958
+ @excel1.namevalue("firstcell").should == "bar"
1914
1959
  end
1915
1960
 
1916
1961
  it "should raise an error if name cannot be evaluated" do
1917
1962
  expect{
1918
- @excel1.set_nameval("foo", 1)
1963
+ @excel1.set_namevalue_glob("foo", 1)
1919
1964
  }.to raise_error(NameNotFound, /name "foo" not in/)
1920
1965
  end
1921
1966
 
1922
1967
  it "should color the cell" do
1923
- @excel1.set_rangeval("firstcell", "foo")
1968
+ @excel1.set_namevalue("firstcell", "foo")
1924
1969
  @book1.Names.Item("firstcell").RefersToRange.Interior.ColorIndex.should == -4142
1925
- @excel1.set_rangeval("firstcell", "foo", :color => 4)
1970
+ @excel1.set_namevalue("firstcell", "foo", :color => 4)
1926
1971
  @book1.Names.Item("firstcell").RefersToRange.Interior.ColorIndex.should == 4
1927
1972
  end
1928
1973
 
data/spec/general_spec.rb CHANGED
@@ -54,8 +54,8 @@ module RobustExcelOle
54
54
  @ole_sheet_methods = []
55
55
  # ["Activate", "Calculate", "Copy", "Name", "Select", "Evaluate", "Protect", "Unprotect"]
56
56
  @sheet_methods = ["book_class", "col_range", "each", "each_column", "each_column_with_index",
57
- "each_row", "each_row_with_index", "nameval", "rangeval",
58
- "set_rangeval", "row_range", "set_nameval"]
57
+ "each_row", "each_row_with_index", "nameval", "namevalue",
58
+ "set_namevalue", "row_range", "set_nameval"]
59
59
  end
60
60
 
61
61
  after do
data/spec/range_spec.rb CHANGED
@@ -1,4 +1,4 @@
1
- # -*- cdoing: utf-8 -*-
1
+ # -*- coding: utf-8 -*-
2
2
  require File.join(File.dirname(__FILE__), './spec_helper')
3
3
 
4
4
  include RobustExcelOle
@@ -119,6 +119,42 @@ describe RobustExcelOle::Range do
119
119
  end
120
120
  end
121
121
 
122
+ describe "#copy" do
123
+
124
+ before do
125
+ @book2 = Book.open(@dir + '/different_workbook.xls')
126
+ @sheet2 = @book.sheet(1)
127
+ @range2 = @sheet2.range(1..2,1..3)
128
+ @book3 = Book.open(@dir + '/another_workbook.xls', :force => {:excel => :new})
129
+ end
130
+
131
+ after do
132
+ @book.close(:if_unsaved => :forget)
133
+ end
134
+
135
+ it "should copy range at a cell" do
136
+ @range2.copy(4,4)
137
+ @sheet2.range(4..5,4..6).values.should == ["foo", "workbook", "sheet1", "foo", nil, "foobaaa"]
138
+ end
139
+
140
+ it "should copy range into a certain worksheet of another workbook" do
141
+ @range2.copy(4,4,@book2.sheet(3))
142
+ @book2.sheet(3).range(4..5,4..6).values.should == ["foo", "workbook", "sheet1", "foo", nil, "foobaaa"]
143
+ end
144
+
145
+ it "should copy range at a cell into a worksheet in another Excel instance" do
146
+ Excel.kill_all
147
+ sleep 1
148
+ book1 = Book.open(@dir + '/workbook.xls', :force_excel => :new)
149
+ book2 = Book.open(@dir + '/different_workbook.xls', :force_excel => :new)
150
+ sheet1 = book1.sheet(1)
151
+ range1 = sheet1.range(1..2,1..3)
152
+ range1.copy(4,4,book2.sheet(1))
153
+ book2.sheet(1).range(4..5,4..6).values.should == ["foo", "workbook", "sheet1", "foo", nil, "foobaaa"]
154
+ end
155
+
156
+ end
157
+
122
158
  describe "#method_missing" do
123
159
  it "can access COM method" do
124
160
  @range.Range(@range.Cells.Item(1), @range.Cells.Item(3)).value.should eq [@range.values(0..2)]
@@ -33,7 +33,94 @@ module RobustExcelOle
33
33
 
34
34
  after do
35
35
  Excel.kill_all
36
- #rm_tmp(@dir)
36
+ rm_tmp(@dir)
37
+ end
38
+
39
+ describe "Address" do
40
+
41
+ it "should read a1-format" do
42
+ address = Address.new("A1")
43
+ address.rows.should == 1 .. 1
44
+ address.columns.should == "A" .."A"
45
+ address.a1format.should be_true
46
+ end
47
+
48
+ it "should read a1-format" do
49
+ address = Address.new("ABO15")
50
+ address.rows.should == 15..15
51
+ address.columns.should == "ABO".."ABO"
52
+ address.a1format.should be_true
53
+ end
54
+
55
+ it "should read a1-format when row and column are given separated" do
56
+ address = Address.new("A",1)
57
+ address.rows.should == 1..1
58
+ address.columns.should == "A".."A"
59
+ address.a1format.should be_true
60
+ end
61
+
62
+ it "should read a1-format with rows as integer range" do
63
+ address = Address.new("AB", 2..4)
64
+ address.rows.should == 2..4
65
+ address.columns.should == "AB".."AB"
66
+ address.a1format.should be_true
67
+ end
68
+
69
+ it "should read a1-format with columns as string range" do
70
+ address = Address.new("A".."C", 2)
71
+ address.rows.should == 2
72
+ address.columns.should == "A".."C"
73
+ address.a1format.should be_true
74
+ end
75
+
76
+ it "should read a1-format with rows and columns as string range" do
77
+ address = Address.new("A".."C", 2..6)
78
+ address.rows.should == 2..6
79
+ address.columns.should == "A".."C"
80
+ address.a1format.should be_true
81
+ end
82
+
83
+ it "should read r1c1-format" do
84
+ address = Address.new(1,2)
85
+ address.rows.should == 1 .. 1
86
+ address.columns.should == 2 .. 2
87
+ address.a1format.should be_false
88
+ end
89
+
90
+ it "should read r1c1-format with rows as integer range" do
91
+ address = Address.new(1..2,3)
92
+ address.rows.should == 1..2
93
+ address.columns.should == 3..3
94
+ address.a1format.should be_false
95
+ end
96
+
97
+ it "should read r1c1-format with columns as integer range" do
98
+ address = Address.new(1,3..5)
99
+ address.rows.should == 1..1
100
+ address.columns.should == 3..5
101
+ address.a1format.should be_false
102
+ end
103
+
104
+ it "should read r1c1-format with rows and columns as integer range" do
105
+ address = Address.new(1..4,3..5)
106
+ address.rows.should == 1..4
107
+ address.columns.should == 3..5
108
+ address.a1format.should be_false
109
+ end
110
+
111
+ it "should raise an error" do
112
+ expect{
113
+ Address.new("1A")
114
+ }.to raise_error(AddressInvalid, /address ("1A") not in A1 format/)
115
+ expect{
116
+ Address.new("A1B")
117
+ }.to raise_error(AddressInvalid, /address ("A1B") not in A1 format/)
118
+ expect{
119
+ Address.new("A".."B","C".."D")
120
+ }.to raise_error(AddressInvalid, /address ("A".."B", "C".."D") not in A1 format/)
121
+ end
122
+
123
+
37
124
  end
38
125
 
39
126
  describe "trace" do
@@ -57,8 +144,8 @@ module RobustExcelOle
57
144
  ["Activate", "ActiveSheet", "Application", "Close", "FullName", "HasPassword", "Name", "Names",
58
145
  "Password", "Protect", "ProtectSharing", "ProtectStructure", "Protect", "ReadOnly", "Save",
59
146
  "SaveAs", "Saved", "Sheets", "Unprotect"]
60
- @book_methods = ["focus", "add_sheet", "alive?", "close", "filename", "nameval", "ole_object",
61
- "ole_workbook", "reopen", "save", "save_as", "saved", "set_nameval"]
147
+ @book_methods = ["focus", "add_sheet", "alive?", "close", "filename", "namevalue", "ole_object",
148
+ "ole_workbook", "reopen", "save", "save_as", "saved", "set_namevalue"]
62
149
  @ole_excel_methods =
63
150
  ["ActiveCell", "ActiveSheet", "ActiveWorkbook", "Application", "Calculate", "Cells", "Columns",
64
151
  "DisplayAlerts", "Evaluate", "Hwnd", "Name", "Names", "Quit", "Range", "Ready", "Save",