Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / odk / examples / CLI / CSharp / Spreadsheet / SpreadsheetDocHelper.cs
blobd8ddd6beb9cf71338a7f6bbb2f8d699d6b7b5fa9
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 using System;
20 using unoidl.com.sun.star.lang;
21 using unoidl.com.sun.star.uno;
22 using unoidl.com.sun.star.bridge;
23 using unoidl.com.sun.star.frame;
25 // __________ implementation ____________________________________
27 /** This is a helper class for the spreadsheet and table samples.
28 It connects to a running office and creates a spreadsheet document.
29 Additionally it contains various helper functions.
31 public class SpreadsheetDocHelper : System.IDisposable
34 // __ private members ___________________________________________
36 private const String msDataSheetName = "Data";
38 private unoidl.com.sun.star.uno.XComponentContext m_xContext;
39 private unoidl.com.sun.star.lang.XMultiServiceFactory mxMSFactory;
40 private unoidl.com.sun.star.sheet.XSpreadsheetDocument mxDocument;
44 public SpreadsheetDocHelper( String[] args )
46 // Connect to a running office and get the service manager
47 mxMSFactory = connect( args );
48 // Create a new spreadsheet document
49 mxDocument = initDocument();
52 // __ helper methods ____________________________________________
54 /** Returns the service manager.
55 @return XMultiServiceFactory interface of the service manager. */
56 public unoidl.com.sun.star.lang.XMultiServiceFactory getServiceManager()
58 return mxMSFactory;
61 /** Returns the whole spreadsheet document.
62 @return XSpreadsheetDocument interface of the document. */
63 public unoidl.com.sun.star.sheet.XSpreadsheetDocument getDocument()
65 return mxDocument;
68 /** Returns the spreadsheet with the specified index (0-based).
69 @param nIndex The index of the sheet.
70 @return XSpreadsheet interface of the sheet. */
71 public unoidl.com.sun.star.sheet.XSpreadsheet getSpreadsheet( int nIndex )
73 // Collection of sheets
74 unoidl.com.sun.star.sheet.XSpreadsheets xSheets =
75 mxDocument.getSheets();
77 unoidl.com.sun.star.container.XIndexAccess xSheetsIA =
78 (unoidl.com.sun.star.container.XIndexAccess) xSheets;
80 unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
81 (unoidl.com.sun.star.sheet.XSpreadsheet)
82 xSheetsIA.getByIndex( nIndex ).Value;
84 return xSheet;
87 /** Inserts a new empty spreadsheet with the specified name.
88 @param aName The name of the new sheet.
89 @param nIndex The insertion index.
90 @return The XSpreadsheet interface of the new sheet. */
91 public unoidl.com.sun.star.sheet.XSpreadsheet insertSpreadsheet(
92 String aName, short nIndex )
94 // Collection of sheets
95 unoidl.com.sun.star.sheet.XSpreadsheets xSheets =
96 mxDocument.getSheets();
98 xSheets.insertNewByName( aName, nIndex );
99 unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
100 (unoidl.com.sun.star.sheet.XSpreadsheet)
101 xSheets.getByName( aName ).Value;
103 return xSheet;
107 // Methods to fill values into cells.
109 /** Writes a double value into a spreadsheet.
110 @param xSheet The XSpreadsheet interface of the spreadsheet.
111 @param aCellName The address of the cell (or a named range).
112 @param fValue The value to write into the cell. */
113 public void setValue(
114 unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
115 String aCellName,
116 double fValue )
118 xSheet.getCellRangeByName( aCellName ).getCellByPosition(
119 0, 0 ).setValue( fValue );
122 /** Writes a formula into a spreadsheet.
123 @param xSheet The XSpreadsheet interface of the spreadsheet.
124 @param aCellName The address of the cell (or a named range).
125 @param aFormula The formula to write into the cell. */
126 public void setFormula(
127 unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
128 String aCellName,
129 String aFormula )
131 xSheet.getCellRangeByName( aCellName ).getCellByPosition(
132 0, 0 ).setFormula( aFormula );
135 /** Writes a date with standard date format into a spreadsheet.
136 @param xSheet The XSpreadsheet interface of the spreadsheet.
137 @param aCellName The address of the cell (or a named range).
138 @param nDay The day of the date.
139 @param nMonth The month of the date.
140 @param nYear The year of the date. */
141 public void setDate(
142 unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
143 String aCellName,
144 int nDay, int nMonth, int nYear )
146 // Set the date value.
147 unoidl.com.sun.star.table.XCell xCell =
148 xSheet.getCellRangeByName( aCellName ).getCellByPosition( 0, 0 );
149 String aDateStr = nMonth + "/" + nDay + "/" + nYear;
150 xCell.setFormula( aDateStr );
152 // Set standard date format.
153 unoidl.com.sun.star.util.XNumberFormatsSupplier xFormatsSupplier =
154 (unoidl.com.sun.star.util.XNumberFormatsSupplier) getDocument();
155 unoidl.com.sun.star.util.XNumberFormatTypes xFormatTypes =
156 (unoidl.com.sun.star.util.XNumberFormatTypes)
157 xFormatsSupplier.getNumberFormats();
158 int nFormat = xFormatTypes.getStandardFormat(
159 unoidl.com.sun.star.util.NumberFormat.DATE,
160 new unoidl.com.sun.star.lang.Locale() );
162 unoidl.com.sun.star.beans.XPropertySet xPropSet =
163 (unoidl.com.sun.star.beans.XPropertySet) xCell;
164 xPropSet.setPropertyValue(
165 "NumberFormat",
166 new uno.Any( (Int32) nFormat ) );
169 /** Draws a colored border around the range and writes the headline
170 in the first cell.
172 @param xSheet The XSpreadsheet interface of the spreadsheet.
173 @param aRange The address of the cell range (or a named range).
174 @param aHeadline The headline text. */
175 public void prepareRange(
176 unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
177 String aRange, String aHeadline )
179 unoidl.com.sun.star.beans.XPropertySet xPropSet = null;
180 unoidl.com.sun.star.table.XCellRange xCellRange = null;
182 // draw border
183 xCellRange = xSheet.getCellRangeByName( aRange );
184 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
185 unoidl.com.sun.star.table.BorderLine aLine =
186 new unoidl.com.sun.star.table.BorderLine();
187 aLine.Color = 0x99CCFF;
188 aLine.InnerLineWidth = aLine.LineDistance = 0;
189 aLine.OuterLineWidth = 100;
190 unoidl.com.sun.star.table.TableBorder aBorder =
191 new unoidl.com.sun.star.table.TableBorder();
192 aBorder.TopLine = aBorder.BottomLine = aBorder.LeftLine =
193 aBorder.RightLine = aLine;
194 aBorder.IsTopLineValid = aBorder.IsBottomLineValid = true;
195 aBorder.IsLeftLineValid = aBorder.IsRightLineValid = true;
196 xPropSet.setPropertyValue(
197 "TableBorder",
198 new uno.Any(
199 typeof (unoidl.com.sun.star.table.TableBorder), aBorder ) );
201 // draw headline
202 unoidl.com.sun.star.sheet.XCellRangeAddressable xAddr =
203 (unoidl.com.sun.star.sheet.XCellRangeAddressable) xCellRange;
204 unoidl.com.sun.star.table.CellRangeAddress aAddr =
205 xAddr.getRangeAddress();
207 xCellRange = xSheet.getCellRangeByPosition(
208 aAddr.StartColumn,
209 aAddr.StartRow, aAddr.EndColumn, aAddr.StartRow );
211 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
212 xPropSet.setPropertyValue(
213 "CellBackColor", new uno.Any( (Int32) 0x99CCFF ) );
214 // write headline
215 unoidl.com.sun.star.table.XCell xCell =
216 xCellRange.getCellByPosition( 0, 0 );
217 xCell.setFormula( aHeadline );
218 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCell;
219 xPropSet.setPropertyValue(
220 "CharColor", new uno.Any( (Int32) 0x003399 ) );
221 xPropSet.setPropertyValue(
222 "CharWeight",
223 new uno.Any( (Single) unoidl.com.sun.star.awt.FontWeight.BOLD ) );
227 // Methods to create cell addresses and range addresses.
229 /** Creates a unoidl.com.sun.star.table.CellAddress and initializes it
230 with the given range.
231 @param xSheet The XSpreadsheet interface of the spreadsheet.
232 @param aCell The address of the cell (or a named cell). */
233 public unoidl.com.sun.star.table.CellAddress createCellAddress(
234 unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
235 String aCell )
237 unoidl.com.sun.star.sheet.XCellAddressable xAddr =
238 (unoidl.com.sun.star.sheet.XCellAddressable)
239 xSheet.getCellRangeByName( aCell ).getCellByPosition( 0, 0 );
240 return xAddr.getCellAddress();
243 /** Creates a unoidl.com.sun.star.table.CellRangeAddress and initializes
244 it with the given range.
245 @param xSheet The XSpreadsheet interface of the spreadsheet.
246 @param aRange The address of the cell range (or a named range). */
247 public unoidl.com.sun.star.table.CellRangeAddress createCellRangeAddress(
248 unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aRange )
250 unoidl.com.sun.star.sheet.XCellRangeAddressable xAddr =
251 (unoidl.com.sun.star.sheet.XCellRangeAddressable)
252 xSheet.getCellRangeByName( aRange );
253 return xAddr.getRangeAddress();
257 // Methods to convert cell addresses and range addresses to strings.
259 /** Returns the text address of the cell.
260 @param nColumn The column index.
261 @param nRow The row index.
262 @return A string containing the cell address. */
263 public String getCellAddressString( int nColumn, int nRow )
265 String aStr = "";
266 if (nColumn > 25)
267 aStr += (char) ('A' + nColumn / 26 - 1);
268 aStr += (char) ('A' + nColumn % 26);
269 aStr += (nRow + 1);
270 return aStr;
273 /** Returns the text address of the cell range.
274 @param aCellRange The cell range address.
275 @return A string containing the cell range address. */
276 public String getCellRangeAddressString(
277 unoidl.com.sun.star.table.CellRangeAddress aCellRange )
279 return
280 getCellAddressString( aCellRange.StartColumn, aCellRange.StartRow )
281 + ":"
282 + getCellAddressString( aCellRange.EndColumn, aCellRange.EndRow );
285 /** Returns the text address of the cell range.
286 @param xCellRange The XSheetCellRange interface of the cell range.
287 @param bWithSheet true = Include sheet name.
288 @return A string containing the cell range address. */
289 public String getCellRangeAddressString(
290 unoidl.com.sun.star.sheet.XSheetCellRange xCellRange, bool bWithSheet )
292 String aStr = "";
293 if (bWithSheet)
295 unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
296 xCellRange.getSpreadsheet();
297 unoidl.com.sun.star.container.XNamed xNamed =
298 (unoidl.com.sun.star.container.XNamed) xSheet;
299 aStr += xNamed.getName() + ".";
301 unoidl.com.sun.star.sheet.XCellRangeAddressable xAddr =
302 (unoidl.com.sun.star.sheet.XCellRangeAddressable) xCellRange;
303 aStr += getCellRangeAddressString( xAddr.getRangeAddress() );
304 return aStr;
307 /** Returns a list of addresses of all cell ranges contained in the
308 collection.
310 @param xRangesIA The XIndexAccess interface of the collection.
311 @return A string containing the cell range address list. */
312 public String getCellRangeListString(
313 unoidl.com.sun.star.container.XIndexAccess xRangesIA )
315 String aStr = "";
316 int nCount = xRangesIA.getCount();
317 for (int nIndex = 0; nIndex < nCount; ++nIndex)
319 if (nIndex > 0)
320 aStr += " ";
321 uno.Any aRangeObj = xRangesIA.getByIndex( nIndex );
322 unoidl.com.sun.star.sheet.XSheetCellRange xCellRange =
323 (unoidl.com.sun.star.sheet.XSheetCellRange) aRangeObj.Value;
324 aStr += getCellRangeAddressString( xCellRange, false );
326 return aStr;
331 /** Connect to a running office that is accepting connections.
332 @return The ServiceManager to instantiate office components. */
333 private XMultiServiceFactory connect( String [] args )
336 m_xContext = uno.util.Bootstrap.bootstrap();
338 return (XMultiServiceFactory) m_xContext.getServiceManager();
341 public void Dispose()
346 /** Creates an empty spreadsheet document.
347 @return The XSpreadsheetDocument interface of the document. */
348 private unoidl.com.sun.star.sheet.XSpreadsheetDocument initDocument()
350 XComponentLoader aLoader = (XComponentLoader)
351 mxMSFactory.createInstance( "com.sun.star.frame.Desktop" );
353 XComponent xComponent = aLoader.loadComponentFromURL(
354 "private:factory/scalc", "_blank", 0,
355 new unoidl.com.sun.star.beans.PropertyValue[0] );
357 return (unoidl.com.sun.star.sheet.XSpreadsheetDocument) xComponent;