Sunday, May 5, 2013

A Calc Example for labeling columns

Labeling Columns macro examples

An Excerpt From my book OpenOffice Calc Solutions CookBook

Sometimes, if you are building a large spreadsheet system that you may want to duplicate the setup over and over again. It's worth the time to have a macro routine create and name sheets and label areas of the individual sheets.

I actually documented this coding example using three different methods. The method below will increment through a series of column positions using the .getCellByPosition. In my book I  demonstrate two other ways to label the columns using the .getCellRangeByName command.

Labeling Columns Examples

Method 1 Accessing Cells with  .getCellByPosition

The subroutine below demonstrates how a macro can access a sheet called “Sheet6” and pre-label a set of cells for the columns “D” through “G” on row 2. To run this code, edit this code into a module. Open a spreadsheet and make sure you actually have a Sheet6 or modify the code for a sheet name that already exists. NOTE: Sheet names are case sensitive. From the spreadsheet click on tools-run macros and from the module tree and your macro lists pop up. Select the macro. 

sub SheetSetupTest1
   'first create variable as an array of label strings
    Labels=array("","Product Name","Model Number","Unit Cost","Time to Install")
    dSprdSht=ThisComponent 'call the already opened spreadsheet
    CurrentSht=dSprdSht.getSheets().getByName( "Sheet6" ) ' Access calc sheet by its name
    'loop thru and label the cells 
    for x=1 to 4
       'place labels on columns D2 through G2
       oCell=CurrentSht.getCellByPosition( x+2, 1 )
       oCell.setString(Labels(x))
   next x
end sub

Here is the result:

 You'll notice that cell spacing is too good, but that can be fixed by keeping an array of cell sizes, using fixed size fonts, or a couple of other techniques.

Method 2 Accessing Cells with  .getCellRangeByName

Another method is to use two arrays. One array for the labels and one for the column letters.
This method uses the .getCellRangeByName command

sub SheetSetupTest2
   'create variable as an array of label strings
   Labels=array("","Product Name","Model Number","Unit Cost","Time to Install")
   'create variable as an array of column names
   Columns=array("","D","E","F","G”)
   'loop thru and label column heading cells
   for x=1 to 4
      oCell=CurrentSht.getCellRangeByName(Columns(x) & "2")
      oCell.setString(Labels(x))
   next x
end sub

Method 3 Incrementing the Column name string

The third method uses a custom function I created and describe in my book. The function
is called nxtColLtr (ColumnString)   The next-column-letter function takes a column letter or
letters as an argument and returns the next column letter(s) value.  The advantage to this method is you just need to specify a starting column value. This is handy if you have to shift the labels a few times in your design. The nxtColLtr function was tested to increment from column "A" through column "AMJ".

nxtColLtr(columnString)  function Examples:
nxtColLtr("A")  returns "B"
nxtColLtr("Z")  returns "AA"
nxtColLtr("AZ") returns "BA"

Here is the example using the nxtColLtr function

sub SheetSetupTest3
   'create variable as an array of label strings 
   Labels=array("","Product Name","Model Number","Unit Cost","Time to Install")
   'Set StartCol variable for first column heading to label
   StartCol="D"
   for x=1 to 4
      oCell=CurrentSht.getCellRangeByName(StartCol & "2")
      oCell.setString(Labels(x))
      StartCol=nxtColLtr(StartCol)   'get the next column name string
   next x
end sub


Solution Books Table of Contents

- Just to give you an idea of what kinds of solutions and design topics I've explained and documented in my Solution Books, The Table of Contents for each book is listed below:

OpenOffice Calc Macro Solutions Book

OpenOffice Writer Macro Solutions Book

OpenOffice Draw Macro Solutions Book

Appendix A talks about dialog components and is a common appendix to all the books

My OpenOffice CookBooks
How did the series come about?
    This OpenOffice Calc Macro CookBook 
What is Calc
Cell functions in Calc versus Excel
Calc API calls you need to know about
Macro code to access calc files
ThisComponent method
Creating a New Calc document method
Opening an Existing Calc document method
Accessing sheets and Cells
Accessing Cells
Examples of Access Cells by CellName or by Column/Row
Labeling Columns Example - By .getCellByPosition
Labeling Columns Example - By .getCellRangeByName
Labeling Columns Example – Adjusting Column Widths
Labels with Fixed width Font
Hiding Columns
Incrementing column names – nxtcolLtr function
Dynamic Test Indicators in Calc
A Calc Cell Style Workaround
Custom Cell Functions
Findnth (item within a) cell function
Personhours cell function
Array or cell reference?
Converting values with labels to standard Units
StatRollUp Cell Function
The formulaLocal Property – accessing the cell range string within the cell function call
AbsoluteName = changing functionality by cell function's location
Writing Calc Subroutines with Dialogs
A Sheet Group Navigator
Sheet Navigator Macro Example
Our first Dialog
Openoffice Bug or Feature
Custom Search Routines
Client Search
ClientSearch dialog and code
Access to Dialog and calc sheet
Basic Search Loop
Aligned columns in a listbox
Adding wildcards to the Search
Birthday Search
Access to Dialog and calc sheet
Search Setup and Preload
BirthDate Search Loop
Adding a search option
Using desktop functions to cut and paste sheets
dispatchURL
CopySheetToNewDoc
The Classic MS Office Example – an interactive two listbox Dialog
Finding an online solution and an API bug
Loading ListBox1 (AddItem and AddItems methods)
The => CommandButton(getSelectedItemPos method)
The <= CommandButton
Iterating Items within a ListBox
Appendix A - Dialog Controls Overview

Appendix B – Cell Formatting Fonts, Bolding, and Color Effects
Using the Debugger to find cell values
Left, Right, and Center Justify Cell text:
Setting Cell Font
Cell Font Height
Setting Font Color
Setting Background Color
Bolding Cell Text
Setting Line Wrapping
Underlining Cell Text
Two Underlining methods
Table Borders

Appendix C – Client Search and Birthday Search Tools
Client search Dialog box
Birthday search Dialog box


Back to top of Page


My OpenOffice CookBooks
How did the series come about?
This OpenOffifce Writer Macro CookBook 
Macro code to access writer files
ThisComponent method
Creating a Writer document method
Opening an Existing Write document method
Writer Document's collection paradigm
writer Tables - Mailing Labels Macro
Enumeration objects
Manipulating Documents with Cursors
Using text cursors
MoveCursorToText Example
MoveCursorToText
Automating Documentation
Module Mailer
LoadMailerOptionsDialog
Interactive Dialog Control routines
Basic MoveClient2MailList operation
Accessing Tables within Writer
Globals and main routine setup
Testing for Paragraphs and Tables
Retrieving Table Cell strings
Macro Book Construction
OpenOffice API calls to build writer documents
Using text cursors
BuildNewDoc code
LoadTempltFile
LoadContentAnHdrs
WriteContentLines
InsertingTableOfContent
Working with Frames – The Writer portion of the two listbox label making example
SOAP Example Extended
Dialog SoapSetup
Module Create Soaps
CopySheetToNewDoc(TempltType as Integer)

Appendix A - Dialog Controls Overview

Appendix B – Contract Automation Example and Code 59
Scrubbed Contract Document 61


Back to top of Page
My OpenOffice CookBooks 4
How did the series come about? 4
This OpenOffice Draw Macro CookBook 5
What is OpenOffice Draw 5
Vector versus Bitmap drawing systems 6
Draw and Impress Applications 6
Macro code to access draw files 6
ThisComponent method 7
Creating a New Draw document method 7
Opening an Existing Draw document method 7
Drawpages and Masterpages 8
DrawPages linked to MasterPage 8
Pages can have the same name 8
Draw Macro Basics 9
Portrait orientation and dimensions 9
Basic draw macro Routines 10
Adding Shapes to a page 10
Creating shapes routine 11
Erasing Shapes from the drawpage 15
Drawing Metadata 16
ConnectorShapes 16
Scanning a drawing for Metadata 18
Which Gluepoint? 21
How About Drawing Objects with Property Supports 23
Object Description Field 2
Appendix A - Dialog Controls Overview 28

Back to top of Page
 
X-Ray or Debugger
Setting the Date in DateField
DateField Options
NumericField Control
CurrencyField Control
TimeField Control
ListBox Control
Aligned Columns in a ListBox
The Global Dialog reference
ComboBox Control
CheckBoxes and Option Controls
OptionButton Control
CheckBox Control
PatternField Control
ImageControl Controls
FixedLine Controls
ScrollBar Controls
ListBox and value list storage decisions
Dynamic ListBox item loading
Back to top of Page