Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OpenOfficeSpreadsheetDocHelper.xft.xml « OpenOfficeSamples « extras - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: df2c818a4066f056e3d2a8b15f560bf194f05368 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?xml version="1.0"?>
<Template Originator   = "Michael Hutchinson"
          Language     = "C#"
          Created      = "2008/05/30"
          LastModified = "2008/05/30">	
	
	<TemplateConfiguration>
		<_Name>SpreadsheetDocHelper</_Name>
		<Icon>md-text-file-icon</Icon>
		<_Category>OpenOffice Samples</_Category>
		<LanguageName>C#</LanguageName>
		<_Description>Spreadsheet helper class for the OpenOffice samples.</_Description>
	</TemplateConfiguration>
	
	<Conditions>
		<ParentProject PermittedCreationPaths="" ExcludedFiles="/SpreadsheetDocHelper.cs" />
	</Conditions>
	
	<TemplateFiles>
		<File Name="SpreadsheetDocHelper.cs" AddStandardHeader="False"><![CDATA[// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either 
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public 
// License along with this library.  If not, see <http://www.gnu.org/licenses/>.


using System;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.sheet;
using unoidl.com.sun.star.container;
using unoidl.com.sun.star.util;
using unoidl.com.sun.star.table;
using unoidl.com.sun.star.beans;

namespace OpenOffice.Samples
{
	
	/// <summary>
	/// This is a helper class for the spreadsheet and table samples.
	/// It connects to a running office and creates a spreadsheet document.
	/// Additionally it contains various helper functions.
	/// </summary>
	public class SpreadsheetDocHelper : IDisposable
	{
		XComponentContext componentContext;
		XMultiServiceFactory  multiServiceFactory;
		XSpreadsheetDocument document;
		
		public SpreadsheetDocHelper (string[] args)
		{
			// Connect to a running office and get the service manager
			multiServiceFactory = Connect (args);
			// Create a new spreadsheet document
			document = InitDocument ();
		}
		
		#region Helper methods
		
		/// <summary>Returns the service manager.</summary>
		/// <returns> The <see cref="XMultiServiceFactory"/> interface of the service manager.</returns>
		public XMultiServiceFactory ServiceManager {
			get { return multiServiceFactory; }
		}
		
		/// <summary>Returns the whole spreadsheet document.</summary>
		/// <returns> The <see cref="XSpreadsheetDocument"/> interface of the document.</returns>
		public XSpreadsheetDocument Document {
			get { return document; }
		}
		
		/// <summary> Returns the spreadsheet with the specified index. </summary>
		/// <param name="index">The index of the sheet (0-based). </param>
		/// <returns> The <see cref="XSpreadsheet"/> interface of the sheet. </returns>
		public XSpreadsheet GetSpreadsheet (int index)
		{
			XSpreadsheets sheets = document.getSheets ();
			XIndexAccess sheetsIA = (XIndexAccess) sheets;
			return (XSpreadsheet) sheetsIA.getByIndex (index).Value;
		}
		
		/// <summary> Inserts a new empty spreadsheet with the specified name. </summary>
		/// <param name="name"> The name of the new sheet. </param>
		/// <param name="index"> The insertion index. </param>
		/// <returns> The <see cref="XSpreadsheet"/> interface of the new sheet. </returns>
		public XSpreadsheet InsertSpreadsheet (string name, short index)
		{
			XSpreadsheets sheets = document.getSheets ();
			sheets.insertNewByName (name, index);
			return (XSpreadsheet) sheets.getByName (name).Value;
		}
		
		#endregion
	
		#region Methods to fill values into cells.
	
		/// <summary> Writes a double value into a spreadsheet. </summary>
		/// <param name="sheet"> The <see cref="XSpreadsheet"/> interface of the spreadsheet. </param>
		/// <param name="cellName"> The address of the cell (or a named range). </param>
		/// <param name="cellValue"> The value to write into the cell.</param>
		public void SetCellValue (XSpreadsheet sheet, string cellName, double cellValue)
		{
			sheet.getCellRangeByName (cellName).getCellByPosition (0, 0).setValue (cellValue);
		}
		
		/// <summary> Writes a formula into a spreadsheet. </summary>
		/// <param name="sheet"> The <see cref="XSpreadsheet"/> interface of the spreadsheet. </param>
		/// <param name="cellName"> The address of the cell (or a named range). </param>
		/// <param name="formula"> The formula to write into the cell. </param>
		public void SetCellFormula (XSpreadsheet sheet, string cellName, string formula)
		{
			sheet.getCellRangeByName (cellName).getCellByPosition (0, 0).setFormula (formula);
		}
		
		/// <summary> Writes a date with standard date format into a spreadsheet. </summary>
		/// <param name="sheet"> The <see cref="XSpreadsheet"/> interface of the spreadsheet. </param>
		/// <param name="cellName"> The address of the cell (or a named range). </param>
		/// <param name="day"> The day of the date. </param>
		/// <param name="month"> The month of the date. </param>
		/// <param name="year">The year of the date. </param>
		public void SetCellDate (XSpreadsheet sheet, string cellName, int day, int month, int year)
		{
			// Set the date value.
			XCell cell = sheet.getCellRangeByName (cellName).getCellByPosition (0, 0);
			cell.setFormula (month + "/" + day + "/" + year);
	
			// Set standard date format.
			XNumberFormatsSupplier formatsSupplier = (XNumberFormatsSupplier) Document;
			XNumberFormatTypes formatTypes = (XNumberFormatTypes) formatsSupplier.getNumberFormats ();
			int numberFormat = formatTypes.getStandardFormat (NumberFormat.DATE, new Locale ());
			XPropertySet propSet = (unoidl.com.sun.star.beans.XPropertySet) cell;
			propSet.setPropertyValue ("NumberFormat", new uno.Any (numberFormat));
		}
		
		/// <summary>Draws a colored border around the range and writes the headline in the first cell. </summary>
		/// <param name="sheet"> The <see cref="XSpreadsheet"/> interface of the spreadsheet. </param>
		/// <param name="range"> The address of the cell range (or a named range). </param>
		/// <param name="headline"> The headline text. </param>
		public void PrepareRange (XSpreadsheet sheet, string range, string headline)
		{
			XPropertySet propSet = null;
			XCellRange cellRange = null;
			
			// draw border
			cellRange = sheet.getCellRangeByName (range);
			propSet = (XPropertySet) cellRange;
			BorderLine aLine = new BorderLine ();
			aLine.Color = 0x99CCFF;
			aLine.InnerLineWidth = aLine.LineDistance = 0;
			aLine.OuterLineWidth = 100;
			TableBorder border = new TableBorder ();
			border.TopLine = border.BottomLine = border.LeftLine = border.RightLine = aLine;
			border.IsTopLineValid = border.IsBottomLineValid = true;
			border.IsLeftLineValid = border.IsRightLineValid = true;
			propSet.setPropertyValue ("TableBorder", new uno.Any (typeof (TableBorder), border));
	
			// draw headline
			XCellRangeAddressable xAddr = (XCellRangeAddressable) cellRange;
			CellRangeAddress aAddr = xAddr.getRangeAddress ();
	
			cellRange = sheet.getCellRangeByPosition (
				aAddr.StartColumn,
				aAddr.StartRow, aAddr.EndColumn, aAddr.StartRow);
			
			propSet = (XPropertySet) cellRange;
			propSet.setPropertyValue ("CellBackColor", new uno.Any ((int) 0x99CCFF));
			
			// write headline
			XCell cell = cellRange.getCellByPosition (0, 0);
			cell.setFormula (headline);
			propSet = (XPropertySet) cell;
			propSet.setPropertyValue ("CharColor", new uno.Any ((int) 0x003399));
			propSet.setPropertyValue ("CharWeight",
				new uno.Any ((Single) unoidl.com.sun.star.awt.FontWeight.BOLD));
		}
		
		#endregion
		
		#region Methods to create cell addresses and range addresses.
	
		/// <summary> Creates a CellAddress and initializes it with the given range </summary>
		/// <param name="sheet"> The <see cref="XSpreadsheet"/> interface of the spreadsheet.	</param>
		/// <param name="cellName"> The address of the cell (or a named cell). </param>
		public CellAddress CreateCellAddress (XSpreadsheet sheet, string cellName)
		{
			XCellAddressable addr = (XCellAddressable) sheet.getCellRangeByName (cellName).getCellByPosition (0, 0);
			return addr.getCellAddress ();
		}
		
		/// <summary> Creates a CellRangeAddress and initializes it with the given range. </summary>
		/// <param name="sheet"> The <see cref="XSpreadsheet"/> interface of the spreadsheet. </param>
		/// <param name="range"> The address of the cell range (or a named range). </param>
		public CellRangeAddress CreateCellRangeAddress (XSpreadsheet sheet, string range)
		{
			XCellRangeAddressable addr = (XCellRangeAddressable) sheet.getCellRangeByName (range);
			return addr.getRangeAddress ();
		}
		
		#endregion
		
		#region Methods to convert cell addresses and range addresses to strings.
		
		/// <summary> Returns the text address of the cell. </summary>
		/// <param name="column"> The column index. </param>
		/// <param name="row"> The row index. </param>
		/// <returns> A string containing the cell address. </returns>
		public string GetCellAddressString (int column, int row)
		{
			return ((column > 25)? new string ((char) ('A' + column / 26 - 1), 1) : string.Empty)
				+ new string ((char) ('A' + column % 26), 1)
				+ (row + 1);
		}
		
		/// <summary> Returns the text address of the cell range. </summary>
		/// <param name="cellRange"> The cell range address. </param>
		/// <returns> A string containing the cell range address. </returns>
		public string GetCellRangeAddressString (CellRangeAddress cellRange)
		{
			return GetCellAddressString (cellRange.StartColumn, cellRange.StartRow)
				+ ":"
				+ GetCellAddressString (cellRange.EndColumn, cellRange.EndRow);
		}
		
		/// <summary> Returns the text address of the cell range. </summary>
		/// <param name="cellRange"> The XSheetCellRange interface of the cell range. </param>
		/// <param name="withSheetName"> Whether to include the sheet name. </param>
		/// <returns> A string containing the cell range address. </returns>
		public string GetCellRangeAddressString (XSheetCellRange cellRange, bool withSheetName)
		{
			XCellRangeAddressable addr = (XCellRangeAddressable) cellRange;
			string str = GetCellRangeAddressString (addr.getRangeAddress ());
			
			if (withSheetName) {
				XSpreadsheet sheet = cellRange.getSpreadsheet ();
				XNamed xNamed = (XNamed) sheet;
				return xNamed.getName () + "." + str;
			} else {
				return str;
			}
			
		}
		
		/// <summary>Returns a list of addresses of all cell ranges contained in the collection. </summary>
		/// <param name="rangesIA"> The <see cref="XIndexAccess"/> XIndexAccess interface of the collection. </param>
		/// <returns> A string containing the cell range address list. </returns>
		public string GetCellRangeListString (XIndexAccess rangesIA)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder ();
			int count = rangesIA.getCount ();
			for (int i = 0; i < count; ++i)
			{
				if (i > 0)
					sb.Append (" ");
				uno.Any rangeObj = rangesIA.getByIndex (i);
				XSheetCellRange cellRange =
					(XSheetCellRange) rangeObj.Value;
				sb.Append (GetCellRangeAddressString (cellRange, false));
			}
			return sb.ToString ();
		}
		
		#endregion
		
		/// <summary> Connect to a running office that is accepting connections. </summary>
		/// <returns> The ServiceManager to instantiate office components. </returns>
		XMultiServiceFactory Connect (string [] args)
		{
			componentContext = uno.util.Bootstrap.bootstrap ();
			return (XMultiServiceFactory) componentContext.getServiceManager ();
		}
	
		public void Dispose ()
		{
		}
	
		/// <summary> Creates an empty spreadsheet document. </summary>
		/// <returns>The <see cref="XSpreadsheetDocument"/> interface of the document. </returns>
		XSpreadsheetDocument InitDocument ()
		{
			XComponentLoader loader 
				= (XComponentLoader) multiServiceFactory.createInstance ("com.sun.star.frame.Desktop");
			return (XSpreadsheetDocument) loader.loadComponentFromURL
				("private:factory/scalc", "_blank", 0, new PropertyValue[0]);
		}
	}
	
}]]>
		</File>
	</TemplateFiles>
	
	<FileOptions/>
	
</Template>