2 <Template originator = "Michael Hutchinson"
4 lastModified = "2008/05/30">
6 <!-- Template Header -->
7 <TemplateConfiguration>
8 <_Name>Spreadsheet Sample</_Name>
9 <_Category>C#/OpenOffice Samples</_Category>
10 <Icon>md-project</Icon>
11 <LanguageName>C#</LanguageName>
12 <_Description>Creates a new C# OpenOffice integration sample project.</_Description>
13 </TemplateConfiguration>
17 <Open filename = "Main.cs"/>
20 <!-- Template Content -->
21 <Combine name = "${ProjectName}" directory = ".">
23 <StartupProject>${ProjectName}</StartupProject>
26 <Project name = "${ProjectName}" directory = ".">
29 <Reference type="Gac" refto="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
30 <Reference type="Gac" refto="System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
31 <Reference type="Gac" refto="cli_basetypes, Version=1.0.9.0, Culture=neutral, PublicKeyToken=ce2cb7e279207b9e" />
32 <Reference type="Gac" refto="cli_uno_bridge, Version=0.0.0.0, Culture=neutral, PublicKeyToken=ce2cb7e279207b9e" />
33 <Reference type="Gac" refto="cli_ure, Version=1.0.12.0, Culture=neutral, PublicKeyToken=ce2cb7e279207b9e" />
34 <Reference type="Gac" refto="cli_types, Version=1.1.12.0, Culture=neutral, PublicKeyToken=ce2cb7e279207b9e" />
35 <Reference type="Gac" refto="cli_cppuhelper, Version=1.0.12.0, Culture=neutral, PublicKeyToken=ce2cb7e279207b9e" />
39 <FileTemplateReference TemplateID="CSharpAssemblyInfo" name="AssemblyInfo.cs" />
40 <FileTemplateReference TemplateID="OpenOfficeSpreadsheetDocHelper" name="SpreadsheetDocHelper.cs" />
41 <File name="Main.cs" AddStandardHeader="False"><![CDATA[//
42 // This library is free software; you can redistribute it and/or
43 // modify it under the terms of the GNU Lesser General Public
44 // License as published by the Free Software Foundation; either
45 // version 2.1 of the License, or (at your option) any later version.
47 // This library is distributed in the hope that it will be useful,
48 // but WITHOUT ANY WARRANTY; without even the implied warranty of
49 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
50 // Lesser General Public License for more details.
52 // You should have received a copy of the GNU Lesser General Public
53 // License along with this library. If not, see <http://www.gnu.org/licenses/>.
56 using unoidl.com.sun.star.sheet;
57 using unoidl.com.sun.star.lang;
59 namespace OpenOffice.Samples
62 ///<summary> Create and modify a spreadsheet document.</summary>
63 public class SpreadsheetSample : SpreadsheetDocHelper
66 public static void Main (string [] args)
69 using (SpreadsheetSample aSample = new SpreadsheetSample (args)) {
70 aSample.doSampleFunctions ();
72 Console.WriteLine ("\nSamples done.");
73 } catch (Exception ex) {
74 Console.WriteLine ("Sample caught exception! " + ex);
78 public SpreadsheetSample (string[] args)
83 /// <summary>This sample function performs all changes on the document.</summary>
84 public void doSampleFunctions ()
87 doCellRangeSamples ();
88 doCellRangesSamples ();
89 doCellCursorSamples ();
90 doFormattingSamples ();
93 doDataPilotSamples ();
94 doNamedRangesSamples ();
95 doFunctionAccessSamples ();
96 doApplicationSettingsSamples ();
99 /// <summary>All samples regarding the service com.sun.star.sheet.SheetCell. </summary>
100 void doCellSamples ()
102 Console.WriteLine ("\n*** Samples for service sheet.SheetCell ***\n");
103 XSpreadsheet xSheet = GetSpreadsheet (0);
104 unoidl.com.sun.star.table.XCell xCell = null;
105 unoidl.com.sun.star.beans.XPropertySet xPropSet = null;
107 PrepareRange (xSheet, "A1:C7", "Cells and Cell Ranges");
109 // --- Get cell B3 by position - (column, row) ---
110 xCell = xSheet.getCellByPosition (1, 2);
112 // --- Insert two text paragraphs into the cell. ---
113 unoidl.com.sun.star.text.XText xText =
114 (unoidl.com.sun.star.text.XText) xCell;
115 unoidl.com.sun.star.text.XTextCursor xTextCursor =
116 xText.createTextCursor ();
118 xText.insertString (xTextCursor, "Text in first line.", false);
119 xText.insertControlCharacter (xTextCursor,
120 unoidl.com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
121 xText.insertString (xTextCursor, "And a ", false);
123 // create a hyperlink
124 XMultiServiceFactory xServiceMan = (XMultiServiceFactory) Document;
125 object aHyperlinkObj =
126 xServiceMan.createInstance ("com.sun.star.text.TextField.URL");
127 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) aHyperlinkObj;
128 xPropSet.setPropertyValue (
129 "URL", new uno.Any ("http://www.example.org"));
130 xPropSet.setPropertyValue (
131 "Representation", new uno.Any ("hyperlink"));
133 unoidl.com.sun.star.text.XTextContent xContent =
134 (unoidl.com.sun.star.text.XTextContent) aHyperlinkObj;
135 xText.insertTextContent (xTextCursor, xContent, false);
137 // --- Query the separate paragraphs. ---
138 unoidl.com.sun.star.container.XEnumerationAccess xParaEA =
139 (unoidl.com.sun.star.container.XEnumerationAccess) xCell;
140 unoidl.com.sun.star.container.XEnumeration xParaEnum =
141 xParaEA.createEnumeration ();
142 // Go through the paragraphs
143 while (xParaEnum.hasMoreElements())
145 uno.Any aPortionObj = xParaEnum.nextElement ();
146 unoidl.com.sun.star.container.XEnumerationAccess xPortionEA =
147 (unoidl.com.sun.star.container.XEnumerationAccess)
149 unoidl.com.sun.star.container.XEnumeration xPortionEnum =
150 xPortionEA.createEnumeration ();
152 // Go through all text portions of a paragraph and construct string.
153 while (xPortionEnum.hasMoreElements())
155 unoidl.com.sun.star.text.XTextRange xRange =
156 (unoidl.com.sun.star.text.XTextRange)
157 xPortionEnum.nextElement().Value;
158 aText += xRange.getString ();
160 Console.WriteLine ("Paragraph text: " + aText);
164 // --- Change cell properties. ---
165 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCell;
166 // from styles.CharacterProperties
167 xPropSet.setPropertyValue (
168 "CharColor", new uno.Any ((Int32) 0x003399));
169 xPropSet.setPropertyValue (
170 "CharHeight", new uno.Any ((Single) 20.0));
171 // from styles.ParagraphProperties
172 xPropSet.setPropertyValue (
173 "ParaLeftMargin", new uno.Any ((Int32) 500));
174 // from table.CellProperties
175 xPropSet.setPropertyValue (
176 "IsCellBackgroundTransparent", new uno.Any (false));
177 xPropSet.setPropertyValue (
178 "CellBackColor", new uno.Any ((Int32) 0x99CCFF));
181 // --- Get cell address. ---
182 XCellAddressable xCellAddr =
183 (XCellAddressable) xCell;
184 unoidl.com.sun.star.table.CellAddress aAddress =
185 xCellAddr.getCellAddress ();
186 aText = "Address of this cell: Column=" + aAddress.Column;
187 aText += "; Row=" + aAddress.Row;
188 aText += "; Sheet=" + aAddress.Sheet;
189 Console.WriteLine (aText);
192 // --- Insert an annotation ---
193 XSheetAnnotationsSupplier xAnnotationsSupp =
194 (XSheetAnnotationsSupplier) xSheet;
195 XSheetAnnotations xAnnotations =
196 xAnnotationsSupp.getAnnotations ();
197 xAnnotations.insertNew (aAddress, "This is an annotation");
199 XSheetAnnotationAnchor xAnnotAnchor =
200 (XSheetAnnotationAnchor) xCell;
201 XSheetAnnotation xAnnotation =
202 xAnnotAnchor.getAnnotation ();
203 xAnnotation.setIsVisible (true);
206 /// <summary>All samples regarding the service com.sun.star.sheet.SheetCellRange.</summary>
207 void doCellRangeSamples()
209 Console.WriteLine ("\n*** Samples for service sheet.SheetCellRange ***\n");
210 XSpreadsheet xSheet = GetSpreadsheet (0);
211 unoidl.com.sun.star.table.XCellRange xCellRange = null;
212 unoidl.com.sun.star.beans.XPropertySet xPropSet = null;
213 unoidl.com.sun.star.table.CellRangeAddress aRangeAddress = null;
216 SetCellFormula (xSheet, "B5", "First cell");
217 SetCellFormula (xSheet, "B6", "Second cell");
218 // Get cell range B5:B6 by position - (column, row, column, row)
219 xCellRange = xSheet.getCellRangeByPosition (1, 4, 1, 5);
222 // --- Change cell range properties. ---
223 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
224 // from com.sun.star.styles.CharacterProperties
225 xPropSet.setPropertyValue (
226 "CharColor", new uno.Any ((Int32) 0x003399));
227 xPropSet.setPropertyValue (
228 "CharHeight", new uno.Any ((Single) 20.0));
229 // from com.sun.star.styles.ParagraphProperties
230 xPropSet.setPropertyValue (
231 "ParaLeftMargin", new uno.Any ((Int32) 500));
232 // from com.sun.star.table.CellProperties
233 xPropSet.setPropertyValue (
234 "IsCellBackgroundTransparent", new uno.Any (false));
235 xPropSet.setPropertyValue (
236 "CellBackColor", new uno.Any ((Int32) 0x99CCFF));
239 // --- Replace text in all cells. ---
240 unoidl.com.sun.star.util.XReplaceable xReplace =
241 (unoidl.com.sun.star.util.XReplaceable) xCellRange;
242 unoidl.com.sun.star.util.XReplaceDescriptor xReplaceDesc =
243 xReplace.createReplaceDescriptor ();
244 xReplaceDesc.setSearchString ("cell");
245 xReplaceDesc.setReplaceString ("text");
246 // property SearchWords searches for whole cells!
247 xReplaceDesc.setPropertyValue ("SearchWords", new uno.Any (false));
248 int nCount = xReplace.replaceAll (xReplaceDesc);
249 Console.WriteLine ("Search text replaced " + nCount + " times.");
252 // --- Merge cells. ---
253 xCellRange = xSheet.getCellRangeByName ("F3:G6");
254 PrepareRange (xSheet, "E1:H7", "XMergeable");
255 unoidl.com.sun.star.util.XMergeable xMerge =
256 (unoidl.com.sun.star.util.XMergeable) xCellRange;
260 // --- Change indentation. ---
261 /* does not work (bug in XIndent implementation)
262 PrepareRange (xSheet, "I20:I23", "XIndent");
263 setValue (xSheet, "I21", 1);
264 setValue (xSheet, "I22", 1);
265 setValue (xSheet, "I23", 1);
267 xCellRange = xSheet.getCellRangeByName ("I21:I22");
268 unoidl.com.sun.star.util.XIndent xIndent =
269 (unoidl.com.sun.star.util.XIndent) xCellRange;
270 xIndent.incrementIndent ();
272 xCellRange = xSheet.getCellRangeByName ("I22:I23");
273 xIndent = (unoidl.com.sun.star.util.XIndent) xCellRange;
274 xIndent.incrementIndent ();
278 // --- Column properties. ---
279 xCellRange = xSheet.getCellRangeByName ("B1");
280 unoidl.com.sun.star.table.XColumnRowRange xColRowRange =
281 (unoidl.com.sun.star.table.XColumnRowRange) xCellRange;
282 unoidl.com.sun.star.table.XTableColumns xColumns =
283 xColRowRange.getColumns ();
285 uno.Any aColumnObj = xColumns.getByIndex (0);
286 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) aColumnObj.Value;
287 xPropSet.setPropertyValue ("Width", new uno.Any ((Int32) 6000));
289 unoidl.com.sun.star.container.XNamed xNamed =
290 (unoidl.com.sun.star.container.XNamed) aColumnObj.Value;
292 "The name of the wide column is " + xNamed.getName() + ".");
295 // --- Cell range data ---
296 PrepareRange (xSheet, "A9:C30", "XCellRangeData");
298 xCellRange = xSheet.getCellRangeByName ("A10:C30");
299 XCellRangeData xData =
300 (XCellRangeData) xCellRange;
301 uno.Any [][] aValues =
303 new uno.Any [] { new uno.Any ("Name"),
304 new uno.Any ("Fruit"),
305 new uno.Any ("Quantity") },
306 new uno.Any [] { new uno.Any ("Alice"),
307 new uno.Any ("Apples"),
308 new uno.Any ((double) 3.0) },
309 new uno.Any [] { new uno.Any ("Alice"),
310 new uno.Any ("Oranges"),
311 new uno.Any ((double) 7.0) },
312 new uno.Any [] { new uno.Any ("Bob"),
313 new uno.Any ("Apples"),
314 new uno.Any ((double) 3.0) },
315 new uno.Any [] { new uno.Any ("Alice"),
316 new uno.Any ("Apples"),
317 new uno.Any ((double) 9.0) },
318 new uno.Any [] { new uno.Any ("Bob"),
319 new uno.Any ("Apples"),
320 new uno.Any ((double) 5.0) },
321 new uno.Any [] { new uno.Any ("Bob"),
322 new uno.Any ("Oranges"),
323 new uno.Any ((double) 6.0) },
324 new uno.Any [] { new uno.Any ("Alice"),
325 new uno.Any ("Oranges"),
326 new uno.Any ((double) 3.0) },
327 new uno.Any [] { new uno.Any ("Alice"),
328 new uno.Any ("Apples"),
329 new uno.Any ((double) 8.0) },
330 new uno.Any [] { new uno.Any ("Alice"),
331 new uno.Any ("Oranges"),
332 new uno.Any ((double) 1.0) },
333 new uno.Any [] { new uno.Any ("Bob"),
334 new uno.Any ("Oranges"),
335 new uno.Any ((double) 2.0) },
336 new uno.Any [] { new uno.Any ("Bob"),
337 new uno.Any ("Oranges"),
338 new uno.Any ((double) 7.0) },
339 new uno.Any [] { new uno.Any ("Bob"),
340 new uno.Any ("Apples"),
341 new uno.Any ((double) 1.0) },
342 new uno.Any [] { new uno.Any ("Alice"),
343 new uno.Any ("Apples"),
344 new uno.Any ((double) 8.0) },
345 new uno.Any [] { new uno.Any ("Alice"),
346 new uno.Any ("Oranges"),
347 new uno.Any ((double) 8.0) },
348 new uno.Any [] { new uno.Any ("Alice"),
349 new uno.Any ("Apples"),
350 new uno.Any ((double) 7.0) },
351 new uno.Any [] { new uno.Any ("Bob"),
352 new uno.Any ("Apples"),
353 new uno.Any ((double) 1.0) },
354 new uno.Any [] { new uno.Any ("Bob"),
355 new uno.Any ("Oranges"),
356 new uno.Any ((double) 9.0) },
357 new uno.Any [] { new uno.Any ("Bob"),
358 new uno.Any ("Oranges"),
359 new uno.Any ((double) 3.0) },
360 new uno.Any [] { new uno.Any ("Alice"),
361 new uno.Any ("Oranges"),
362 new uno.Any ((double) 4.0) },
363 new uno.Any [] { new uno.Any ("Alice"),
364 new uno.Any ("Apples"),
365 new uno.Any ((double) 9.0) }
367 xData.setDataArray (aValues);
370 // --- Get cell range address. ---
371 XCellRangeAddressable xRangeAddr =
372 (XCellRangeAddressable) xCellRange;
373 aRangeAddress = xRangeAddr.getRangeAddress ();
375 "Address of this range: Sheet=" + aRangeAddress.Sheet);
377 "Start column=" + aRangeAddress.StartColumn + "; Start row=" +
378 aRangeAddress.StartRow);
380 "End column =" + aRangeAddress.EndColumn + "; End row =" +
381 aRangeAddress.EndRow);
384 // --- Sheet operation. ---
385 // uses the range filled with XCellRangeData
386 XSheetOperation xSheetOp = (XSheetOperation) xData;
387 double fResult = xSheetOp.computeFunction (GeneralFunction.AVERAGE);
388 Console.WriteLine ("Average value of the data table A10:C30: " + fResult);
391 // --- Fill series ---
392 // Prepare the example
393 SetCellValue (xSheet, "E10", 1);
394 SetCellValue (xSheet, "E11", 4);
395 SetCellDate (xSheet, "E12", 30, 1, 2002);
396 SetCellFormula (xSheet, "I13", "Text 10");
397 SetCellFormula (xSheet, "E14", "Jan");
398 SetCellValue (xSheet, "K14", 10);
399 SetCellValue (xSheet, "E16", 1);
400 SetCellValue (xSheet, "F16", 2);
401 SetCellDate (xSheet, "E17", 28, 2, 2002);
402 SetCellDate (xSheet, "F17", 28, 1, 2002);
403 SetCellValue (xSheet, "E18", 6);
404 SetCellValue (xSheet, "F18", 4);
406 XCellSeries xSeries = null;
407 // Fill 2 rows linear with end value
408 // -> 2nd series is not filled completely
409 xSeries = getCellSeries (xSheet, "E10:I11");
411 FillDirection.TO_RIGHT,
413 FillDateMode.FILL_DATE_DAY, 2, 9);
414 // Add months to a date
415 xSeries = getCellSeries (xSheet, "E12:I12");
417 FillDirection.TO_RIGHT,
419 FillDateMode.FILL_DATE_MONTH,
421 // Fill right to left with a text containing a value
422 xSeries = getCellSeries (xSheet, "E13:I13");
424 FillDirection.TO_LEFT,
426 FillDateMode.FILL_DATE_DAY,
428 // Fill with an user defined list
429 xSeries = getCellSeries (xSheet, "E14:I14");
431 FillDirection.TO_RIGHT,
433 FillDateMode.FILL_DATE_DAY,
435 // Fill bottom to top with a geometric series
436 xSeries = getCellSeries (xSheet, "K10:K14");
438 FillDirection.TO_TOP,
440 FillDateMode.FILL_DATE_DAY,
443 xSeries = getCellSeries (xSheet, "E16:K18");
445 FillDirection.TO_RIGHT, 2);
446 // Fill series copies cell formats -> draw border here
447 PrepareRange (xSheet, "E9:K18", "XCellSeries");
450 // --- Array formulas ---
451 xCellRange = xSheet.getCellRangeByName ("E21:G23");
452 PrepareRange (xSheet, "E20:G23", "XArrayFormulaRange");
453 XArrayFormulaRange xArrayFormula =
454 (XArrayFormulaRange) xCellRange;
455 // Insert a 3x3 unit matrix.
456 xArrayFormula.setArrayFormula ("=A10:C12");
457 Console.WriteLine ("Array formula is: " + xArrayFormula.getArrayFormula());
460 // --- Multiple operations ---
461 SetCellFormula (xSheet, "E26", "=E27^F26");
462 SetCellValue (xSheet, "E27", 1);
463 SetCellValue (xSheet, "F26", 1);
464 getCellSeries (xSheet, "E27:E31").fillAuto (FillDirection.TO_BOTTOM, 1);
465 getCellSeries (xSheet, "F26:J26").fillAuto (FillDirection.TO_RIGHT, 1);
466 SetCellFormula (xSheet, "F33", "=SIN(E33)");
467 SetCellFormula (xSheet, "G33", "=COS(E33)");
468 SetCellFormula (xSheet, "H33", "=TAN(E33)");
469 SetCellValue (xSheet, "E34", 0);
470 SetCellValue (xSheet, "E35", 0.2);
471 getCellSeries (xSheet, "E34:E38").fillAuto (FillDirection.TO_BOTTOM, 2);
472 PrepareRange (xSheet, "E25:J38", "XMultipleOperation");
474 unoidl.com.sun.star.table.CellRangeAddress aFormulaRange = CreateCellRangeAddress (xSheet, "E26");
475 unoidl.com.sun.star.table.CellAddress aColCell = CreateCellAddress (xSheet, "E27");
476 unoidl.com.sun.star.table.CellAddress aRowCell = CreateCellAddress (xSheet, "F26");
478 xCellRange = xSheet.getCellRangeByName ("E26:J31");
479 XMultipleOperation xMultOp = (XMultipleOperation) xCellRange;
480 xMultOp.setTableOperation (
481 aFormulaRange, TableOperationMode.BOTH,
484 aFormulaRange = CreateCellRangeAddress (xSheet, "F33:H33");
485 aColCell = CreateCellAddress (xSheet, "E33");
486 // Row cell not needed
488 xCellRange = xSheet.getCellRangeByName ("E34:H38");
489 xMultOp = (XMultipleOperation) xCellRange;
490 xMultOp.setTableOperation (
491 aFormulaRange, TableOperationMode.COLUMN,
495 // --- Cell Ranges Query ---
496 xCellRange = xSheet.getCellRangeByName ("A10:C30");
497 XCellRangesQuery xRangesQuery =
498 (XCellRangesQuery) xCellRange;
499 XSheetCellRanges xCellRanges =
500 xRangesQuery.queryContentCells (
501 (short) CellFlags.STRING);
503 "Cells in A10:C30 containing text: "
504 + xCellRanges.getRangeAddressesAsString());
507 /** Returns the XCellSeries interface of a cell range.
508 @param xSheet The spreadsheet containing the cell range.
509 @param aRange The address of the cell range.
510 @return The XCellSeries interface. */
511 XCellSeries getCellSeries (
512 XSpreadsheet xSheet, string aRange)
515 xSheet.getCellRangeByName (aRange);
518 // ________________________________________________________________
520 /** All samples regarding cell range collections. */
521 void doCellRangesSamples ()
523 Console.WriteLine ("\n*** Samples for cell range collections ***\n");
525 // Create a new cell range container
526 XMultiServiceFactory xDocFactory = (XMultiServiceFactory) Document;
527 XSheetCellRangeContainer xRangeCont =
528 (XSheetCellRangeContainer)
529 xDocFactory.createInstance (
530 "com.sun.star.sheet.SheetCellRanges");
533 // --- Insert ranges ---
534 insertRange (xRangeCont, 0, 0, 0, 0, 0, false); // A1:A1
535 insertRange (xRangeCont, 0, 0, 1, 0, 2, true); // A2:A3
536 insertRange (xRangeCont, 0, 1, 0, 1, 2, false); // B1:B3
539 // --- Query the list of filled cells ---
540 Console.WriteLine ("All filled cells: ");
541 unoidl.com.sun.star.container.XEnumerationAccess xCellsEA =
542 xRangeCont.getCells ();
543 unoidl.com.sun.star.container.XEnumeration xEnum =
544 xCellsEA.createEnumeration ();
545 while (xEnum.hasMoreElements())
547 uno.Any aCellObj = xEnum.nextElement ();
548 XCellAddressable xAddr =
549 (XCellAddressable) aCellObj.Value;
550 unoidl.com.sun.star.table.CellAddress aAddr =
551 xAddr.getCellAddress ();
553 GetCellAddressString (aAddr.Column, aAddr.Row) + " ");
555 Console.WriteLine ();
558 /** Inserts a cell range address into a cell range container and prints
560 @param xContainer XSheetCellRangeContainer
561 interface of the container.
562 @param nSheet Index of sheet of the range.
563 @param nStartCol Index of first column of the range.
564 @param nStartRow Index of first row of the range.
565 @param nEndCol Index of last column of the range.
566 @param nEndRow Index of last row of the range.
567 @param bMerge Determines whether the new range should be merged
568 with the existing ranges.
571 XSheetCellRangeContainer xContainer,
572 int nSheet, int nStartCol, int nStartRow, int nEndCol, int nEndRow,
575 unoidl.com.sun.star.table.CellRangeAddress aAddress =
576 new unoidl.com.sun.star.table.CellRangeAddress ();
577 aAddress.Sheet = (short)nSheet;
578 aAddress.StartColumn = nStartCol;
579 aAddress.StartRow = nStartRow;
580 aAddress.EndColumn = nEndCol;
581 aAddress.EndRow = nEndRow;
582 xContainer.addRangeAddress (aAddress, bMerge);
584 "Inserting " + GetCellRangeAddressString (aAddress)
585 + " " + (bMerge ? " with" : "without") + " merge,"
586 + " resulting list: " + xContainer.getRangeAddressesAsString());
589 // ________________________________________________________________
591 /** All samples regarding cell cursors. */
592 void doCellCursorSamples ()
594 Console.WriteLine ("\n*** Samples for cell cursor ***\n");
595 XSpreadsheet xSheet = GetSpreadsheet (0);
598 // --- Find the array formula using a cell cursor ---
599 unoidl.com.sun.star.table.XCellRange xRange =
600 xSheet.getCellRangeByName ("F22");
601 XSheetCellRange xCellRange =
602 (XSheetCellRange) xRange;
603 XSheetCellCursor xCursor =
604 xSheet.createCursorByRange (xCellRange);
606 xCursor.collapseToCurrentArray ();
607 XArrayFormulaRange xArray =
608 (XArrayFormulaRange) xCursor;
610 "Array formula in " + GetCellRangeAddressString (xCursor, false)
611 + " contains formula " + xArray.getArrayFormula());
614 // --- Find the used area ---
615 XUsedAreaCursor xUsedCursor =
616 (XUsedAreaCursor) xCursor;
617 xUsedCursor.gotoStartOfUsedArea (false);
618 xUsedCursor.gotoEndOfUsedArea (true);
619 // xUsedCursor and xCursor are interfaces of the same object -
620 // so modifying xUsedCursor takes effect on xCursor:
621 Console.WriteLine ("The used area is: " + GetCellRangeAddressString (xCursor, true));
624 // ________________________________________________________________
626 /** All samples regarding the formatting of cells and ranges. */
627 void doFormattingSamples ()
629 Console.WriteLine ("\n*** Formatting samples ***\n");
630 XSpreadsheet xSheet = GetSpreadsheet (1);
631 unoidl.com.sun.star.table.XCellRange xCellRange;
632 unoidl.com.sun.star.beans.XPropertySet xPropSet = null;
633 unoidl.com.sun.star.container.XIndexAccess xRangeIA = null;
634 XMultiServiceFactory xServiceManager;
637 // --- Cell styles ---
638 // get the cell style container
639 unoidl.com.sun.star.style.XStyleFamiliesSupplier xFamiliesSupplier =
640 (unoidl.com.sun.star.style.XStyleFamiliesSupplier) Document;
641 unoidl.com.sun.star.container.XNameAccess xFamiliesNA =
642 xFamiliesSupplier.getStyleFamilies ();
643 uno.Any aCellStylesObj = xFamiliesNA.getByName ("CellStyles");
644 unoidl.com.sun.star.container.XNameContainer xCellStylesNA =
645 (unoidl.com.sun.star.container.XNameContainer) aCellStylesObj.Value;
647 // create a new cell style
649 (XMultiServiceFactory) Document;
650 object aCellStyle = xServiceManager.createInstance (
651 "com.sun.star.style.CellStyle");
652 string aStyleName = "MyNewCellStyle";
653 xCellStylesNA.insertByName (
654 aStyleName, new uno.Any (typeof (object), aCellStyle));
656 // modify properties of the new style
657 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) aCellStyle;
658 xPropSet.setPropertyValue (
659 "CellBackColor", new uno.Any ((Int32) 0x888888));
660 xPropSet.setPropertyValue (
661 "IsCellBackgroundTransparent", new uno.Any (false));
665 // --- Query equal-formatted cell ranges ---
666 // prepare example, use the new cell style
667 xCellRange = xSheet.getCellRangeByName ("D2:F2");
668 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
669 xPropSet.setPropertyValue ("CellStyle", new uno.Any (aStyleName));
671 xCellRange = xSheet.getCellRangeByName ("A3:G3");
672 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
673 xPropSet.setPropertyValue ("CellStyle", new uno.Any (aStyleName));
675 // All ranges in one container
676 xCellRange = xSheet.getCellRangeByName ("A1:G3");
677 Console.WriteLine ("Service CellFormatRanges:");
678 XCellFormatRangesSupplier xFormatSupp =
679 (XCellFormatRangesSupplier) xCellRange;
680 xRangeIA = xFormatSupp.getCellFormatRanges ();
681 Console.WriteLine (GetCellRangeListString (xRangeIA));
683 // Ranges sorted in SheetCellRanges containers
684 Console.WriteLine ("\nService UniqueCellFormatRanges:");
685 XUniqueCellFormatRangesSupplier
687 (XUniqueCellFormatRangesSupplier)
689 unoidl.com.sun.star.container.XIndexAccess xRangesIA =
690 xUniqueFormatSupp.getUniqueCellFormatRanges ();
691 int nCount = xRangesIA.getCount ();
692 for (int nIndex = 0; nIndex < nCount; ++nIndex)
694 uno.Any aRangesObj = xRangesIA.getByIndex (nIndex);
696 (unoidl.com.sun.star.container.XIndexAccess) aRangesObj.Value;
698 "Container " + (nIndex + 1) + ": " +
699 GetCellRangeListString (xRangeIA));
703 // --- Table auto formats ---
704 // get the global collection of table auto formats,
705 // use global service manager
706 xServiceManager = ServiceManager;
707 object aAutoFormatsObj = xServiceManager.createInstance (
708 "com.sun.star.sheet.TableAutoFormats");
709 unoidl.com.sun.star.container.XNameContainer xAutoFormatsNA =
710 (unoidl.com.sun.star.container.XNameContainer) aAutoFormatsObj;
712 // create a new table auto format and insert into the container
713 string aAutoFormatName = "Temp_Example";
714 bool bExistsAlready = xAutoFormatsNA.hasByName (aAutoFormatName);
715 uno.Any aAutoFormatObj;
717 // auto format already exists -> use it
718 aAutoFormatObj = xAutoFormatsNA.getByName (aAutoFormatName);
721 // create a new auto format (with document service manager!)
723 (XMultiServiceFactory) Document;
724 aAutoFormatObj = new uno.Any (
726 xServiceManager.createInstance (
727 "com.sun.star.sheet.TableAutoFormat"));
728 xAutoFormatsNA.insertByName (aAutoFormatName, aAutoFormatObj);
730 // index access to the auto format fields
731 unoidl.com.sun.star.container.XIndexAccess xAutoFormatIA =
732 (unoidl.com.sun.star.container.XIndexAccess) aAutoFormatObj.Value;
734 // set properties of all auto format fields
735 for (int nRow = 0; nRow < 4; ++nRow)
740 case 0: nRowColor = 0x999999; break;
741 case 1: nRowColor = 0xFFFFCC; break;
742 case 2: nRowColor = 0xEEEEEE; break;
743 case 3: nRowColor = 0x999999; break;
746 for (int nColumn = 0; nColumn < 4; ++nColumn)
748 int nColor = nRowColor;
749 if ((nColumn == 0) || (nColumn == 3))
752 // get the auto format field and apply properties
753 uno.Any aFieldObj = xAutoFormatIA.getByIndex (
756 (unoidl.com.sun.star.beans.XPropertySet) aFieldObj.Value;
757 xPropSet.setPropertyValue (
758 "CellBackColor", new uno.Any ((Int32) nColor));
762 // set the auto format to the spreadsheet
763 xCellRange = xSheet.getCellRangeByName ("A5:H25");
764 unoidl.com.sun.star.table.XAutoFormattable xAutoForm =
765 (unoidl.com.sun.star.table.XAutoFormattable) xCellRange;
766 xAutoForm.autoFormat (aAutoFormatName);
768 // remove the auto format
770 xAutoFormatsNA.removeByName (aAutoFormatName);
773 // --- Conditional formats ---
774 xSheet = GetSpreadsheet (0);
775 PrepareRange (xSheet, "K20:K23", "Cond. Format");
776 SetCellValue (xSheet, "K21", 1);
777 SetCellValue (xSheet, "K22", 2);
778 SetCellValue (xSheet, "K23", 3);
780 // get the conditional format object of the cell range
781 xCellRange = xSheet.getCellRangeByName ("K21:K23");
782 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
783 XSheetConditionalEntries xEntries =
784 (XSheetConditionalEntries)
785 xPropSet.getPropertyValue ("ConditionalFormat").Value;
787 // create a condition and apply it to the range
788 unoidl.com.sun.star.beans.PropertyValue[] aCondition =
789 new unoidl.com.sun.star.beans.PropertyValue[3];
790 aCondition[0] = new unoidl.com.sun.star.beans.PropertyValue ();
791 aCondition[0].Name = "Operator";
792 aCondition[0].Value =
794 typeof (ConditionOperator),
795 ConditionOperator.GREATER);
796 aCondition[1] = new unoidl.com.sun.star.beans.PropertyValue ();
797 aCondition[1].Name = "Formula1";
798 aCondition[1].Value = new uno.Any ("1");
799 aCondition[2] = new unoidl.com.sun.star.beans.PropertyValue ();
800 aCondition[2].Name = "StyleName";
801 aCondition[2].Value = new uno.Any (aStyleName);
802 xEntries.addNew (aCondition);
803 xPropSet.setPropertyValue (
806 typeof (XSheetConditionalEntries),
810 // ________________________________________________________________
812 /** All samples regarding the spreadsheet document. */
813 void doDocumentSamples ()
815 Console.WriteLine ("\n*** Samples for spreadsheet document ***\n");
818 // --- Insert a new spreadsheet ---
819 XSpreadsheet xSheet = InsertSpreadsheet ("A new sheet", (short) 0x7FFF);
822 // --- Copy a cell range ---
823 PrepareRange (xSheet, "A1:B3", "Copy from");
824 PrepareRange (xSheet, "D1:E3", "To");
825 SetCellValue (xSheet, "A2", 123);
826 SetCellValue (xSheet, "B2", 345);
827 SetCellFormula (xSheet, "A3", "=SUM(A2:B2)");
828 SetCellFormula (xSheet, "B3", "=FORMULA(A3)");
830 XCellRangeMovement xMovement =
831 (XCellRangeMovement) xSheet;
832 unoidl.com.sun.star.table.CellRangeAddress aSourceRange =
833 CreateCellRangeAddress (xSheet, "A2:B3");
834 unoidl.com.sun.star.table.CellAddress aDestCell =
835 CreateCellAddress (xSheet, "D2");
836 xMovement.copyRange (aDestCell, aSourceRange);
839 // --- Print automatic column page breaks ---
840 XSheetPageBreak xPageBreak =
841 (XSheetPageBreak) xSheet;
842 TablePageBreakData[] aPageBreakArray =
843 xPageBreak.getColumnPageBreaks ();
845 Console.Write ("Automatic column page breaks:");
846 for (int nIndex = 0; nIndex < aPageBreakArray.Length; ++nIndex)
847 if (!aPageBreakArray[nIndex].ManualBreak)
848 Console.Write (" " + aPageBreakArray[nIndex].Position);
849 Console.WriteLine ();
852 // --- Document properties ---
853 unoidl.com.sun.star.beans.XPropertySet xPropSet =
854 (unoidl.com.sun.star.beans.XPropertySet) Document;
856 string aText = "Value of property IsIterationEnabled: ";
858 (Boolean) xPropSet.getPropertyValue ("IsIterationEnabled").Value;
859 Console.WriteLine (aText);
860 aText = "Value of property IterationCount: ";
861 aText += (Int32) xPropSet.getPropertyValue ("IterationCount").Value;
862 Console.WriteLine (aText);
863 aText = "Value of property NullDate: ";
864 unoidl.com.sun.star.util.Date aDate = (unoidl.com.sun.star.util.Date)
865 xPropSet.getPropertyValue ("NullDate").Value;
866 aText += aDate.Year + "-" + aDate.Month + "-" + aDate.Day;
867 Console.WriteLine (aText);
870 // --- Data validation ---
871 PrepareRange (xSheet, "A5:C7", "Validation");
872 SetCellFormula (xSheet, "A6", "Insert values between 0.0 and 5.0 below:");
874 unoidl.com.sun.star.table.XCellRange xCellRange =
875 xSheet.getCellRangeByName ("A7:C7");
876 unoidl.com.sun.star.beans.XPropertySet xCellPropSet =
877 (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
878 // validation properties
879 unoidl.com.sun.star.beans.XPropertySet xValidPropSet =
880 (unoidl.com.sun.star.beans.XPropertySet)
881 xCellPropSet.getPropertyValue ("Validation").Value;
882 xValidPropSet.setPropertyValue (
885 typeof (ValidationType),
886 ValidationType.DECIMAL));
887 xValidPropSet.setPropertyValue (
888 "ShowErrorMessage", new uno.Any (true));
889 xValidPropSet.setPropertyValue (
890 "ErrorMessage", new uno.Any ("This is an invalid value!"));
891 xValidPropSet.setPropertyValue (
894 typeof (ValidationAlertStyle),
895 ValidationAlertStyle.STOP));
897 XSheetCondition xCondition =
898 (XSheetCondition) xValidPropSet;
899 xCondition.setOperator (
900 ConditionOperator.BETWEEN);
901 xCondition.setFormula1 ("0.0");
902 xCondition.setFormula2 ("5.0");
903 // apply on cell range
904 xCellPropSet.setPropertyValue (
907 typeof (unoidl.com.sun.star.beans.XPropertySet),
912 uno.Any [][] aValues = {
913 new uno.Any [] { uno.Any.VOID, uno.Any.VOID },
914 new uno.Any [] { uno.Any.VOID, uno.Any.VOID }
917 aValues[ 0 ][ 0 ] = new uno.Any ((double) 11);
918 aValues[ 0 ][ 1 ] = new uno.Any ((double) 12);
919 aValues[ 1 ][ 0 ] = new uno.Any ("Test13");
920 aValues[ 1 ][ 1 ] = new uno.Any ("Test14");
922 xSheet, "B10:C11", aValues,
923 "First Scenario", "The first scenario.");
925 aValues[ 0 ][ 0 ] = new uno.Any ("Test21");
926 aValues[ 0 ][ 1 ] = new uno.Any ("Test22");
927 aValues[ 1 ][ 0 ] = new uno.Any ((double) 23);
928 aValues[ 1 ][ 1 ] = new uno.Any ((double) 24);
930 xSheet, "B10:C11", aValues,
931 "Second Scenario", "The visible scenario.");
933 aValues[ 0 ][ 0 ] = new uno.Any ((double) 31);
934 aValues[ 0 ][ 1 ] = new uno.Any ((double) 32);
935 aValues[ 1 ][ 0 ] = new uno.Any ("Test33");
936 aValues[ 1 ][ 1 ] = new uno.Any ("Test34");
938 xSheet, "B10:C11", aValues,
939 "Third Scenario", "The last scenario.");
941 // show second scenario
942 showScenario (xSheet, "Second Scenario");
945 /** Inserts a scenario containing one cell range into a sheet and
946 applies the value array.
947 @param xSheet The XSpreadsheet interface of the spreadsheet.
948 @param aRange The range address for the scenario.
949 @param aValueArray The array of cell contents.
950 @param aScenarioName The name of the new scenario.
951 @param aScenarioComment The user comment for the scenario. */
952 void insertScenario (
955 uno.Any [][] aValueArray,
956 string aScenarioName,
957 string aScenarioComment)
959 // get the cell range with the given address
960 unoidl.com.sun.star.table.XCellRange xCellRange =
961 xSheet.getCellRangeByName (aRange);
963 // create the range address sequence
964 XCellRangeAddressable xAddr =
965 (XCellRangeAddressable) xCellRange;
966 unoidl.com.sun.star.table.CellRangeAddress[] aRangesSeq =
967 new unoidl.com.sun.star.table.CellRangeAddress[1];
968 aRangesSeq[0] = xAddr.getRangeAddress ();
970 // create the scenario
971 XScenariosSupplier xScenSupp =
972 (XScenariosSupplier) xSheet;
973 XScenarios xScenarios =
974 xScenSupp.getScenarios ();
975 xScenarios.addNewByName (aScenarioName, aRangesSeq, aScenarioComment);
977 // insert the values into the range
978 XCellRangeData xData =
979 (XCellRangeData) xCellRange;
980 xData.setDataArray (aValueArray);
983 /** Activates a scenario.
984 @param xSheet The XSpreadsheet interface of the spreadsheet.
985 @param aScenarioName The name of the scenario. */
988 string aScenarioName)
990 // get the scenario set
991 XScenariosSupplier xScenSupp =
992 (XScenariosSupplier) xSheet;
993 XScenarios xScenarios =
994 xScenSupp.getScenarios ();
996 // get the scenario and activate it
997 uno.Any aScenarioObj = xScenarios.getByName (aScenarioName);
998 XScenario xScenario =
999 (XScenario) aScenarioObj.Value;
1003 // ________________________________________________________________
1005 void doNamedRangesSamples ()
1007 Console.WriteLine ("\n*** Samples for named ranges ***\n");
1008 XSpreadsheetDocument xDocument = Document;
1009 XSpreadsheet xSheet = GetSpreadsheet (0);
1012 // --- Named ranges ---
1013 PrepareRange (xSheet, "G42:H45", "Named ranges");
1014 xSheet.getCellByPosition (6, 42).setValue (1);
1015 xSheet.getCellByPosition (6, 43).setValue (2);
1016 xSheet.getCellByPosition (7, 42).setValue (3);
1017 xSheet.getCellByPosition (7, 43).setValue (4);
1019 // insert a named range
1020 unoidl.com.sun.star.beans.XPropertySet xDocProp =
1021 (unoidl.com.sun.star.beans.XPropertySet) xDocument;
1022 uno.Any aRangesObj = xDocProp.getPropertyValue ("NamedRanges");
1023 XNamedRanges xNamedRanges =
1024 (XNamedRanges) aRangesObj.Value;
1025 unoidl.com.sun.star.table.CellAddress aRefPos =
1026 new unoidl.com.sun.star.table.CellAddress ();
1030 xNamedRanges.addNewByName ("ExampleName", "SUM(G43:G44)", aRefPos, 0);
1032 // use the named range in formulas
1033 xSheet.getCellByPosition (6, 44).setFormula ("=ExampleName");
1034 xSheet.getCellByPosition (7, 44).setFormula ("=ExampleName");
1037 // --- Label ranges ---
1038 PrepareRange (xSheet, "G47:I50", "Label ranges");
1039 unoidl.com.sun.star.table.XCellRange xRange =
1040 xSheet.getCellRangeByPosition (6, 47, 7, 49);
1041 XCellRangeData xData =
1042 (XCellRangeData) xRange;
1043 uno.Any [][] aValues =
1045 new uno.Any [] { new uno.Any ("Apples"),
1046 new uno.Any ("Oranges") },
1047 new uno.Any [] { new uno.Any ((double) 5),
1048 new uno.Any ((double) 7) },
1049 new uno.Any [] { new uno.Any ((double) 6),
1050 new uno.Any ((double) 8) }
1052 xData.setDataArray (aValues);
1054 // insert a column label range
1055 uno.Any aLabelsObj = xDocProp.getPropertyValue ("ColumnLabelRanges");
1056 XLabelRanges xLabelRanges =
1057 (XLabelRanges) aLabelsObj.Value;
1058 unoidl.com.sun.star.table.CellRangeAddress aLabelArea =
1059 new unoidl.com.sun.star.table.CellRangeAddress ();
1060 aLabelArea.Sheet = 0;
1061 aLabelArea.StartColumn = 6;
1062 aLabelArea.StartRow = 47;
1063 aLabelArea.EndColumn = 7;
1064 aLabelArea.EndRow = 47;
1065 unoidl.com.sun.star.table.CellRangeAddress aDataArea =
1066 new unoidl.com.sun.star.table.CellRangeAddress ();
1067 aDataArea.Sheet = 0;
1068 aDataArea.StartColumn = 6;
1069 aDataArea.StartRow = 48;
1070 aDataArea.EndColumn = 7;
1071 aDataArea.EndRow = 49;
1072 xLabelRanges.addNew (aLabelArea, aDataArea);
1074 // use the label range in formulas
1075 xSheet.getCellByPosition (8, 48).setFormula ("=Apples+Oranges");
1076 xSheet.getCellByPosition (8, 49).setFormula ("=Apples+Oranges");
1079 // ________________________________________________________________
1081 /** Helper for doDatabaseSamples: get name of first database. */
1082 string getFirstDatabaseName ()
1084 string aDatabase = null;
1085 XMultiServiceFactory xServiceManager = ServiceManager;
1086 unoidl.com.sun.star.container.XNameAccess xContext =
1087 (unoidl.com.sun.star.container.XNameAccess)
1088 xServiceManager.createInstance (
1089 "com.sun.star.sdb.DatabaseContext");
1090 string[] aNames = xContext.getElementNames ();
1091 if (aNames.Length > 0)
1092 aDatabase = aNames[0];
1096 /** Helper for doDatabaseSamples: get name of first table in a database. */
1097 string getFirstTableName (string aDatabase)
1099 if (aDatabase == null)
1102 string aTable = null;
1103 XMultiServiceFactory xServiceManager = ServiceManager;
1104 unoidl.com.sun.star.container.XNameAccess xContext =
1105 (unoidl.com.sun.star.container.XNameAccess)
1106 xServiceManager.createInstance (
1107 "com.sun.star.sdb.DatabaseContext");
1108 unoidl.com.sun.star.sdb.XCompletedConnection xSource =
1109 (unoidl.com.sun.star.sdb.XCompletedConnection)
1110 xContext.getByName (aDatabase).Value;
1111 unoidl.com.sun.star.task.XInteractionHandler xHandler =
1112 (unoidl.com.sun.star.task.XInteractionHandler)
1113 xServiceManager.createInstance (
1114 "com.sun.star.sdb.InteractionHandler");
1115 unoidl.com.sun.star.sdbcx.XTablesSupplier xSupplier =
1116 (unoidl.com.sun.star.sdbcx.XTablesSupplier)
1117 xSource.connectWithCompletion (xHandler);
1118 unoidl.com.sun.star.container.XNameAccess xTables =
1119 xSupplier.getTables ();
1120 string[] aNames = xTables.getElementNames ();
1121 if (aNames.Length > 0)
1126 void doDatabaseSamples ()
1128 Console.WriteLine ("\n*** Samples for database operations ***\n");
1129 XSpreadsheet xSheet = GetSpreadsheet (2);
1132 // --- put some example data into the sheet ---
1133 unoidl.com.sun.star.table.XCellRange xRange =
1134 xSheet.getCellRangeByName ("B3:D24");
1135 XCellRangeData xData =
1136 (XCellRangeData) xRange;
1137 uno.Any [][] aValues =
1139 new uno.Any [] { new uno.Any ("Name"),
1140 new uno.Any ("Year"),
1141 new uno.Any ("Sales") },
1142 new uno.Any [] { new uno.Any ("Alice"),
1143 new uno.Any ((double) 2001),
1144 new uno.Any ((double) 4.0) },
1145 new uno.Any [] { new uno.Any ("Carol"),
1146 new uno.Any ((double) 1997),
1147 new uno.Any ((double) 3.0) },
1148 new uno.Any [] { new uno.Any ("Carol"),
1149 new uno.Any ((double) 1998),
1150 new uno.Any ((double) 8.0) },
1151 new uno.Any [] { new uno.Any ("Bob"),
1152 new uno.Any ((double) 1997),
1153 new uno.Any ((double) 8.0) },
1154 new uno.Any [] { new uno.Any ("Alice"),
1155 new uno.Any ((double) 2002),
1156 new uno.Any ((double) 9.0) },
1157 new uno.Any [] { new uno.Any ("Alice"),
1158 new uno.Any ((double) 1999),
1159 new uno.Any ((double) 7.0) },
1160 new uno.Any [] { new uno.Any ("Alice"),
1161 new uno.Any ((double) 1996),
1162 new uno.Any ((double) 3.0) },
1163 new uno.Any [] { new uno.Any ("Bob"),
1164 new uno.Any ((double) 2000),
1165 new uno.Any ((double) 1.0) },
1166 new uno.Any [] { new uno.Any ("Carol"),
1167 new uno.Any ((double) 1999),
1168 new uno.Any ((double) 5.0) },
1169 new uno.Any [] { new uno.Any ("Bob"),
1170 new uno.Any ((double) 2002),
1171 new uno.Any ((double) 1.0) },
1172 new uno.Any [] { new uno.Any ("Carol"),
1173 new uno.Any ((double) 2001),
1174 new uno.Any ((double) 5.0) },
1175 new uno.Any [] { new uno.Any ("Carol"),
1176 new uno.Any ((double) 2000),
1177 new uno.Any ((double) 1.0) },
1178 new uno.Any [] { new uno.Any ("Carol"),
1179 new uno.Any ((double) 1996),
1180 new uno.Any ((double) 8.0) },
1181 new uno.Any [] { new uno.Any ("Bob"),
1182 new uno.Any ((double) 1996),
1183 new uno.Any ((double) 7.0) },
1184 new uno.Any [] { new uno.Any ("Alice"),
1185 new uno.Any ((double) 1997),
1186 new uno.Any ((double) 3.0) },
1187 new uno.Any [] { new uno.Any ("Alice"),
1188 new uno.Any ((double) 2000),
1189 new uno.Any ((double) 9.0) },
1190 new uno.Any [] { new uno.Any ("Bob"),
1191 new uno.Any ((double) 1998),
1192 new uno.Any ((double) 1.0) },
1193 new uno.Any [] { new uno.Any ("Bob"),
1194 new uno.Any ((double) 1999),
1195 new uno.Any ((double) 6.0) },
1196 new uno.Any [] { new uno.Any ("Carol"),
1197 new uno.Any ((double) 2002),
1198 new uno.Any ((double) 8.0) },
1199 new uno.Any [] { new uno.Any ("Alice"),
1200 new uno.Any ((double) 1998),
1201 new uno.Any ((double) 5.0) },
1202 new uno.Any [] { new uno.Any ("Bob"),
1203 new uno.Any ((double) 2001),
1204 new uno.Any ((double) 6.0) }
1206 xData.setDataArray (aValues);
1209 // --- filter for second column >= 1998 ---
1210 XSheetFilterable xFilter =
1211 (XSheetFilterable) xRange;
1212 XSheetFilterDescriptor xFilterDesc =
1213 xFilter.createFilterDescriptor (true);
1214 TableFilterField[] aFilterFields =
1215 new TableFilterField[1];
1216 aFilterFields[0] = new TableFilterField ();
1217 aFilterFields[0].Field = 1;
1218 aFilterFields[0].IsNumeric = true;
1219 aFilterFields[0].Operator =
1220 FilterOperator.GREATER_EQUAL;
1221 aFilterFields[0].NumericValue = 1998;
1222 xFilterDesc.setFilterFields (aFilterFields);
1223 unoidl.com.sun.star.beans.XPropertySet xFilterProp =
1224 (unoidl.com.sun.star.beans.XPropertySet) xFilterDesc;
1225 xFilterProp.setPropertyValue (
1226 "ContainsHeader", new uno.Any (true));
1227 xFilter.filter (xFilterDesc);
1230 // --- do the same filter as above, using criteria from a cell range ---
1231 unoidl.com.sun.star.table.XCellRange xCritRange =
1232 xSheet.getCellRangeByName ("B27:B28");
1233 XCellRangeData xCritData =
1234 (XCellRangeData) xCritRange;
1235 uno.Any [][] aCritValues =
1237 new uno.Any [] { new uno.Any ("Year") },
1238 new uno.Any [] { new uno.Any (">= 1998") }
1240 xCritData.setDataArray (aCritValues);
1241 XSheetFilterableEx xCriteria =
1242 (XSheetFilterableEx) xCritRange;
1243 xFilterDesc = xCriteria.createFilterDescriptorByObject (xFilter);
1244 if (xFilterDesc != null)
1245 xFilter.filter (xFilterDesc);
1248 // --- sort by second column, ascending ---
1249 unoidl.com.sun.star.util.SortField[] aSortFields =
1250 new unoidl.com.sun.star.util.SortField[1];
1251 aSortFields[0] = new unoidl.com.sun.star.util.SortField ();
1252 aSortFields[0].Field = 1;
1253 aSortFields[0].SortAscending = true;
1255 unoidl.com.sun.star.beans.PropertyValue[] aSortDesc =
1256 new unoidl.com.sun.star.beans.PropertyValue[2];
1257 aSortDesc[0] = new unoidl.com.sun.star.beans.PropertyValue ();
1258 aSortDesc[0].Name = "SortFields";
1259 aSortDesc[0].Value =
1261 typeof (unoidl.com.sun.star.util.SortField []),
1263 aSortDesc[1] = new unoidl.com.sun.star.beans.PropertyValue ();
1264 aSortDesc[1].Name = "ContainsHeader";
1265 aSortDesc[1].Value = new uno.Any (true);
1267 unoidl.com.sun.star.util.XSortable xSort =
1268 (unoidl.com.sun.star.util.XSortable) xRange;
1269 xSort.sort (aSortDesc);
1272 // --- insert subtotals ---
1273 XSubTotalCalculatable xSub =
1274 (XSubTotalCalculatable) xRange;
1275 XSubTotalDescriptor xSubDesc =
1276 xSub.createSubTotalDescriptor (true);
1277 SubTotalColumn[] aColumns =
1278 new SubTotalColumn[1];
1279 // calculate sum of third column
1280 aColumns[0] = new SubTotalColumn ();
1281 aColumns[0].Column = 2;
1282 aColumns[0].Function = GeneralFunction.SUM;
1283 // group by first column
1284 xSubDesc.addNew (aColumns, 0);
1285 xSub.applySubTotals (xSubDesc, true);
1287 string aDatabase = getFirstDatabaseName ();
1288 string aTableName = getFirstTableName (aDatabase);
1289 if (aDatabase != null && aTableName != null)
1291 // --- import from database ---
1292 unoidl.com.sun.star.beans.PropertyValue[] aImportDesc =
1293 new unoidl.com.sun.star.beans.PropertyValue[3];
1294 aImportDesc[0] = new unoidl.com.sun.star.beans.PropertyValue ();
1295 aImportDesc[0].Name = "DatabaseName";
1296 aImportDesc[0].Value = new uno.Any (aDatabase);
1297 aImportDesc[1] = new unoidl.com.sun.star.beans.PropertyValue ();
1298 aImportDesc[1].Name = "SourceType";
1299 aImportDesc[1].Value =
1301 typeof (DataImportMode),
1302 DataImportMode.TABLE);
1303 aImportDesc[2] = new unoidl.com.sun.star.beans.PropertyValue ();
1304 aImportDesc[2].Name = "SourceObject";
1305 aImportDesc[2].Value = new uno.Any (aTableName);
1307 unoidl.com.sun.star.table.XCellRange xImportRange =
1308 xSheet.getCellRangeByName ("B35:B35");
1309 unoidl.com.sun.star.util.XImportable xImport =
1310 (unoidl.com.sun.star.util.XImportable) xImportRange;
1311 xImport.doImport (aImportDesc);
1314 // --- use the temporary database range to find the
1315 // imported data's size ---
1316 unoidl.com.sun.star.beans.XPropertySet xDocProp =
1317 (unoidl.com.sun.star.beans.XPropertySet) Document;
1318 uno.Any aRangesObj = xDocProp.getPropertyValue ("DatabaseRanges");
1319 unoidl.com.sun.star.container.XNameAccess xRanges =
1320 (unoidl.com.sun.star.container.XNameAccess) aRangesObj.Value;
1321 string[] aNames = xRanges.getElementNames ();
1322 for (int i=0; i<aNames.Length; i++)
1324 uno.Any aRangeObj = xRanges.getByName (aNames[i]);
1325 unoidl.com.sun.star.beans.XPropertySet xRangeProp =
1326 (unoidl.com.sun.star.beans.XPropertySet) aRangeObj.Value;
1327 bool bUser = (Boolean)
1328 xRangeProp.getPropertyValue ("IsUserDefined").Value;
1330 // this is the temporary database range -
1331 // get the cell range and format it
1332 XCellRangeReferrer xRef =
1333 (XCellRangeReferrer)
1335 unoidl.com.sun.star.table.XCellRange xResultRange =
1336 xRef.getReferredCells ();
1337 unoidl.com.sun.star.beans.XPropertySet xResultProp =
1338 (unoidl.com.sun.star.beans.XPropertySet) xResultRange;
1339 xResultProp.setPropertyValue (
1340 "IsCellBackgroundTransparent", new uno.Any (false));
1341 xResultProp.setPropertyValue (
1342 "CellBackColor", new uno.Any ((int) 0xFFFFCC));
1347 Console.WriteLine("can't get database");
1350 // ________________________________________________________________
1352 void doDataPilotSamples ()
1354 Console.WriteLine ("\n*** Samples for Data Pilot ***\n");
1355 XSpreadsheet xSheet = GetSpreadsheet (0);
1358 // --- Create a new DataPilot table ---
1359 PrepareRange (xSheet, "A38:C38", "Data Pilot");
1360 XDataPilotTablesSupplier xDPSupp = (XDataPilotTablesSupplier) xSheet;
1361 XDataPilotTables xDPTables = xDPSupp.getDataPilotTables ();
1362 XDataPilotDescriptor xDPDesc = xDPTables.createDataPilotDescriptor ();
1364 // set source range (use data range from CellRange test)
1365 unoidl.com.sun.star.table.CellRangeAddress aSourceAddress = CreateCellRangeAddress (xSheet, "A10:C30");
1366 xDPDesc.setSourceRange (aSourceAddress);
1368 // settings for fields
1369 unoidl.com.sun.star.container.XIndexAccess xFields = xDPDesc.getDataPilotFields ();
1371 unoidl.com.sun.star.beans.XPropertySet xFieldProp;
1373 // use first column as column field
1374 aFieldObj = xFields.getByIndex(0);
1375 xFieldProp = (unoidl.com.sun.star.beans.XPropertySet) aFieldObj.Value;
1376 xFieldProp.setPropertyValue ("Orientation",
1377 new uno.Any (typeof (DataPilotFieldOrientation), DataPilotFieldOrientation.COLUMN));
1379 // use second column as row field
1380 aFieldObj = xFields.getByIndex(1);
1381 xFieldProp = (unoidl.com.sun.star.beans.XPropertySet) aFieldObj.Value;
1382 xFieldProp.setPropertyValue ("Orientation",
1383 new uno.Any (typeof (DataPilotFieldOrientation),DataPilotFieldOrientation.ROW));
1385 // use third column as data field, calculating the sum
1386 aFieldObj = xFields.getByIndex(2);
1387 xFieldProp = (unoidl.com.sun.star.beans.XPropertySet) aFieldObj.Value;
1388 xFieldProp.setPropertyValue ("Orientation",
1389 new uno.Any (typeof (DataPilotFieldOrientation), DataPilotFieldOrientation.DATA));
1390 xFieldProp.setPropertyValue ("Function",
1391 new uno.Any (typeof (GeneralFunction), GeneralFunction.SUM));
1393 // select output position
1394 unoidl.com.sun.star.table.CellAddress aDestAddress = CreateCellAddress (xSheet, "A40");
1395 xDPTables.insertNewByName ("DataPilotExample", aDestAddress, xDPDesc);
1398 // --- Modify the DataPilot table ---
1399 uno.Any aDPTableObj = xDPTables.getByName ("DataPilotExample");
1400 xDPDesc = (XDataPilotDescriptor) aDPTableObj.Value;
1401 xFields = xDPDesc.getDataPilotFields ();
1403 // add a second data field from the third column,
1404 // calculating the average
1405 aFieldObj = xFields.getByIndex(2);
1406 xFieldProp = (unoidl.com.sun.star.beans.XPropertySet) aFieldObj.Value;
1407 xFieldProp.setPropertyValue ("Orientation",
1408 new uno.Any (typeof (DataPilotFieldOrientation), DataPilotFieldOrientation.DATA));
1409 xFieldProp.setPropertyValue ("Function",
1410 new uno.Any (typeof (GeneralFunction), GeneralFunction.AVERAGE));
1413 void doFunctionAccessSamples ()
1415 Console.WriteLine ("\n*** Samples for function handling ***\n");
1416 XMultiServiceFactory xServiceManager = ServiceManager;
1418 // --- Calculate a function ---
1419 object aFuncInst = xServiceManager.createInstance ("com.sun.star.sheet.FunctionAccess");
1420 XFunctionAccess xFuncAcc = (XFunctionAccess) aFuncInst;
1422 // put the data in a two-dimensional array
1423 double [][] aData = { new double [] { 1.0, 2.0, 3.0 } };
1425 // construct the array of function arguments
1426 uno.Any [] aArgs = new uno.Any [2];
1427 aArgs[0] = new uno.Any (typeof (double [][]), aData);
1428 aArgs[1] = new uno.Any ((double) 2.0);
1429 uno.Any aResult = xFuncAcc.callFunction ("ZTEST", aArgs);
1430 Console.WriteLine ("ZTEST result for data {1,2,3} and value 2 is " + aResult.Value);
1432 // --- Get the list of recently used functions ---
1433 object aRecInst = xServiceManager.createInstance ("com.sun.star.sheet.RecentFunctions");
1434 XRecentFunctions xRecFunc = (XRecentFunctions) aRecInst;
1435 int[] nRecentIds = xRecFunc.getRecentFunctionIds ();
1437 // --- Get the names for these functions ---
1438 object aDescInst = xServiceManager.createInstance ("com.sun.star.sheet.FunctionDescriptions");
1439 XFunctionDescriptions xFuncDesc = (XFunctionDescriptions) aDescInst;
1440 Console.Write("Recently used functions: ");
1441 for (int nFunction=0; nFunction<nRecentIds.Length; nFunction++)
1443 unoidl.com.sun.star.beans.PropertyValue[] aProperties = xFuncDesc.getById (nRecentIds[nFunction]);
1444 for (int nProp=0; nProp<aProperties.Length; nProp++)
1445 if (aProperties[nProp].Name.Equals ("Name"))
1446 Console.Write (aProperties[nProp].Value + " ");
1448 Console.WriteLine ();
1451 void doApplicationSettingsSamples ()
1453 Console.WriteLine ("\n*** Samples for application settings ***\n");
1454 XMultiServiceFactory xServiceManager = ServiceManager;
1456 // --- Get the user defined sort lists ---
1457 object aSettings = xServiceManager.createInstance ("com.sun.star.sheet.GlobalSheetSettings");
1458 unoidl.com.sun.star.beans.XPropertySet xPropSet = (unoidl.com.sun.star.beans.XPropertySet) aSettings;
1459 string[] aEntries = (string []) xPropSet.getPropertyValue ("UserLists").Value;
1460 Console.WriteLine("User defined sort lists:");
1461 for (int i=0; i<aEntries.Length; i++)
1462 Console.WriteLine (aEntries[i]);