Archive for August, 2005

Macros: Text Utilities Part I

Monday, August 29th, 2005

It is straightforward to enhance the capabilities of OOo Calc with your home grown functions and features. In this tip, we add the ability to change the case of the text in a range of selected cells.(UPPER CASE, lower case or Proper Case).

As you can in the screenshot below, we have added a new top level menu item TextUtils with 3 new functions, UpperC, LowerC, and ProperC. The last function capitalizes the first character only.

We could have invoked these functions with Tools - Macros etc but from a user’s standpoint, it is much more convenient to invoke them the way we have implemented it.

textutils_1.jpg

Pretty straightforward - the results of applying UpperC to the selected cells.

textutils_2.jpg

First - the source code…

Sub UpperC
oDesktop = createUnoService(”com.sun.star.frame.Desktop”)
oDocument = ThisComponent
oSelectedCells = oDocument.CurrentSelection
oActiveCells = oSelectedCells.RangeAddress
oSheets = oDocument.Sheets
oSheet = oSheets.getByIndex(oActiveCells.Sheet) ‘ active table
For nRow = oActiveCells.StartRow To oActiveCells.EndRow
For nCol = oActiveCells.StartColumn To oActiveCells.EndColumn
oCell = oSheet.getCellByPosition(nCol,nRow)
CellVal = oCell.getString()
oCell.setString(UCase(CellVal))
Next
Next
End Sub

Sub LowerC
oDesktop = createUnoService(”com.sun.star.frame.Desktop”)
oDocument = ThisComponent
oSelectedCells = oDocument.CurrentSelection
oActiveCells = oSelectedCells.RangeAddress
oSheets = oDocument.Sheets
oSheet = oSheets.getByIndex(oActiveCells.Sheet) ‘ active table
For nRow = oActiveCells.StartRow To oActiveCells.EndRow
For nCol = oActiveCells.StartColumn To oActiveCells.EndColumn
oCell = oSheet.getCellByPosition(nCol,nRow)
CellVal = oCell.getString()
oCell.setString(LCase(CellVal))
Next
Next
End Sub

Sub ProperC
oDesktop = createUnoService(”com.sun.star.frame.Desktop”)
oDocument = ThisComponent
oSelectedCells = oDocument.CurrentSelection
oActiveCells = oSelectedCells.RangeAddress
oSheets = oDocument.Sheets
oSheet = oSheets.getByIndex(oActiveCells.Sheet) ‘ active table
For nRow = oActiveCells.StartRow To oActiveCells.EndRow
For nCol = oActiveCells.StartColumn To oActiveCells.EndColumn
oCell = oSheet.getCellByPosition(nCol,nRow)
CellVal = oCell.getString()
oCell.setString(UCase(Left(CellVal,1)) & Right(CellVal,Len(CellVal) - 1) )
Next
Next
End Sub

Creating the new menu entries is simple. Invoke the Menu Customization dialog with Tools - Customize as shown below…

textutils_3.jpg

Select the Menus tab. Add a new top level menu item - TextUtils and then a seperate entry for each of our new functions.

textutils_4.jpg

Here is the Add Commands dialog. We select the module in the left hand box and the macro subroutine on the right. By default, the menu entry is given the same name as the subroutine but hat can be changed.

textutils_5.jpg

In the next installment, we will add a dialog to enhance out new functionality even further.

Summing across sheets

Thursday, August 25th, 2005

Often, we need to sum up a set of numbers that occupy the same position over a range of sheets. This is straightforward as shown below.

We have a sheet for each month as well as a Totals sheet. For illustration we add up the SUM of the A1 cells for each sheet from January to April with the formula =SUM(January.A1:April.A1). Note that this is slightly different from the corresponding Excel formula - =SUM(January:April!A1)

sheetsum.png

Counting unique entries in a range

Wednesday, August 24th, 2005

A good source of hints and tips for OOo Calc can be the numerous Excel blogs. One of the best is from Dick Kusleika.

This formula for counting the number of unique items in a range of cells was one of three suggested by Dick - but the only one that ported successfully to OOo Calc.

(more…)

Spelling out numbers

Tuesday, August 23rd, 2005

A simple macro that converts a numeric value to it’s English equivalent.

(more…)

DataPilot Revisited II

Monday, August 22nd, 2005

In this tip, we build on our previous lesson by looking at one of the key new OOo 2.0 features related to the DataPilot.

The example table below has four fields, Sex (M/F), pay grade, state of residence , and salary.

dp3_1.jpg

We open the DataPilot dialog as before. For OOo 2.0, a new Page Field is available. We drag the headers into the various DataPilot fields as shown below.

dp3_2.jpg

The new Page field gives us a filter to control the data that is displayed/processed in the DataPilot table.

dp3_3.jpg

Below, we have returned to the DataPilot dialog and swapped the STATE and GRADE fields. We see the different options provided by the filter. A good illustration of the power and flexibility of the updated DataPilot feature in OOo Calc.

dp3_4.jpg

Datapilot Revisited

Sunday, August 21st, 2005

Version 2.0 of OOo Calc will feature significant improvements to the DataPlot functionality. For this reason, we will revisit this powerful feature over the next few days.

First, the basics…

(more…)

Splitting a string into characters

Thursday, August 18th, 2005

Here’s a silly trick to split your string into separate characters across a range of cells.

stringtrick.jpg

You just need to enter the formula in C2 and then copy across the row as shown.

Basic functions: TEXT

Wednesday, August 17th, 2005

At first glance, the TEXT function may not sound very useful. Here are a few simple applications.

In the top example, without the TEXT function, the total is not formatted correctlt (as a currency). However, the TEXT function allows us to apply the correct format to the number component of the string.

In the second example, we see how the TEXT function allows us to reformat existing data - such as dates.

text2.jpg