-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalc.java
More file actions
1912 lines (1383 loc) · 55.3 KB
/
Copy pathCalc.java
File metadata and controls
1912 lines (1383 loc) · 55.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Calc.java
// Andrew Davison, ad@fivedots.coe.psu.ac.th, September 2014
/* A growing collection of utility functions to make Office
easier to use. They are currently divided into the following
groups:
* document methods
* sheet methods
* view methods
* view data methods
* insert/remove rows, columns, cells
* get/set values in cells, arrays, rows, columns
* get XCell and XCellRange methods
* convert cell/cellrange names to positions
* get cell and range addresses
* convert cell ranges to strings
* search
* cell decoration
* scenarios
* data pilot methods
* using calc functions
* solver methods
* headers /footers
*/
import java.awt.Point;
import java.util.*;
import java.util.regex.*;
import com.sun.star.beans.*;
import com.sun.star.frame.*;
import com.sun.star.lang.*;
import com.sun.star.text.*;
import com.sun.star.uno.*;
import com.sun.star.util.*;
import com.sun.star.container.*;
import com.sun.star.sheet.*;
import com.sun.star.style.*;
import com.sun.star.table.*;
import com.sun.star.view.*;
import com.sun.star.uno.Exception;
import com.sun.star.io.IOException;
public class Calc
{
// for headers and footers
public static final int HF_LEFT = 0;
public static final int HF_CENTER = 1;
public static final int HF_RIGHT = 2;
// for zooming
public static final short OPTIMAL = 0;
public static final short PAGE_WIDTH = 1;
public static final short ENTIRE_PAGE = 2;
// public static final short BY_VALUE = 3;
public static final short PAGE_WIDTH_EXACT = 4;
// for border decoration (bitwise composition is possible)
public static final int TOP_BORDER = 0x01;
public static final int BOTTOM_BORDER = 0x02;
public static final int LEFT_BORDER = 0x04;
public static final int RIGHT_BORDER = 0x08;
// largest value used in XCellSeries.fillSeries
public static final int MAX_VALUE = 0x7FFFFFFF;
// use a better name when date mode doesn't matter
public static final FillDateMode NO_DATE = FillDateMode.FILL_DATE_DAY;
// some hex values for commonly used colors
public static final int BLACK = 0x000000;
public static final int WHITE = 0xFFFFFF;
public static final int RED = 0xFF0000;
public static final int GREEN = 0x00FF00;
public static final int BLUE = 0x0000FF;
public static final int YELLOW = 0xFFFF00;
public static final int ORANGE = 0xFFA500;
public static final int DARK_BLUE = 0x003399;
public static final int LIGHT_BLUE = 0x99CCFF;
public static final int PALE_BLUE = 0xD6EBFF;
private static final com.sun.star.awt.Point CELL_POS =
new com.sun.star.awt.Point(3,4);
// --------------- document methods ------------------
public static XSpreadsheetDocument openDoc(String fnm, XComponentLoader loader)
{
XComponent doc = Lo.openDoc(fnm, loader);
if (doc == null) {
System.out.println("Document is null");
return null;
}
return getSSDoc(doc);
} // end of openDoc()
public static XSpreadsheetDocument getSSDoc(XComponent doc)
{
if (!Info.isDocType(doc, Lo.CALC_SERVICE)) {
System.out.println("Not a spreadsheet doc; closing");
Lo.closeDoc(doc);
return null;
}
XSpreadsheetDocument ssDoc = Lo.qi(XSpreadsheetDocument.class, doc);
if (ssDoc == null) {
System.out.println("Not a spreadsheet doc; closing");
Lo.closeDoc(doc);
return null;
}
return ssDoc;
} // end of getSSDoc()
public static XSpreadsheetDocument createDoc(XComponentLoader loader)
{ XComponent doc = Lo.createDoc("scalc", loader);
return Lo.qi(XSpreadsheetDocument.class, doc);
// XSpreadsheetDocument does not inherit XComponent!
}
/*
public static void closeDoc(XSpreadsheetDocument doc)
{ XCloseable closeable = Lo.qi(XCloseable.class, doc);
Lo.close(closeable);
}
public static void saveDoc(XSpreadsheetDocument doc, String fnm)
{ // XStorable store = Lo.qi(XStorable.class, doc);
XComponent doc = Lo.qi(XComponent.class, doc);
Lo.saveDoc(doc, fnm);
}
*/
// ------------------------ sheet methods -------------------------
public static XSpreadsheet getSheet(XSpreadsheetDocument doc, int index)
// return the spreadsheet with the specified index (0-based)
{
// System.out.println("Accessing spreadsheet " + index) ;
XSpreadsheets sheets = doc.getSheets();
XSpreadsheet sheet = null;
try {
XIndexAccess xSheetsIdx = Lo.qi(XIndexAccess.class, sheets);
// must convert since XSpreadsheet is a named container
sheet = Lo.qi(XSpreadsheet.class, xSheetsIdx.getByIndex(index));
}
catch (Exception e) {
System.out.println("Could not access spreadsheet: " + index);
}
return sheet;
} // end of getSheet()
public static XSpreadsheet getSheet(XSpreadsheetDocument doc, String sheetName)
// return the spreadsheet by name
{
// System.out.println("Accessing spreadsheet \"" + sheetName + "\"") ;
XSpreadsheets sheets = doc.getSheets();
XSpreadsheet sheet = null;
try {
sheet = Lo.qi(XSpreadsheet.class, sheets.getByName(sheetName));
}
catch (Exception e) {
System.out.println("Could not access spreadsheet: \"" + sheetName + "\"");
}
return sheet;
} // end of getSheet()
public static XSpreadsheet insertSheet(XSpreadsheetDocument doc, String name, short idx)
// Inserts a new empty spreadsheet with the specified name
{
XSpreadsheets sheets = doc.getSheets();
XSpreadsheet sheet = null;
try {
sheets.insertNewByName(name, idx);
sheet = Lo.qi(XSpreadsheet.class, sheets.getByName(name));
}
catch (Exception ex) {
System.out.println("Could not insert sheet: " + ex);
}
return sheet;
} // end of insertSheet()
public static boolean removeSheet(XSpreadsheetDocument doc, String name)
{
XSpreadsheets sheets = doc.getSheets();
try {
sheets.removeByName(name);
return true;
}
catch (Exception ex) {
System.out.println("Could not remove sheet: " + name);
return false;
}
} // end of removeSheet()
public static boolean moveSheet(XSpreadsheetDocument doc, String name, short idx)
{
XSpreadsheets sheets = doc.getSheets();
int numSheets = sheets.getElementNames().length;
if ((idx < 0) || (idx >= numSheets)) {
System.out.println("Index " + idx + " is out of range");
return false;
}
else {
sheets.moveByName(name, idx);
return true;
}
} // end of moveSheet()
public static String[] getSheetNames(XSpreadsheetDocument doc)
{ XSpreadsheets sheets = doc.getSheets();
return sheets.getElementNames();
}
public static String getSheetName(XSpreadsheet sheet)
{
XNamed xNamed = Lo.qi(XNamed.class, sheet);
if (xNamed == null){
System.out.println("Could not access spreadsheet name");
return null;
}
else
return xNamed.getName();
} // end of getSheetName()
public static void setSheetName(XSpreadsheet sheet, String name)
{
XNamed xNamed = Lo.qi(XNamed.class, sheet);
if (xNamed == null)
System.out.println("Could not access spreadsheet");
else
xNamed.setName(name);
} // end of setSheetName()
// ----------------- view methods --------------------------
public static XController getController(XSpreadsheetDocument doc)
{ XModel model = Lo.qi(XModel.class, doc);
return model.getCurrentController();
}
public static void zoomValue(XSpreadsheetDocument doc, int value)
// value constants are defined at the top of Calc
{
XController ctrl = getController(doc);
Props.setProperty(ctrl, "ZoomType", DocumentZoomType.BY_VALUE);
Props.setProperty(ctrl, "ZoomValue", (short)value);
// in SpreadsheetViewSettings
}
public static void zoom(XSpreadsheetDocument doc, short type)
{ XController ctrl = getController(doc);
Props.setProperty(ctrl, "ZoomType", type);
}
public static XSpreadsheetView getView(XSpreadsheetDocument doc)
{ return Lo.qi(XSpreadsheetView.class, getController(doc)); }
public static void setActiveSheet(XSpreadsheetDocument doc, XSpreadsheet sheet)
// bring the sheet to the foreground
{
XSpreadsheetView ssView = getView(doc);
ssView.setActiveSheet(sheet);
} // end of setActiveSheet()
public static XSpreadsheet getActiveSheet(XSpreadsheetDocument doc)
{ return getView(doc).getActiveSheet(); }
public static void freezeRows(XSpreadsheetDocument doc, int numRows)
{ freeze(doc, 0, numRows); }
public static void freezeCols(XSpreadsheetDocument doc, int numCols)
{ freeze(doc, numCols, 0); }
public static void freeze(XSpreadsheetDocument doc, int numCols, int numRows)
{ XViewFreezable xFreeze = Lo.qi(XViewFreezable.class, getController(doc));
xFreeze.freezeAtPosition(numCols, numRows);
} // end of freeze()
public static void gotoCell(XSpreadsheetDocument doc, String cellName)
{ XFrame frame = getController(doc).getFrame();
gotoCell(frame, cellName);
}
public static void gotoCell(XFrame frame, String cellName)
{ Lo.dispatchCmd(frame, "GoToCell", Props.makeProps("ToPoint", cellName)); }
public static void splitWindow(XSpreadsheetDocument doc, String cellName)
{
XFrame frame = getController(doc).getFrame();
//XViewSplitable viewSplit = Lo.qi(XViewSplitable.class, getController(doc));
//viewSplit.splitAtPosition(x*100, y*100);
//deprecated
gotoCell(frame, cellName);
Lo.dispatchCmd(frame, "SplitWindow", Props.makeProps("ToPoint", cellName));
} // end of splitWindow()
public static CellRangeAddress getSelectedAddr(XSpreadsheetDocument doc)
{ XModel model = Lo.qi(XModel.class, doc);
return getSelectedAddr(model);
}
public static CellRangeAddress getSelectedAddr(XModel model)
{
if (model == null) {
System.out.println("No document model found");
return null;
}
XCellRangeAddressable ra = Lo.qi(
XCellRangeAddressable.class, model.getCurrentSelection());
if (ra != null)
return ra.getRangeAddress();
else {
System.out.println("No range address found");
return null;
}
} // end of getSelectedAddr()
public static CellAddress getSelectedCellAddr(XSpreadsheetDocument doc)
// will return null if a range was selected
{
CellRangeAddress crAddr = getSelectedAddr(doc);
CellAddress addr = null;
if (Calc.isSingleCellRange(crAddr)) {
XSpreadsheet sheet = getActiveSheet(doc);
XCell cell = Calc.getCell(sheet, crAddr.StartColumn, crAddr.StartRow);
addr = Calc.getCellAddress(cell);
}
return addr;
} // end of getSelectedCellAddr()
// -------------------- view data methods ---------------------------------
public static XViewPane[] getViewPanes(XSpreadsheetDocument doc)
{
XIndexAccess con = Lo.qi(XIndexAccess.class, getController(doc));
if (con == null) {
System.out.println("Could not access the view pane container");
return null;
}
if (con.getCount() == 0) {
System.out.println("No view panes found");
return null;
}
// System.out.println("No of panes: " + con.getCount());
XViewPane[] panes = new XViewPane[con.getCount()];
for (int i=0; i < con.getCount(); i++) {
try {
panes[i] = Lo.qi(XViewPane.class, con.getByIndex(i));
}
catch(com.sun.star.uno.Exception e)
{ System.out.println("Could not get view pane " + i); }
}
return panes;
} // end of getViewPanes()
public static String getViewData(XSpreadsheetDocument doc)
{ XController ctrl = getController(doc);
return (String) ctrl.getViewData();
}
public static void setViewData(XSpreadsheetDocument doc, String viewData)
{ XController ctrl = getController(doc);
ctrl.restoreViewData(viewData);
}
public static ViewState[] getViewStates(XSpreadsheetDocument doc)
/* Extract the view states for all the sheets from the view data.
The states are returned as an array of ViewState objects.
The view data string has the format:
100/60/0;0;tw:879;0/4998/0/1/0/218/2/0/0/4988/4998
The view state info starts after the third ";", the fourth entry.
The view state for each sheet is separated by ";"s
Based on a post by user Hanya to:
https://forum.openoffice.org/en/forum/viewtopic.php?
f=45&t=29195&p=133202&hilit=getViewData#p133202
*/
{
XController ctrl = getController(doc);
String viewData = (String) ctrl.getViewData();
String[] viewParts = viewData.split(";");
if (viewParts.length < 4) {
System.out.println("No sheet view states found in view data");
return null;
}
ViewState[] states = new ViewState[viewParts.length-3];
for(int i=3; i < viewParts.length; i++)
states[i-3] = new ViewState(viewParts[i]);
return states;
} // end of getViewStates()
public static void setViewStates(XSpreadsheetDocument doc, ViewState[] states)
/* Update the sheet state part of the view data, which starts as
the 4th entry in the view data string
*/
{
XController ctrl = getController(doc);
String viewData = (String) ctrl.getViewData();
String[] viewParts = viewData.split(";");
if (viewParts.length < 4) {
System.out.println("No sheet states found in view data");
return;
}
StringBuilder vdNew = new StringBuilder();
for (int i=0; i < 3; i++)
vdNew.append(viewParts[i]).append(";"); // copy over unchanged
for(int i=0; i < states.length; i++) {
vdNew.append( states[i].toString()); // update states
if (i != states.length-1)
vdNew.append(";");
}
// System.out.println("New view data: \"" + vdNew + "\"");
ctrl.restoreViewData(vdNew.toString());
} // end of setViewStates()
// ----------- insert/remove rows, columns, cells ---------------
public static void insertRow(XSpreadsheet sheet, int idx)
{
XColumnRowRange crRange = Lo.qi(XColumnRowRange.class, sheet);
XTableRows rows = crRange.getRows();
rows.insertByIndex(idx, 1); // add 1 row at idx position
}
public static void deleteRow(XSpreadsheet sheet, int idx)
{
XColumnRowRange crRange = Lo.qi(XColumnRowRange.class, sheet);
XTableRows rows = crRange.getRows();
rows.removeByIndex(idx, 1); // remove 1 row at idx position
}
public static void insertColumn(XSpreadsheet sheet, int idx)
{
XColumnRowRange crRange = Lo.qi(XColumnRowRange.class, sheet);
XTableColumns cols = crRange.getColumns();
cols.insertByIndex(idx, 1); // add 1 column at idx position
}
public static void deleteColumn(XSpreadsheet sheet, int idx)
{
XColumnRowRange crRange = Lo.qi(XColumnRowRange.class, sheet);
XTableColumns cols = crRange.getColumns();
cols.removeByIndex(idx, 1); // remove 1 row at idx position
}
public static void insertCells(XSpreadsheet sheet,
XCellRange cellRange, boolean isShiftRight)
{
XCellRangeMovement mover = Lo.qi(XCellRangeMovement.class, sheet);
CellRangeAddress addr = getAddress(cellRange);
if (isShiftRight)
mover.insertCells(addr, CellInsertMode.RIGHT);
else // move old cells down
mover.insertCells(addr, CellInsertMode.DOWN);
} // end of insertCells()
public static void deleteCells(XSpreadsheet sheet,
XCellRange cellRange, boolean isShiftLeft)
{
XCellRangeMovement mover = Lo.qi(XCellRangeMovement.class, sheet);
CellRangeAddress addr = getAddress(cellRange);
if (isShiftLeft)
mover.removeRange(addr, CellDeleteMode.LEFT);
else // move old cells up
mover.removeRange(addr, CellDeleteMode.UP);
} // end of deleteCells()
// ----------- set/get values in cells ------------------
public static void setVal(XSpreadsheet sheet, String cellName, Object value)
{ Point pos = getCellPosition(cellName);
setVal(sheet, pos.x, pos.y, value); // column, row
}
public static void setVal(XSpreadsheet sheet, int column, int row, Object value)
{ XCell cell = getCell(sheet, column, row);
setVal(cell, value);
}
public static void setVal(XCell cell, Object value)
{
if (value instanceof Number)
cell.setValue( convertToDouble(value));
else if (value instanceof String)
cell.setFormula((String)value);
else
System.out.println("Value is not a number or string: " + value);
} // end of setVal()
public static double convertToDouble(Object val)
{
if (val == null) {
System.out.println("Value is null; using 0");
return 0;
}
try {
if (val instanceof Integer)
return (double)((Integer)val).intValue();
else
return (Double) val;
}
catch(ClassCastException e)
{ System.out.println("Could not convert " + val + " to double; using 0");
return 0;
}
} // end of convertToDouble()
public static String getTypeString(XCell cell)
{
CellContentType type = cell.getType();
if (type == CellContentType.EMPTY)
return "EMPTY";
else if (type == CellContentType.VALUE)
return "VALUE";
else if (type == CellContentType.TEXT)
return "TEXT";
else if (type == CellContentType.FORMULA)
return "FORMULA";
else {
System.out.println("Unknown cell type");
return "??";
}
} // end of getTypeString()
public static Object getVal(XSpreadsheet sheet, CellAddress addr)
{
if (addr == null)
return null;
return getVal(sheet, addr.Column, addr.Row);
} // end of getVal()
public static Object getVal(XSpreadsheet sheet, String cellName)
{ Point pos = getCellPosition(cellName);
return getVal(sheet, pos.x, pos.y); // column, row
} // end of getVal()
public static Object getVal(XSpreadsheet sheet, int column, int row)
{ XCell xCell = getCell(sheet, column, row);
return getVal(xCell, column, row);
} // end of getVal()
public static Object getVal(XCell cell, int column, int row)
{
CellContentType type = cell.getType();
if (type == CellContentType.EMPTY)
return null;
else if (type == CellContentType.VALUE)
return new Double( cell.getValue());
else if ((type == CellContentType.TEXT) || (type == CellContentType.FORMULA))
return cell.getFormula();
else {
System.out.println("Unknown cell type; returning null");
return null;
}
} // end of getVal()
public static double getNum(XSpreadsheet sheet, String cellName)
{ return convertToDouble(getVal(sheet, cellName)); }
public static double getNum(XSpreadsheet sheet, CellAddress addr)
{ return convertToDouble(getVal(sheet, addr)); }
public static double getNum(XSpreadsheet sheet, int column, int row)
{ return convertToDouble(getVal(sheet, column, row)); }
public static String getString(XSpreadsheet sheet, String cellName)
{ return (String) getVal(sheet, cellName); }
public static String getString(XSpreadsheet sheet, CellAddress addr)
{ return (String) getVal(sheet, addr); }
public static String getString(XSpreadsheet sheet, int column, int row)
{ return (String) getVal(sheet, column, row); }
// ----------- set/get values in 2D array ------------------
public static void setArray(XSpreadsheet sheet, String name, Object[][] values)
{
if (isCellRangeName(name))
setArrayRange(sheet, name, values);
else // is a cell name
setArrayCell(sheet, name, values);
} // end of setArray()
public static void setArrayRange(XSpreadsheet sheet, String rangeName, Object[][] values)
{ XCellRange cellRange = getCellRange(sheet, rangeName);
setCellRangeArray(cellRange, values);
} // end of setArrayRange()
public static void setCellRangeArray(XCellRange cellRange, Object[][] values)
{ XCellRangeData crData = Lo.qi(XCellRangeData.class, cellRange);
crData.setDataArray(values);
} // end of setCellRangeArray()
public static void setArrayCell(XSpreadsheet sheet, String cellName, Object[][] values)
{
Point pos = getCellPosition(cellName);
int colEnd = pos.x + values[0].length-1;
int rowEnd = pos.y + values.length-1;
XCellRange cellRange = getCellRange(sheet, pos.x, pos.y, colEnd, rowEnd);
setCellRangeArray(cellRange, values);
} // end of setArrayCell()
public static Object[][] getArray(XSpreadsheet sheet, String rangeName)
{
XCellRange cellRange = getCellRange(sheet, rangeName);
XCellRangeData crData = Lo.qi(XCellRangeData.class, cellRange);
return crData.getDataArray();
} // end of getArray()
public static Object[][] getCellRangeArray(XCellRange cellRange)
{ XCellRangeData crData = Lo.qi(XCellRangeData.class, cellRange);
return crData.getDataArray();
}
public static void printArray(Object[][] vals)
{
System.out.println("Row x Column size: " + vals.length + " x " + (vals[0].length));
for (int row = 0; row < vals.length; row++) {
for(int col = 0; col < vals[row].length; col++)
System.out.print(" " + vals[row][col]);
System.out.println();
}
System.out.println();
} // end of printArray()
public static double[][] getDoublesArray(XSpreadsheet sheet, String rangeName)
{ return convertToDoubles( getArray(sheet, rangeName)); }
public static double[][] convertToDoubles(Object[][] vals)
{
int rowSize = vals.length;
int colSize = vals[0].length; // assuming all columns are this length
// System.out.println("Row x Column size: " + rowSize + " x " + colSize);
double[][] doubles = new double[rowSize][colSize];
for (int row = 0; row < rowSize; row++)
for(int col = 0; col < colSize; col++)
doubles[row][col] = convertToDouble(vals[row][col]);
return doubles;
} // end of convertToDoubles()
public static void printArray(double[][] vals)
// repeated code but for printing doubles array
{
System.out.println("Row x Column size: " + vals.length + " x " + (vals[0].length));
for (int row = 0; row < vals.length; row++) {
for(int col = 0; col < vals[row].length; col++)
System.out.print(" " + vals[row][col]);
System.out.println();
}
System.out.println();
} // end of printArray()
// ---------- set/get rows and columns -------------------------
public static void setCol(XSpreadsheet sheet, String cellName, Object[] values)
{ Point pos = getCellPosition(cellName);
setCol(sheet, pos.x, pos.y, values); // column, row
}
public static void setCol(XSpreadsheet sheet,
int colStart, int rowStart, Object[] values)
// add values down a single column starting at (colstart, rowstart)
{
XCellRange cellRange = getCellRange(sheet, colStart, rowStart,
colStart, rowStart + values.length-1);
XCell xCell = null;
for (int i = 0; i < values.length; i++) {
xCell = getCell(cellRange, 0, i); // column -- row
setVal(xCell, values[i]);
}
} // end of setCol()
public static void setRow(XSpreadsheet sheet, String cellName, Object[] values)
{ Point pos = getCellPosition(cellName);
setRow(sheet, pos.x, pos.y, values); // column, row
}
public static void setRow(XSpreadsheet sheet,
int colStart, int rowStart, Object[] values)
// add values along a single row starting at (colstart, rowstart)
{
XCellRange cellRange = getCellRange(sheet, colStart, rowStart,
colStart + values.length-1, rowStart);
XCellRangeData crData = Lo.qi(XCellRangeData.class, cellRange);
crData.setDataArray( new Object[][]{values}); // 1-row 2D array
} // end of setRow()
public static Object[] getRow(XSpreadsheet sheet, String rangeName)
{
Object[][] vals = getArray(sheet, rangeName);
return extractRow(vals, 0); // assumes user wants 1st row
} // end of getRow()
public static Object[] extractRow(Object[][] vals, int rowIdx)
{
int rowSize = vals.length;
if ((rowIdx < 0) || (rowIdx > rowSize-1)) {
System.out.println("Row index out of range");
return null;
}
else
return vals[rowIdx];
} // end of extractRow()
public static Object[] getCol(XSpreadsheet sheet, String rangeName)
{
Object[][] vals = getArray(sheet, rangeName);
return extractCol(vals, 0); // assumes user wants 1st column
} // end of getCol()
public static Object[] extractCol(Object[][] vals, int colIdx)
{
int rowSize = vals.length;
int colSize = vals[0].length; // assuming all columns are this length
if ((colIdx < 0) || (colIdx > colSize-1)) {
System.out.println("Column index out of range");
return null;
}
else {
Object[] colVals = new Object[rowSize];
for (int row = 0; row < rowSize; row++)
colVals[row] = vals[row][colIdx];
return colVals;
}
} // end of extractCol()
public static double[] convertToDoubles(Object[] vals)
{
int size = vals.length;
double[] doubles = new double[size];
for(int i = 0; i < size; i++)
doubles[i] = convertToDouble(vals[i]);
return doubles;
} // end of convertToDoubles()
// ----------------- special cell types ---------------------
public static void setDate(XSpreadsheet sheet, String cellName,
int day, int month, int year)
// Writes a date with standard date format into a spreadsheet
{
XCell xCell = getCell(sheet, cellName);
xCell.setFormula(month + "/" + day + "/" + year);
XNumberFormatsSupplier nfsSupplier =
Lo.createInstanceMCF(XNumberFormatsSupplier.class,
"com.sun.star.util.NumberFormatsSupplier");
XNumberFormats numberFormats = nfsSupplier.getNumberFormats();
XNumberFormatTypes xFormatTypes =
Lo.qi(XNumberFormatTypes.class, numberFormats);
com.sun.star.lang.Locale aLocale = new com.sun.star.lang.Locale();
//aLocale.Country = "GB";
//aLocale.Language = "en";
int nFormat = xFormatTypes.getStandardFormat(NumberFormat.DATE, aLocale);
// NumberFormat.DATETIME
Props.setProperty(xCell, "NumberFormat", nFormat);
} // end of setDate()
public static void addAnnotation(XSpreadsheet sheet, String cellName, String msg)
{
// add the annotation
CellAddress addr = getCellAddress(sheet, cellName);
XSheetAnnotationsSupplier annsSupp =
Lo.qi(XSheetAnnotationsSupplier.class, sheet);
XSheetAnnotations anns = annsSupp.getAnnotations();
anns.insertNew(addr, msg);
// get a reference to the annotation
XCell xCell = getCell(sheet, cellName);
XSheetAnnotationAnchor annAnchor = Lo.qi(XSheetAnnotationAnchor.class, xCell);
XSheetAnnotation ann = annAnchor.getAnnotation();
ann.setIsVisible(true);
} // end of addAnnotation()
// ----------------- get XCell and XCellRange methods ---------------------------
public static XCell getCell(XSpreadsheet sheet, int column, int row)
{ try {
return sheet.getCellByPosition(column, row);
}
catch (Exception e) {
System.out.println("Could not access cell at: " + column + " - " + row);
return null;
}
} // end of getCell()
public static XCell getCell(XSpreadsheet sheet, CellAddress addr)
{ return getCell(sheet, addr.Column, addr.Row); } // not using Sheet value in addr
public static XCell getCell(XCellRange cellRange, int column, int row)
{ try {
return cellRange.getCellByPosition(column, row);
}
catch (Exception e) {
System.out.println("Could not access cell in cellrange at: " + column + " - " + row);
return null;
}
} // end of getCell()
public static XCell getCell(XSpreadsheet sheet, String cellName)
{ XCellRange cellRange = sheet.getCellRangeByName(cellName);
return getCell(cellRange, 0, 0);
}
public static boolean isCellRangeName(String s)
{ return s.contains(":"); }
public static XCellRange getCellRange(XSpreadsheet sheet, CellRangeAddress addr)
{ return getCellRange(sheet, addr.StartColumn, addr.StartRow,
addr.EndColumn, addr.EndRow); }
// not using Sheet value in addr
public static boolean isSingleCellRange(CellRangeAddress addr)
{ return ((addr.StartColumn == addr.EndColumn) &&
(addr.StartRow == addr.EndRow)); }
public static XCellRange getCellRange(XSpreadsheet sheet,
int colStart, int rowStart, int colEnd, int rowEnd)
{ try {
return sheet.getCellRangeByPosition(colStart, rowStart, colEnd, rowEnd);
}
catch (Exception e) {
System.out.println("Could not access cell range : (" +
colStart + ", " + rowStart + ") to (" +
colEnd + ", " + rowEnd + ")" );
return null;
}
} // end of getCellRange()