<_Name>Spreadsheet View Sample
<_Category>C#/OpenOffice Samples
md-project
C#
<_Description>Creates a new C# OpenOffice integration sample project.
${ProjectName}
.
using System;
using System.Threading;
using unoidl.com.sun.star.sheet;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.container;
using unoidl.com.sun.star.table;
using unoidl.com.sun.star.view;
using unoidl.com.sun.star.frame;
namespace OpenOffice.Samples
{
/// Create and modify a spreadsheet view.
public class ViewSample : SpreadsheetDocHelper
{
public static void Main (String [] args)
{
try {
using (ViewSample sample = new ViewSample (args)) {
sample.doSampleFunction ();
}
Console.WriteLine ("\nSamples done.");
} catch (Exception ex) {
Console.WriteLine ("Sample caught exception! " + ex);
}
}
public ViewSample (string[] args)
: base (args)
{
}
///
/// This sample function performs all changes on the view.
///
public void doSampleFunction()
{
XModel model = (XModel) Document;
XController xController = model.getCurrentController();
// --- Spreadsheet view ---
// freeze the first column and first two rows
XViewFreezable xFreeze = (XViewFreezable) xController;
if (null != xFreeze)
Console.WriteLine ("got xFreeze");
xFreeze.freezeAtPosition (1, 2);
// --- View pane ---
// get the cell range shown in the second pane and assign
// a cell background to them
XIndexAccess xIndex = (XIndexAccess) xController;
uno.Any aPane = xIndex.getByIndex(1);
XCellRangeReferrer xRefer = (XCellRangeReferrer) aPane.Value;
XCellRange xRange = xRefer.getReferredCells();
XPropertySet xRangeProp = (XPropertySet) xRange;
xRangeProp.setPropertyValue("IsCellBackgroundTransparent", new uno.Any (false));
xRangeProp.setPropertyValue("CellBackColor", new uno.Any ((int) 0xFFFFCC));
// --- View settings ---
// change the view to display green grid lines
XPropertySet xProp = (XPropertySet) xController;
xProp.setPropertyValue ("ShowGrid", new uno.Any (true));
xProp.setPropertyValue ("GridColor", new uno.Any ((int) 0x00CC00));
// --- Range selection ---
// let the user select a range and use it as the view's selection
XRangeSelection xRngSel = (XRangeSelection) xController;
ExampleRangeListener aListener = new ExampleRangeListener();
xRngSel.addRangeSelectionListener (aListener);
PropertyValue[] aArguments = new PropertyValue[2];
aArguments[0] = new PropertyValue ();
aArguments[0].Name = "Title";
aArguments[0].Value = new uno.Any ("Please select a range");
aArguments[1] = new PropertyValue();
aArguments[1].Name = "CloseOnMouseRelease";
aArguments[1].Value = new uno.Any (true);
xRngSel.startRangeSelection (aArguments);
Monitor.Enter (aListener);
try {
// wait until the selection is done
Monitor.Wait (aListener);
} finally {
Monitor.Exit (aListener);
}
xRngSel.removeRangeSelectionListener (aListener);
if (aListener.aResult != null && aListener.aResult.Length != 0) {
XSelectionSupplier xSel = (XSelectionSupplier) xController;
XSpreadsheetView xView = (XSpreadsheetView) xController;
XSpreadsheet xSheet = xView.getActiveSheet();
XCellRange xResultRange = xSheet.getCellRangeByName (aListener.aResult);
xSel.select (new uno.Any (typeof (XCellRange), xResultRange));
}
}
// listener to react on finished selection
private class ExampleRangeListener : XRangeSelectionListener
{
public string aResult;
public void done (RangeSelectionEvent evt)
{
aResult = evt.RangeDescriptor;
Monitor.Enter (this);
try {
Monitor.Pulse (this);
} finally {
Monitor.Exit (this);
}
}
public void aborted (RangeSelectionEvent aEvent)
{
Monitor.Enter (this);
try {
Monitor.Pulse (this);
} finally {
Monitor.Exit (this);
}
}
public void disposing (unoidl.com.sun.star.lang.EventObject aObj)
{
}
}
}
}]]>