1*cdf0e10cSrcweir 2*cdf0e10cSrcweir using System; 3*cdf0e10cSrcweir 4*cdf0e10cSrcweir 5*cdf0e10cSrcweir // __________ implementation ____________________________________ 6*cdf0e10cSrcweir 7*cdf0e10cSrcweir /** Create a spreadsheet document and provide access to table contents. 8*cdf0e10cSrcweir */ 9*cdf0e10cSrcweir public class GeneralTableSample : SpreadsheetDocHelper 10*cdf0e10cSrcweir { 11*cdf0e10cSrcweir 12*cdf0e10cSrcweir public static void Main( String [] args ) 13*cdf0e10cSrcweir { 14*cdf0e10cSrcweir try 15*cdf0e10cSrcweir { 16*cdf0e10cSrcweir using ( GeneralTableSample aSample = 17*cdf0e10cSrcweir new GeneralTableSample( args ) ) 18*cdf0e10cSrcweir { 19*cdf0e10cSrcweir aSample.doSampleFunction(); 20*cdf0e10cSrcweir } 21*cdf0e10cSrcweir Console.WriteLine( "Sample done." ); 22*cdf0e10cSrcweir } 23*cdf0e10cSrcweir catch (Exception ex) 24*cdf0e10cSrcweir { 25*cdf0e10cSrcweir Console.WriteLine( "Sample caught exception! " + ex ); 26*cdf0e10cSrcweir } 27*cdf0e10cSrcweir } 28*cdf0e10cSrcweir 29*cdf0e10cSrcweir // ________________________________________________________________ 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir public GeneralTableSample( String[] args ) : base( args ) 32*cdf0e10cSrcweir { 33*cdf0e10cSrcweir } 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir // ________________________________________________________________ 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir /// This sample function modifies cells and cell ranges. 38*cdf0e10cSrcweir public void doSampleFunction() 39*cdf0e10cSrcweir { 40*cdf0e10cSrcweir // for common usage 41*cdf0e10cSrcweir unoidl.com.sun.star.sheet.XSpreadsheet xSheet = getSpreadsheet( 0 ); 42*cdf0e10cSrcweir unoidl.com.sun.star.beans.XPropertySet xPropSet = null; 43*cdf0e10cSrcweir unoidl.com.sun.star.table.XCell xCell = null; 44*cdf0e10cSrcweir unoidl.com.sun.star.table.XCellRange xCellRange = null; 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir // *** Access and modify a VALUE CELL *** 47*cdf0e10cSrcweir Console.WriteLine( "*** Sample for service table.Cell ***" ); 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir xCell = xSheet.getCellByPosition( 0, 0 ); 50*cdf0e10cSrcweir // Set cell value. 51*cdf0e10cSrcweir xCell.setValue( 1234 ); 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir // Get cell value. 54*cdf0e10cSrcweir double nDblValue = xCell.getValue() * 2; 55*cdf0e10cSrcweir xSheet.getCellByPosition( 0, 1 ).setValue( nDblValue ); 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir // *** Create a FORMULA CELL and query error type *** 58*cdf0e10cSrcweir xCell = xSheet.getCellByPosition( 0, 2 ); 59*cdf0e10cSrcweir // Set formula string. 60*cdf0e10cSrcweir xCell.setFormula( "=1/0" ); 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir // Get error type. 63*cdf0e10cSrcweir bool bValid = (xCell.getError() == 0); 64*cdf0e10cSrcweir // Get formula string. 65*cdf0e10cSrcweir String aText = "The formula " + xCell.getFormula() + " is "; 66*cdf0e10cSrcweir aText += bValid ? "valid." : "erroneous."; 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir // *** Insert a TEXT CELL using the XText interface *** 69*cdf0e10cSrcweir xCell = xSheet.getCellByPosition( 0, 3 ); 70*cdf0e10cSrcweir unoidl.com.sun.star.text.XText xCellText = 71*cdf0e10cSrcweir (unoidl.com.sun.star.text.XText) xCell; 72*cdf0e10cSrcweir unoidl.com.sun.star.text.XTextCursor xTextCursor = 73*cdf0e10cSrcweir xCellText.createTextCursor(); 74*cdf0e10cSrcweir xCellText.insertString( xTextCursor, aText, false ); 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir // *** Change cell properties *** 77*cdf0e10cSrcweir int nValue = bValid ? 0x00FF00 : 0xFF4040; 78*cdf0e10cSrcweir xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCell; 79*cdf0e10cSrcweir xPropSet.setPropertyValue( 80*cdf0e10cSrcweir "CellBackColor", new uno.Any( (Int32) nValue ) ); 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir // *** Accessing a CELL RANGE *** 84*cdf0e10cSrcweir Console.WriteLine( "*** Sample for service table.CellRange ***" ); 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir // Accessing a cell range over its position. 87*cdf0e10cSrcweir xCellRange = xSheet.getCellRangeByPosition( 2, 0, 3, 1 ); 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir // Change properties of the range. 90*cdf0e10cSrcweir xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange; 91*cdf0e10cSrcweir xPropSet.setPropertyValue( 92*cdf0e10cSrcweir "CellBackColor", new uno.Any( (Int32) 0x8080FF ) ); 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir // Accessing a cell range over its name. 95*cdf0e10cSrcweir xCellRange = xSheet.getCellRangeByName( "C4:D5" ); 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir // Change properties of the range. 98*cdf0e10cSrcweir xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange; 99*cdf0e10cSrcweir xPropSet.setPropertyValue( 100*cdf0e10cSrcweir "CellBackColor", new uno.Any( (Int32) 0xFFFF80 ) ); 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir // *** Using the CELL CURSOR to add some data below of 104*cdf0e10cSrcweir // the filled area *** 105*cdf0e10cSrcweir Console.WriteLine( "*** Sample for service table.CellCursor ***" ); 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir // Create a cursor using the XSpreadsheet method createCursorByRange() 108*cdf0e10cSrcweir xCellRange = xSheet.getCellRangeByName( "A1" ); 109*cdf0e10cSrcweir unoidl.com.sun.star.sheet.XSheetCellRange xSheetCellRange = 110*cdf0e10cSrcweir (unoidl.com.sun.star.sheet.XSheetCellRange) xCellRange; 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir unoidl.com.sun.star.sheet.XSheetCellCursor xSheetCellCursor = 113*cdf0e10cSrcweir xSheet.createCursorByRange( xSheetCellRange ); 114*cdf0e10cSrcweir unoidl.com.sun.star.table.XCellCursor xCursor = 115*cdf0e10cSrcweir (unoidl.com.sun.star.table.XCellCursor) xSheetCellCursor; 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir // Move to the last filled cell. 118*cdf0e10cSrcweir xCursor.gotoEnd(); 119*cdf0e10cSrcweir // Move one row down. 120*cdf0e10cSrcweir xCursor.gotoOffset( 0, 1 ); 121*cdf0e10cSrcweir xCursor.getCellByPosition( 0, 0 ).setFormula( 122*cdf0e10cSrcweir "Beyond of the last filled cell." ); 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir // *** Modifying COLUMNS and ROWS *** 126*cdf0e10cSrcweir Console.WriteLine( "*** Sample for services table.TableRows and " + 127*cdf0e10cSrcweir "table.TableColumns ***" ); 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir unoidl.com.sun.star.table.XColumnRowRange xCRRange = 130*cdf0e10cSrcweir (unoidl.com.sun.star.table.XColumnRowRange) xSheet; 131*cdf0e10cSrcweir unoidl.com.sun.star.table.XTableColumns xColumns = 132*cdf0e10cSrcweir xCRRange.getColumns(); 133*cdf0e10cSrcweir unoidl.com.sun.star.table.XTableRows xRows = xCRRange.getRows(); 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir // Get column C by index (interface XIndexAccess). 136*cdf0e10cSrcweir uno.Any aColumnObj = xColumns.getByIndex( 2 ); 137*cdf0e10cSrcweir xPropSet = (unoidl.com.sun.star.beans.XPropertySet) aColumnObj.Value; 138*cdf0e10cSrcweir xPropSet.setPropertyValue( "Width", new uno.Any( (Int32) 5000 ) ); 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir // Get the name of the column. 141*cdf0e10cSrcweir unoidl.com.sun.star.container.XNamed xNamed = 142*cdf0e10cSrcweir (unoidl.com.sun.star.container.XNamed) aColumnObj.Value; 143*cdf0e10cSrcweir aText = "The name of this column is " + xNamed.getName() + "."; 144*cdf0e10cSrcweir xSheet.getCellByPosition( 2, 2 ).setFormula( aText ); 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir // Get column D by name (interface XNameAccess). 147*cdf0e10cSrcweir unoidl.com.sun.star.container.XNameAccess xColumnsName = 148*cdf0e10cSrcweir (unoidl.com.sun.star.container.XNameAccess) xColumns; 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir aColumnObj = xColumnsName.getByName( "D" ); 151*cdf0e10cSrcweir xPropSet = (unoidl.com.sun.star.beans.XPropertySet) aColumnObj.Value; 152*cdf0e10cSrcweir xPropSet.setPropertyValue( 153*cdf0e10cSrcweir "IsVisible", new uno.Any( (Boolean) false ) ); 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir // Get row 7 by index (interface XIndexAccess) 156*cdf0e10cSrcweir uno.Any aRowObj = xRows.getByIndex( 6 ); 157*cdf0e10cSrcweir xPropSet = (unoidl.com.sun.star.beans.XPropertySet) aRowObj.Value; 158*cdf0e10cSrcweir xPropSet.setPropertyValue( "Height", new uno.Any( (Int32) 5000 ) ); 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir xSheet.getCellByPosition( 2, 6 ).setFormula( "What a big cell." ); 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir // Create a cell series with the values 1 ... 7. 163*cdf0e10cSrcweir for (int nRow = 8; nRow < 15; ++nRow) 164*cdf0e10cSrcweir xSheet.getCellByPosition( 0, nRow ).setValue( nRow - 7 ); 165*cdf0e10cSrcweir // Insert a row between 1 and 2 166*cdf0e10cSrcweir xRows.insertByIndex( 9, 1 ); 167*cdf0e10cSrcweir // Delete the rows with the values 3 and 4. 168*cdf0e10cSrcweir xRows.removeByIndex( 11, 2 ); 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir // *** Inserting CHARTS *** 171*cdf0e10cSrcweir Console.WriteLine( "*** Sample for service table.TableCharts ***" ); 172*cdf0e10cSrcweir 173*cdf0e10cSrcweir unoidl.com.sun.star.table.XTableChartsSupplier xChartsSupp = 174*cdf0e10cSrcweir (unoidl.com.sun.star.table.XTableChartsSupplier) xSheet; 175*cdf0e10cSrcweir unoidl.com.sun.star.table.XTableCharts xCharts = 176*cdf0e10cSrcweir xChartsSupp.getCharts(); 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir // The chart will base on the last cell series, initializing all values. 179*cdf0e10cSrcweir String aName = "newChart"; 180*cdf0e10cSrcweir unoidl.com.sun.star.awt.Rectangle aRect = 181*cdf0e10cSrcweir new unoidl.com.sun.star.awt.Rectangle(); 182*cdf0e10cSrcweir aRect.X = 10000; 183*cdf0e10cSrcweir aRect.Y = 3000; 184*cdf0e10cSrcweir aRect.Width = aRect.Height = 5000; 185*cdf0e10cSrcweir unoidl.com.sun.star.table.CellRangeAddress[] aRanges = 186*cdf0e10cSrcweir new unoidl.com.sun.star.table.CellRangeAddress[1]; 187*cdf0e10cSrcweir aRanges[0] = createCellRangeAddress( xSheet, "A9:A14" ); 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir // Create the chart. 190*cdf0e10cSrcweir xCharts.addNewByName( aName, aRect, aRanges, false, false ); 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir // Get the chart by name. 193*cdf0e10cSrcweir uno.Any aChartObj = xCharts.getByName( aName ); 194*cdf0e10cSrcweir unoidl.com.sun.star.table.XTableChart xChart = 195*cdf0e10cSrcweir (unoidl.com.sun.star.table.XTableChart) aChartObj.Value; 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir // Query the state of row and column headers. 198*cdf0e10cSrcweir aText = "Chart has column headers: "; 199*cdf0e10cSrcweir aText += xChart.getHasColumnHeaders() ? "yes" : "no"; 200*cdf0e10cSrcweir xSheet.getCellByPosition( 2, 8 ).setFormula( aText ); 201*cdf0e10cSrcweir aText = "Chart has row headers: "; 202*cdf0e10cSrcweir aText += xChart.getHasRowHeaders() ? "yes" : "no"; 203*cdf0e10cSrcweir xSheet.getCellByPosition( 2, 9 ).setFormula( aText ); 204*cdf0e10cSrcweir } 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir } 207