* Makefile.am:
[monodevelop.git] / extras / OpenOfficeSamples / OpenOfficeSpreadsheetDocHelper.xft.xml
blobdf2c818a4066f056e3d2a8b15f560bf194f05368
1 <?xml version="1.0"?>
2 <Template Originator   = "Michael Hutchinson"
3           Language     = "C#"
4           Created      = "2008/05/30"
5           LastModified = "2008/05/30">  
6         
7         <TemplateConfiguration>
8                 <_Name>SpreadsheetDocHelper</_Name>
9                 <Icon>md-text-file-icon</Icon>
10                 <_Category>OpenOffice Samples</_Category>
11                 <LanguageName>C#</LanguageName>
12                 <_Description>Spreadsheet helper class for the OpenOffice samples.</_Description>
13         </TemplateConfiguration>
14         
15         <Conditions>
16                 <ParentProject PermittedCreationPaths="" ExcludedFiles="/SpreadsheetDocHelper.cs" />
17         </Conditions>
18         
19         <TemplateFiles>
20                 <File Name="SpreadsheetDocHelper.cs" AddStandardHeader="False"><![CDATA[// 
21 // This library is free software; you can redistribute it and/or
22 // modify it under the terms of the GNU Lesser General Public
23 // License as published by the Free Software Foundation; either 
24 // version 2.1 of the License, or (at your option) any later version.
25 // 
26 // This library is distributed in the hope that it will be useful,
27 // but WITHOUT ANY WARRANTY; without even the implied warranty of
28 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29 // Lesser General Public License for more details.
30 // 
31 // You should have received a copy of the GNU Lesser General Public 
32 // License along with this library.  If not, see <http://www.gnu.org/licenses/>.
35 using System;
36 using unoidl.com.sun.star.lang;
37 using unoidl.com.sun.star.uno;
38 using unoidl.com.sun.star.bridge;
39 using unoidl.com.sun.star.frame;
40 using unoidl.com.sun.star.sheet;
41 using unoidl.com.sun.star.container;
42 using unoidl.com.sun.star.util;
43 using unoidl.com.sun.star.table;
44 using unoidl.com.sun.star.beans;
46 namespace OpenOffice.Samples
48         
49         /// <summary>
50         /// This is a helper class for the spreadsheet and table samples.
51         /// It connects to a running office and creates a spreadsheet document.
52         /// Additionally it contains various helper functions.
53         /// </summary>
54         public class SpreadsheetDocHelper : IDisposable
55         {
56                 XComponentContext componentContext;
57                 XMultiServiceFactory  multiServiceFactory;
58                 XSpreadsheetDocument document;
59                 
60                 public SpreadsheetDocHelper (string[] args)
61                 {
62                         // Connect to a running office and get the service manager
63                         multiServiceFactory = Connect (args);
64                         // Create a new spreadsheet document
65                         document = InitDocument ();
66                 }
67                 
68                 #region Helper methods
69                 
70                 /// <summary>Returns the service manager.</summary>
71                 /// <returns> The <see cref="XMultiServiceFactory"/> interface of the service manager.</returns>
72                 public XMultiServiceFactory ServiceManager {
73                         get { return multiServiceFactory; }
74                 }
75                 
76                 /// <summary>Returns the whole spreadsheet document.</summary>
77                 /// <returns> The <see cref="XSpreadsheetDocument"/> interface of the document.</returns>
78                 public XSpreadsheetDocument Document {
79                         get { return document; }
80                 }
81                 
82                 /// <summary> Returns the spreadsheet with the specified index. </summary>
83                 /// <param name="index">The index of the sheet (0-based). </param>
84                 /// <returns> The <see cref="XSpreadsheet"/> interface of the sheet. </returns>
85                 public XSpreadsheet GetSpreadsheet (int index)
86                 {
87                         XSpreadsheets sheets = document.getSheets ();
88                         XIndexAccess sheetsIA = (XIndexAccess) sheets;
89                         return (XSpreadsheet) sheetsIA.getByIndex (index).Value;
90                 }
91                 
92                 /// <summary> Inserts a new empty spreadsheet with the specified name. </summary>
93                 /// <param name="name"> The name of the new sheet. </param>
94                 /// <param name="index"> The insertion index. </param>
95                 /// <returns> The <see cref="XSpreadsheet"/> interface of the new sheet. </returns>
96                 public XSpreadsheet InsertSpreadsheet (string name, short index)
97                 {
98                         XSpreadsheets sheets = document.getSheets ();
99                         sheets.insertNewByName (name, index);
100                         return (XSpreadsheet) sheets.getByName (name).Value;
101                 }
102                 
103                 #endregion
104         
105                 #region Methods to fill values into cells.
106         
107                 /// <summary> Writes a double value into a spreadsheet. </summary>
108                 /// <param name="sheet"> The <see cref="XSpreadsheet"/> interface of the spreadsheet. </param>
109                 /// <param name="cellName"> The address of the cell (or a named range). </param>
110                 /// <param name="cellValue"> The value to write into the cell.</param>
111                 public void SetCellValue (XSpreadsheet sheet, string cellName, double cellValue)
112                 {
113                         sheet.getCellRangeByName (cellName).getCellByPosition (0, 0).setValue (cellValue);
114                 }
115                 
116                 /// <summary> Writes a formula into a spreadsheet. </summary>
117                 /// <param name="sheet"> The <see cref="XSpreadsheet"/> interface of the spreadsheet. </param>
118                 /// <param name="cellName"> The address of the cell (or a named range). </param>
119                 /// <param name="formula"> The formula to write into the cell. </param>
120                 public void SetCellFormula (XSpreadsheet sheet, string cellName, string formula)
121                 {
122                         sheet.getCellRangeByName (cellName).getCellByPosition (0, 0).setFormula (formula);
123                 }
124                 
125                 /// <summary> Writes a date with standard date format into a spreadsheet. </summary>
126                 /// <param name="sheet"> The <see cref="XSpreadsheet"/> interface of the spreadsheet. </param>
127                 /// <param name="cellName"> The address of the cell (or a named range). </param>
128                 /// <param name="day"> The day of the date. </param>
129                 /// <param name="month"> The month of the date. </param>
130                 /// <param name="year">The year of the date. </param>
131                 public void SetCellDate (XSpreadsheet sheet, string cellName, int day, int month, int year)
132                 {
133                         // Set the date value.
134                         XCell cell = sheet.getCellRangeByName (cellName).getCellByPosition (0, 0);
135                         cell.setFormula (month + "/" + day + "/" + year);
136         
137                         // Set standard date format.
138                         XNumberFormatsSupplier formatsSupplier = (XNumberFormatsSupplier) Document;
139                         XNumberFormatTypes formatTypes = (XNumberFormatTypes) formatsSupplier.getNumberFormats ();
140                         int numberFormat = formatTypes.getStandardFormat (NumberFormat.DATE, new Locale ());
141                         XPropertySet propSet = (unoidl.com.sun.star.beans.XPropertySet) cell;
142                         propSet.setPropertyValue ("NumberFormat", new uno.Any (numberFormat));
143                 }
144                 
145                 /// <summary>Draws a colored border around the range and writes the headline in the first cell. </summary>
146                 /// <param name="sheet"> The <see cref="XSpreadsheet"/> interface of the spreadsheet. </param>
147                 /// <param name="range"> The address of the cell range (or a named range). </param>
148                 /// <param name="headline"> The headline text. </param>
149                 public void PrepareRange (XSpreadsheet sheet, string range, string headline)
150                 {
151                         XPropertySet propSet = null;
152                         XCellRange cellRange = null;
153                         
154                         // draw border
155                         cellRange = sheet.getCellRangeByName (range);
156                         propSet = (XPropertySet) cellRange;
157                         BorderLine aLine = new BorderLine ();
158                         aLine.Color = 0x99CCFF;
159                         aLine.InnerLineWidth = aLine.LineDistance = 0;
160                         aLine.OuterLineWidth = 100;
161                         TableBorder border = new TableBorder ();
162                         border.TopLine = border.BottomLine = border.LeftLine = border.RightLine = aLine;
163                         border.IsTopLineValid = border.IsBottomLineValid = true;
164                         border.IsLeftLineValid = border.IsRightLineValid = true;
165                         propSet.setPropertyValue ("TableBorder", new uno.Any (typeof (TableBorder), border));
166         
167                         // draw headline
168                         XCellRangeAddressable xAddr = (XCellRangeAddressable) cellRange;
169                         CellRangeAddress aAddr = xAddr.getRangeAddress ();
170         
171                         cellRange = sheet.getCellRangeByPosition (
172                                 aAddr.StartColumn,
173                                 aAddr.StartRow, aAddr.EndColumn, aAddr.StartRow);
174                         
175                         propSet = (XPropertySet) cellRange;
176                         propSet.setPropertyValue ("CellBackColor", new uno.Any ((int) 0x99CCFF));
177                         
178                         // write headline
179                         XCell cell = cellRange.getCellByPosition (0, 0);
180                         cell.setFormula (headline);
181                         propSet = (XPropertySet) cell;
182                         propSet.setPropertyValue ("CharColor", new uno.Any ((int) 0x003399));
183                         propSet.setPropertyValue ("CharWeight",
184                                 new uno.Any ((Single) unoidl.com.sun.star.awt.FontWeight.BOLD));
185                 }
186                 
187                 #endregion
188                 
189                 #region Methods to create cell addresses and range addresses.
190         
191                 /// <summary> Creates a CellAddress and initializes it with the given range </summary>
192                 /// <param name="sheet"> The <see cref="XSpreadsheet"/> interface of the spreadsheet.   </param>
193                 /// <param name="cellName"> The address of the cell (or a named cell). </param>
194                 public CellAddress CreateCellAddress (XSpreadsheet sheet, string cellName)
195                 {
196                         XCellAddressable addr = (XCellAddressable) sheet.getCellRangeByName (cellName).getCellByPosition (0, 0);
197                         return addr.getCellAddress ();
198                 }
199                 
200                 /// <summary> Creates a CellRangeAddress and initializes it with the given range. </summary>
201                 /// <param name="sheet"> The <see cref="XSpreadsheet"/> interface of the spreadsheet. </param>
202                 /// <param name="range"> The address of the cell range (or a named range). </param>
203                 public CellRangeAddress CreateCellRangeAddress (XSpreadsheet sheet, string range)
204                 {
205                         XCellRangeAddressable addr = (XCellRangeAddressable) sheet.getCellRangeByName (range);
206                         return addr.getRangeAddress ();
207                 }
208                 
209                 #endregion
210                 
211                 #region Methods to convert cell addresses and range addresses to strings.
212                 
213                 /// <summary> Returns the text address of the cell. </summary>
214                 /// <param name="column"> The column index. </param>
215                 /// <param name="row"> The row index. </param>
216                 /// <returns> A string containing the cell address. </returns>
217                 public string GetCellAddressString (int column, int row)
218                 {
219                         return ((column > 25)? new string ((char) ('A' + column / 26 - 1), 1) : string.Empty)
220                                 + new string ((char) ('A' + column % 26), 1)
221                                 + (row + 1);
222                 }
223                 
224                 /// <summary> Returns the text address of the cell range. </summary>
225                 /// <param name="cellRange"> The cell range address. </param>
226                 /// <returns> A string containing the cell range address. </returns>
227                 public string GetCellRangeAddressString (CellRangeAddress cellRange)
228                 {
229                         return GetCellAddressString (cellRange.StartColumn, cellRange.StartRow)
230                                 + ":"
231                                 + GetCellAddressString (cellRange.EndColumn, cellRange.EndRow);
232                 }
233                 
234                 /// <summary> Returns the text address of the cell range. </summary>
235                 /// <param name="cellRange"> The XSheetCellRange interface of the cell range. </param>
236                 /// <param name="withSheetName"> Whether to include the sheet name. </param>
237                 /// <returns> A string containing the cell range address. </returns>
238                 public string GetCellRangeAddressString (XSheetCellRange cellRange, bool withSheetName)
239                 {
240                         XCellRangeAddressable addr = (XCellRangeAddressable) cellRange;
241                         string str = GetCellRangeAddressString (addr.getRangeAddress ());
242                         
243                         if (withSheetName) {
244                                 XSpreadsheet sheet = cellRange.getSpreadsheet ();
245                                 XNamed xNamed = (XNamed) sheet;
246                                 return xNamed.getName () + "." + str;
247                         } else {
248                                 return str;
249                         }
250                         
251                 }
252                 
253                 /// <summary>Returns a list of addresses of all cell ranges contained in the collection. </summary>
254                 /// <param name="rangesIA"> The <see cref="XIndexAccess"/> XIndexAccess interface of the collection. </param>
255                 /// <returns> A string containing the cell range address list. </returns>
256                 public string GetCellRangeListString (XIndexAccess rangesIA)
257                 {
258                         System.Text.StringBuilder sb = new System.Text.StringBuilder ();
259                         int count = rangesIA.getCount ();
260                         for (int i = 0; i < count; ++i)
261                         {
262                                 if (i > 0)
263                                         sb.Append (" ");
264                                 uno.Any rangeObj = rangesIA.getByIndex (i);
265                                 XSheetCellRange cellRange =
266                                         (XSheetCellRange) rangeObj.Value;
267                                 sb.Append (GetCellRangeAddressString (cellRange, false));
268                         }
269                         return sb.ToString ();
270                 }
271                 
272                 #endregion
273                 
274                 /// <summary> Connect to a running office that is accepting connections. </summary>
275                 /// <returns> The ServiceManager to instantiate office components. </returns>
276                 XMultiServiceFactory Connect (string [] args)
277                 {
278                         componentContext = uno.util.Bootstrap.bootstrap ();
279                         return (XMultiServiceFactory) componentContext.getServiceManager ();
280                 }
281         
282                 public void Dispose ()
283                 {
284                 }
285         
286                 /// <summary> Creates an empty spreadsheet document. </summary>
287                 /// <returns>The <see cref="XSpreadsheetDocument"/> interface of the document. </returns>
288                 XSpreadsheetDocument InitDocument ()
289                 {
290                         XComponentLoader loader 
291                                 = (XComponentLoader) multiServiceFactory.createInstance ("com.sun.star.frame.Desktop");
292                         return (XSpreadsheetDocument) loader.loadComponentFromURL
293                                 ("private:factory/scalc", "_blank", 0, new PropertyValue[0]);
294                 }
295         }
296         
297 }]]>
298                 </File>
299         </TemplateFiles>
300         
301         <FileOptions/>
302         
303 </Template>