SCUtil.java (cebb507a) SCUtil.java (1dfd36f5)
1/**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance

--- 14 unchanged lines hidden (view full) ---

23package testlib.uno;
24
25import java.util.HashMap;
26
27import org.openoffice.test.common.FileUtil;
28import org.openoffice.test.common.Testspace;
29import org.openoffice.test.uno.UnoApp;
30
1/**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance

--- 14 unchanged lines hidden (view full) ---

23package testlib.uno;
24
25import java.util.HashMap;
26
27import org.openoffice.test.common.FileUtil;
28import org.openoffice.test.common.Testspace;
29import org.openoffice.test.uno.UnoApp;
30
31import com.sun.star.awt.Rectangle;
31import com.sun.star.beans.PropertyValue;
32import com.sun.star.beans.XPropertySet;
32import com.sun.star.beans.PropertyValue;
33import com.sun.star.beans.XPropertySet;
34import com.sun.star.chart.XChartDocument;
35import com.sun.star.chart.XDiagram;
33import com.sun.star.container.XIndexAccess;
36import com.sun.star.container.XIndexAccess;
37import com.sun.star.container.XNameAccess;
34import com.sun.star.container.XNamed;
38import com.sun.star.container.XNamed;
39import com.sun.star.document.XEmbeddedObjectSupplier;
35import com.sun.star.frame.XController;
36import com.sun.star.frame.XModel;
37import com.sun.star.frame.XStorable;
38import com.sun.star.lang.XComponent;
40import com.sun.star.frame.XController;
41import com.sun.star.frame.XModel;
42import com.sun.star.frame.XStorable;
43import com.sun.star.lang.XComponent;
44import com.sun.star.lang.XMultiServiceFactory;
45import com.sun.star.sheet.XCellRangeAddressable;
39import com.sun.star.sheet.XSpreadsheet;
40import com.sun.star.sheet.XSpreadsheetDocument;
41import com.sun.star.sheet.XSpreadsheetView;
42import com.sun.star.sheet.XSpreadsheets;
46import com.sun.star.sheet.XSpreadsheet;
47import com.sun.star.sheet.XSpreadsheetDocument;
48import com.sun.star.sheet.XSpreadsheetView;
49import com.sun.star.sheet.XSpreadsheets;
50import com.sun.star.table.CellRangeAddress;
43import com.sun.star.table.XCell;
44import com.sun.star.table.XCellRange;
45import com.sun.star.table.XColumnRowRange;
51import com.sun.star.table.XCell;
52import com.sun.star.table.XCellRange;
53import com.sun.star.table.XColumnRowRange;
54import com.sun.star.table.XTableChart;
55import com.sun.star.table.XTableCharts;
56import com.sun.star.table.XTableChartsSupplier;
46import com.sun.star.table.XTableColumns;
47import com.sun.star.table.XTableRows;
48import com.sun.star.text.XText;
49import com.sun.star.uno.UnoRuntime;
50import com.sun.star.util.XCloseable;
51
52
53/**

--- 229 unchanged lines hidden (view full) ---

283 * @param xSpreadsheet
284 * @param start_col
285 * @param start_row
286 * @param end_col
287 * @param end_row
288 * @param values
289 * @throws Exception
290 */
57import com.sun.star.table.XTableColumns;
58import com.sun.star.table.XTableRows;
59import com.sun.star.text.XText;
60import com.sun.star.uno.UnoRuntime;
61import com.sun.star.util.XCloseable;
62
63
64/**

--- 229 unchanged lines hidden (view full) ---

294 * @param xSpreadsheet
295 * @param start_col
296 * @param start_row
297 * @param end_col
298 * @param end_row
299 * @param values
300 * @throws Exception
301 */
302 @Deprecated
291 public static void setValueToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row, double[][] values) throws Exception {
292 XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row);
293 XCell xCell = null;
294 for (int i = 0; i <= (end_row - start_row); i++ ) {
295 for(int j = 0; j <= (end_col - start_col); j++) {
296 xCell = xCellRange.getCellByPosition(j, i);
297 xCell.setValue(values[i][j]);
298 }
299 }
300 }
301
303 public static void setValueToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row, double[][] values) throws Exception {
304 XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row);
305 XCell xCell = null;
306 for (int i = 0; i <= (end_row - start_row); i++ ) {
307 for(int j = 0; j <= (end_col - start_col); j++) {
308 xCell = xCellRange.getCellByPosition(j, i);
309 xCell.setValue(values[i][j]);
310 }
311 }
312 }
313
314 public static void setValueToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, double[][] values) throws Exception {
315 XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, start_col + values[0].length - 1, start_row + values.length - 1);
316 XCell xCell = null;
317 for (int i = 0; i < values.length; i++ ) {
318 for(int j = 0; j < values[0].length; j++) {
319 xCell = xCellRange.getCellByPosition(j, i);
320 xCell.setValue(values[i][j]);
321 }
322 }
323 }
324
302 /**
303 * Set text into a cell range
304 * @param xSpreadsheet
305 * @param start_col
306 * @param start_row
307 * @param end_col
308 * @param end_row
309 * @param texts
310 * @throws Exception
311 */
325 /**
326 * Set text into a cell range
327 * @param xSpreadsheet
328 * @param start_col
329 * @param start_row
330 * @param end_col
331 * @param end_row
332 * @param texts
333 * @throws Exception
334 */
335 @Deprecated
312 public static void setTextToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row, String[][] texts) throws Exception {
313 XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row);
314 XCell xCell = null;
315 XText xText = null;
316 for (int i = 0; i <= (end_row - start_row); i++ ) {
317 for(int j = 0; j <= (end_col - start_col); j++) {
318 xCell = xCellRange.getCellByPosition(j, i);
319 xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
320 xText.setString(texts[i][j]);
321 }
322 }
323 }
324
336 public static void setTextToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, int end_col, int end_row, String[][] texts) throws Exception {
337 XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, end_col, end_row);
338 XCell xCell = null;
339 XText xText = null;
340 for (int i = 0; i <= (end_row - start_row); i++ ) {
341 for(int j = 0; j <= (end_col - start_col); j++) {
342 xCell = xCellRange.getCellByPosition(j, i);
343 xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
344 xText.setString(texts[i][j]);
345 }
346 }
347 }
348
349 public static void setTextToCellRange(XSpreadsheet xSpreadsheet, int start_col, int start_row, String[][] texts) throws Exception {
350 XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(start_col, start_row, start_col + texts[0].length - 1, start_row + texts.length - 1);
351 XCell xCell = null;
352 XText xText = null;
353 for (int i = 0; i < texts.length; i++ ) {
354 for(int j = 0; j < texts[0].length; j++) {
355 xCell = xCellRange.getCellByPosition(j, i);
356 xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
357 xText.setString(texts[i][j]);
358 }
359 }
360 }
361
325 /**
326 * Get number content from a cell range
327 * @param xSpreadsheet
328 * @param start_col
329 * @param start_row
330 * @param end_col
331 * @param end_row
332 * @return

--- 85 unchanged lines hidden (view full) ---

418 xSpreadsheetModel.getCurrentController());
419 XSpreadsheet activesheet = xSpeadsheetView.getActiveSheet();
420 XNamed activesheetName = (XNamed) UnoRuntime.queryInterface(
421 XNamed.class, activesheet);
422 return activesheetName.getName();
423 }
424
425 /**
362 /**
363 * Get number content from a cell range
364 * @param xSpreadsheet
365 * @param start_col
366 * @param start_row
367 * @param end_col
368 * @param end_row
369 * @return

--- 85 unchanged lines hidden (view full) ---

455 xSpreadsheetModel.getCurrentController());
456 XSpreadsheet activesheet = xSpeadsheetView.getActiveSheet();
457 XNamed activesheetName = (XNamed) UnoRuntime.queryInterface(
458 XNamed.class, activesheet);
459 return activesheetName.getName();
460 }
461
462 /**
463 * Set specific property's value for an object
464 * @param obj
465 * @param propName
466 * @param value
467 * @throws Exception
468 */
469 public static void setProperties(Object obj, String propName, Object value) throws Exception {
470 XPropertySet xPropertySet =
471 (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, obj);
472 xPropertySet.setPropertyValue(propName, value);
473 }
474
475 /**
476 * Get specific property's value of an object
477 * @param obj
478 * @param propName
479 * @return
480 * @throws Exception
481 */
482 public static Object getProperties(Object obj, String propName) throws Exception {
483 XPropertySet xPropertySet =
484 (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, obj);
485 Object value = xPropertySet.getPropertyValue(propName);
486
487 return value;
488 }
489
490 /**
426 * Set value of specific property from a cell
427 * @param xCell
428 * @param propName
429 * @param value
430 * @throws Exception
431 */
432 public static void setCellProperties(XCell xCell, String propName, Object value) throws Exception {
433
491 * Set value of specific property from a cell
492 * @param xCell
493 * @param propName
494 * @param value
495 * @throws Exception
496 */
497 public static void setCellProperties(XCell xCell, String propName, Object value) throws Exception {
498
434 XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xCell);
435 xPropertySet.setPropertyValue(propName, value);
499 setProperties(xCell, propName, value);
436 }
437
438 /**
439 * Get value of specific property from a cell
440 * @param xCell
441 * @param propName
442 * @return
443 * @throws Exception
444 */
445 public static Object getCellProperties(XCell xCell, String propName) throws Exception {
500 }
501
502 /**
503 * Get value of specific property from a cell
504 * @param xCell
505 * @param propName
506 * @return
507 * @throws Exception
508 */
509 public static Object getCellProperties(XCell xCell, String propName) throws Exception {
446 XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xCell);
447 Object value = xPropertySet.getPropertyValue(propName);
448
449 return value;
510 return getProperties(xCell, propName);
450 }
451
452 /**
453 * Clear temp file directory
454 */
455 public static void clearTempDir() {
456 FileUtil.deleteFile(Testspace.getFile(Testspace.getPath(scTempDir)));
457 }

--- 20 unchanged lines hidden (view full) ---

478 storeProps[1].Value = new Boolean(true);
479
480 XStorable scStorable =
481 (XStorable) UnoRuntime.queryInterface(XStorable.class, scComponent);
482 scStorable.storeAsURL(storeUrl, storeProps);
483 }
484
485 /**
511 }
512
513 /**
514 * Clear temp file directory
515 */
516 public static void clearTempDir() {
517 FileUtil.deleteFile(Testspace.getFile(Testspace.getPath(scTempDir)));
518 }

--- 20 unchanged lines hidden (view full) ---

539 storeProps[1].Value = new Boolean(true);
540
541 XStorable scStorable =
542 (XStorable) UnoRuntime.queryInterface(XStorable.class, scComponent);
543 scStorable.storeAsURL(storeUrl, storeProps);
544 }
545
546 /**
486 * Save file after open file.
487 *
547 * Save file after open file.
488 * @param xSpreadsheetDocument
489 * @throws Exception
490 */
491 public static void save(XSpreadsheetDocument xSpreadsheetDocument)
492 throws Exception {
548 * @param xSpreadsheetDocument
549 * @throws Exception
550 */
551 public static void save(XSpreadsheetDocument xSpreadsheetDocument)
552 throws Exception {
493
494 XStorable scStorable = (XStorable) UnoRuntime.queryInterface(
495 XStorable.class, xSpreadsheetDocument);
496 scStorable.store();
553 XStorable scStorable = (XStorable) UnoRuntime.queryInterface(
554 XStorable.class, xSpreadsheetDocument);
555 scStorable.store();
497
498 }
499
500
501 /**
502 * Close specific opening spreadsheet file which has been saved
503 * @param xSpreadsheetDocument
504 * @throws Exception
505 */

--- 16 unchanged lines hidden (view full) ---

522 String filePath = Testspace.getPath(scTempDir + fullFileName);
523 XSpreadsheetDocument xScDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, unoApp.loadDocument(filePath));
524
525 return xScDocument;
526 }
527
528 /**
529 * open file in Spreadsheet.
556 }
557
558
559 /**
560 * Close specific opening spreadsheet file which has been saved
561 * @param xSpreadsheetDocument
562 * @throws Exception
563 */

--- 16 unchanged lines hidden (view full) ---

580 String filePath = Testspace.getPath(scTempDir + fullFileName);
581 XSpreadsheetDocument xScDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, unoApp.loadDocument(filePath));
582
583 return xScDocument;
584 }
585
586 /**
587 * open file in Spreadsheet.
530 * @param unoApp
531 * @param filtpath File path with the extension name. (e.g. "testcase/uno/sc/data/sample.xls")
588 * @param app
589 * @param filePath File path with the extension name. (e.g. "testcase/uno/sc/data/sample.xls")
532 * @return
533 * @throws Exception
534 */
535 public static XSpreadsheetDocument openFile(String filePath, UnoApp app) throws Exception {
536 return (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, app.loadDocument(filePath));
537 }
538
539 /**

--- 6 unchanged lines hidden (view full) ---

546 }
547
548 filterName.put("ods", "calc8");
549 filterName.put("ots", "calc8_template");
550 filterName.put("xls", "MS Excel 97");
551 filterName.put("xlt", "MS Excel 97 Vorlage/Template");
552 filterName.put("csv", "Text - txt - csv (StarCalc)");
553 }
590 * @return
591 * @throws Exception
592 */
593 public static XSpreadsheetDocument openFile(String filePath, UnoApp app) throws Exception {
594 return (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, app.loadDocument(filePath));
595 }
596
597 /**

--- 6 unchanged lines hidden (view full) ---

604 }
605
606 filterName.put("ods", "calc8");
607 filterName.put("ots", "calc8_template");
608 filterName.put("xls", "MS Excel 97");
609 filterName.put("xlt", "MS Excel 97 Vorlage/Template");
610 filterName.put("csv", "Text - txt - csv (StarCalc)");
611 }
612
613
614 /***************************************************************
615 * Chart Utility method - using chart interface *
616 ****************************************************************/
554
617
618 /**
619 * Get a CellRangeAddress by cell range reference name
620 * @param xSpreadsheet
621 * @param rangeName a cell range reference name(e.g. "A1:B2")
622 * @return
623 */
624 public static CellRangeAddress getChartDataRangeByName(XSpreadsheet xSpreadsheet, String rangeName) {
625 XCellRange cellRange = xSpreadsheet.getCellRangeByName(rangeName);
626 XCellRangeAddressable xCellRangeAddressable =
627 (XCellRangeAddressable) UnoRuntime.queryInterface(XCellRangeAddressable.class, cellRange);
628
629 CellRangeAddress cellRangeAddress = xCellRangeAddressable.getRangeAddress();
630 return cellRangeAddress;
631 }
632
633 /**
634 * Create a spreadsheet chart with data in a specific cell range.
635 * @param xSpreadsheet
636 * @param rec a rectangle shape object
637 * @param dataRangeAddress the CellRangeAddress array of chart data source
638 * @param chartName
639 * @return
640 * @throws Exception
641 */
642 public static XChartDocument createChart(XSpreadsheet xSpreadsheet, Rectangle rec, CellRangeAddress[] dataRangeAddress, String chartName) throws Exception {
643 XChartDocument xChartDocument = null;
644 XTableChartsSupplier xTChartSupplier =
645 (XTableChartsSupplier) UnoRuntime.queryInterface(XTableChartsSupplier.class, xSpreadsheet);
646 XTableCharts xTableCharts = xTChartSupplier.getCharts();
647 XNameAccess xNameAccess =
648 (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, xTableCharts);
649 if (xNameAccess != null && !xNameAccess.hasByName(chartName)) {
650
651 xTableCharts.addNewByName(chartName, rec, dataRangeAddress, true, false);
652 XTableChart xTableChart = (XTableChart) UnoRuntime.queryInterface(
653 XTableChart.class, xNameAccess.getByName(chartName));
654 XEmbeddedObjectSupplier xEmbeddedObjectSupplier = (XEmbeddedObjectSupplier) UnoRuntime.queryInterface(
655 XEmbeddedObjectSupplier.class, xTableChart);
656 xChartDocument = (XChartDocument) UnoRuntime.queryInterface(
657 XChartDocument.class, xEmbeddedObjectSupplier.getEmbeddedObject());
658 }
659
660 return xChartDocument;
661 }
662
663 /**
664 * Get XChartDocument object via the chart name.
665 * @param xSpreadsheet
666 * @param chartName
667 * @return
668 * @throws Exception
669 */
670 public static XChartDocument getChartByName(XSpreadsheet xSpreadsheet, String chartName) throws Exception {
671 XChartDocument xChartDocument = null;
672 XTableChartsSupplier xTChartSupplier =
673 (XTableChartsSupplier) UnoRuntime.queryInterface(XTableChartsSupplier.class, xSpreadsheet);
674 XTableCharts xTableCharts = xTChartSupplier.getCharts();
675 XNameAccess xNameAccess =
676 (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, xTableCharts);
677
678 if (xNameAccess != null && xNameAccess.hasByName(chartName)) {
679 XTableChart xTableChart = (XTableChart) UnoRuntime.queryInterface(
680 XTableChart.class, xNameAccess.getByName(chartName));
681 XEmbeddedObjectSupplier xEmbeddedObjectSupplier = (XEmbeddedObjectSupplier) UnoRuntime.queryInterface(
682 XEmbeddedObjectSupplier.class, xTableChart);
683 xChartDocument = (XChartDocument) UnoRuntime.queryInterface(
684 XChartDocument.class, xEmbeddedObjectSupplier.getEmbeddedObject());
685 }
686
687 return xChartDocument;
688 }
689
690 /**
691 * Set specific basic type to chart
692 * @param xChartDocument
693 * @param chartType
694 * @throws Exception
695 */
696 public static void setChartType(XChartDocument xChartDocument, String chartType) throws Exception {
697 XMultiServiceFactory xMultiServiceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(
698 XMultiServiceFactory.class, xChartDocument);
699 XDiagram xDiagram = (XDiagram) UnoRuntime.queryInterface(
700 XDiagram.class, xMultiServiceFactory.createInstance(chartType));
701 xChartDocument.setDiagram(xDiagram);
702 }
703
704 /**
705 * Get the type string of a chart
706 * @param xChartDocument
707 * @return
708 * @throws Exception
709 */
710 public static String getChartType(XChartDocument xChartDocument) throws Exception {
711 return xChartDocument.getDiagram().getDiagramType();
712 }
713
714 /**
715 * Get the names of charts in specific sheet
716 * @param xSpreadsheet
717 * @return
718 * @throws Exception
719 */
720 public static String[] getChartNameList(XSpreadsheet xSpreadsheet) throws Exception {
721 XChartDocument xChartDocument = null;
722 XTableChartsSupplier xTChartSupplier =
723 (XTableChartsSupplier) UnoRuntime.queryInterface(XTableChartsSupplier.class, xSpreadsheet);
724 XTableCharts xTableCharts = xTChartSupplier.getCharts();
725 String[] chartNames = xTableCharts.getElementNames();
726 return chartNames;
727 }
728
729
730
555}
731}