Professional Documents
Culture Documents
File : calcUtils.i
Purpose : Procedures for use with Open Office Calc (spreadsheet)
programming
Syntax : { appl/utils/oo-calcutils.i }
***************************************************************************/
ASSIGN
chOpenOffice = ?
chWorkBook = ?
chDesktop = ?
chWorkSheet = ?.
/****************************************************************************/
/***** Function Definitions ***********************************************/
/****************************************************************************/
/****************************************************************************/
FUNCTION col_letter RETURNS CHARACTER
(INPUT ip_Col AS INT):
/* Purpose: Returns the column letter for the column number */
/****************************************************************************/
/****************************************************************************/
FUNCTION col_number RETURNS INTEGER
( INPUT ip_ColLetter AS CHARACTER ):
/* Purpose: returns the column number for the column letter passed int */
/****************************************************************************/
/* upper case */
ASSIGN ip_ColLetter = CAPS(ip_ColLetter).
DO i = 1 TO LENGTH(ip_ColLetter) - 1 :
ASSIGN
intCurr = ASC(SUBSTR(ip_ColLetter, i, 1)) - 64.
intReturn = intReturn + (intCurr * 26).
END.
RETURN intReturn.
END FUNCTION. /* col_number */
/****************************************************************************/
/***** Procedure Definitions **********************************************/
/****************************************************************************/
/*******************************************************************/
PROCEDURE align_cell:
/* Purpose: Align the cell specified. */
/*******************************************************************/
DEF INPUT PARAM ip_Col AS INT NO-UNDO. /* Column
Number */
DEF INPUT PARAM ip_Row AS INT NO-UNDO. /* Row Number
*/
DEF INPUT PARAM ip_Horizontal AS CHAR NO-UNDO. /* Values:
BLOCK, CENTER, LEFT, REPEAT, RIGHT, STANDARD */
DEF INPUT PARAM ip_Vertical AS CHAR NO-UNDO. /* Values:
BOTTOM, CENTER, STANDARD, TOP */
DEF INPUT PARAM ip_Orientation AS CHAR NO-UNDO. /* Values:
BOTTOMTOP, STANDARD, STACKED, TOPBOTTOM */
/*******************************************************************/
PROCEDURE align_cell_range:
/* Purpose: Align the row specified. */
/*******************************************************************/
DEF INPUT PARAM ip_Range AS CHAR NO-UNDO. /* eg B2:D4 */
DEF INPUT PARAM ip_Horizontal AS CHAR NO-UNDO. /* Values:
BLOCK, CENTER, LEFT, REPEAT, RIGHT, STANDARD */
DEF INPUT PARAM ip_Vertical AS CHAR NO-UNDO. /* Values:
BOTTOM, CENTER, STANDARD, TOP */
DEF INPUT PARAM ip_Orientation AS CHAR NO-UNDO. /* Values:
BOTTOMTOP, STANDARD, STACKED, TOPBOTTOM */
/*******************************************************************/
PROCEDURE autofit_column:
/* Purpose: Use the Calc OptimalWidth attribute to set widths */
/* for columns. */
/* Parameters: Pass in as Column# */
/*******************************************************************/
DEF INPUT PARAM ip_Col AS INT NO-UNDO. /* column number */
chWorkSheet:COLUMNS(ip_Col):OptimalWidth = TRUE.
/*******************************************************************/
PROCEDURE autofit_row:
/* Purpose: Use the Calc OptimalHeight attribute to set height */
/* for row */
/* Parameters: Pass in as Row# */
/*******************************************************************/
DEF INPUT PARAM ip_Row AS INT NO-UNDO. /* row number */
chWorkSheet:Rows(ip_Row):OptimalHeight = TRUE.
/****************************************************************************/
PROCEDURE CleanUp:
/* Purpose: cleans up the com-handles */
/****************************************************************************/
ASSIGN
chOpenOffice = ?
chWorkBook = ?
chDesktop = ?
chWorkSheet = ?.
/*******************************************************************/
PROCEDURE close_Calc:
/* Purpose: Close the Calc program. */
/*******************************************************************/
/*******************************************************************/
PROCEDURE col_function:
/* Purpose: Provides a utility to create a simple "column" */
/* function */
/* Valid functions are: */
/* SUM: sums all numerical values */
/* COUNT: total # of all values (including chars) */
/* COUNTNUMS: total number of all numerical cells */
/* AVERAGE: average of all numerical cells */
/* MAX: largest numerical value */
/* MIN: smallest numerical value */
/* PRODUCT: product of all numerical values */
/* STDEV: standard deviation */
/* VAR: variance */
/* STDEVP: standard devt'n based on total population */
/* VARP: varianced based on total population */
/*******************************************************************/
DEF INPUT PARAM ip_Col AS INT NO-UNDO. /* Column Number */
DEF INPUT PARAM ip_Row AS INT NO-UNDO. /* Row Number */
DEF INPUT PARAM ip_StartRow AS INT NO-UNDO.
DEF INPUT PARAM ip_EndRow AS INT NO-UNDO.
DEF INPUT PARAM ip_Function AS CHAR NO-UNDO.
DEF VAR chrFormula AS CHAR NO-UNDO.
chCell:SetFormula(chrFormula).
/*******************************************************************/
PROCEDURE delete_col:
/* Purpose: Deletes columns below the passed in col # */
/*******************************************************************/
DEF INPUT PARAM ip_Col AS INT NO-UNDO.
DEF INPUT PARAM ip_NumCols AS INT NO-UNDO.
/* delete columns */
chWorkSheet:Columns:DeleteByIndex(ip_Col, ip_NumCols).
/*******************************************************************/
PROCEDURE delete_row:
/* Purpose: Deletes rows below the passed in row # */
/*******************************************************************/
DEF INPUT PARAM ip_Row AS INT NO-UNDO.
DEF INPUT PARAM ip_NumRows AS INT NO-UNDO.
/* delete rows */
chWorkSheet:Rows:DeleteByIndex(ip_Row, ip_NumRows).
/*******************************************************************/
PROCEDURE freeze_panes:
/* Purpose: freezes panes with the specified number of columns */
/* and rows. To freeze only horizontally, specify */
/* ip_Row as 0. To freeze only vertically, specify */
/* ip_Col as 0. */
/*******************************************************************/
DEF INPUT PARAM ip_Col AS INT NO-UNDO. /* Column Number */
DEF INPUT PARAM ip_Row AS INT NO-UNDO. /* Row Number */
chWorkBook:getCurrentController():freezeAtPosition(ip_Col, ip_Row).
/****************************************************************************/
PROCEDURE format_cell:
/* Purpose: Formats a cell with a specific format. */
/****************************************************************************/
DEF INPUT PARAM ip_Col AS INT NO-UNDO.
DEF INPUT PARAM ip_Row AS INT NO-UNDO.
DEF INPUT PARAM ip_DataType AS CHAR NO-UNDO.
DEF INPUT PARAM ip_Decimals AS INT NO-UNDO.
CASE ip_DataType:
WHEN "Decimal" THEN chrNumberFormatString = "###,###,###,##0." + FILL("0",
ip_Decimals).
OTHERWISE chrNumberFormatString = "".
END CASE. /* ip_DataType */
intNumberFormatId = chNumberFormats:queryKey(chrNumberFormatString,
chLocalSettings, TRUE).
*/
/* And get the value */
ASSIGN
chCell:NumberFormat = 8. /*intNumberFormatId. */
RETURN.
/*******************************************************************/
PROCEDURE get_cell_data:
/* Purpose: Gets the cell data (char) from a worksheet */
/*******************************************************************/
DEF INPUT PARAM ip_Col AS INT NO-UNDO. /* Column Number */
DEF INPUT PARAM ip_Row AS INT NO-UNDO. /* Row Number */
DEF OUTPUT PARAM op_CellData AS CHAR NO-UNDO.
/*******************************************************************/
PROCEDURE get_last_row:
/* Purpose: Returns the last row as an integer. */
/*******************************************************************/
DEF INPUT PARAM ip_Col AS INT NO-UNDO.
DEF OUTPUT PARAM op_LastRow AS INT NO-UNDO.
/*******************************************************************/
PROCEDURE insert_col:
/* Purpose: Inserts columns below the passed in col # */
/*******************************************************************/
DEF INPUT PARAM ip_Col AS INT NO-UNDO.
DEF INPUT PARAM ip_NumCols AS INT NO-UNDO.
/* Insert columns */
chWorkSheet:Columns:InsertByIndex(ip_Col, ip_NumCols).
/*******************************************************************/
PROCEDURE insert_row:
/* Purpose: Inserts rows below the passed in row # */
/*******************************************************************/
DEF INPUT PARAM ip_Row AS INT NO-UNDO.
DEF INPUT PARAM ip_NumRows AS INT NO-UNDO.
chWorksheet:rows:insertByIndex(ip_Row, ip_NumRows).
/****************************************************************************/
PROCEDURE Maximize_Window:
/* Purpose: Maximizes the OO screen. */
/****************************************************************************/
DEF VAR chFrame AS COM-HANDLE NO-UNDO.
DEF VAR chWindow AS COM-HANDLE NO-UNDO.
DEF VAR chRect AS COM-HANDLE NO-UNDO.
chFrame = chDesktop:getCurrentFrame().
chWindow = chFrame:getContainerWindow().
chRect = chWindow:setPosSize(1,1,800,600,15).
RETURN.
/****************************************************************************/
PROCEDURE Minimize_Window:
/* Purpose: Minimizes the OO screen. */
/****************************************************************************/
DEF VAR chFrame AS COM-HANDLE NO-UNDO.
DEF VAR chWindow AS COM-HANDLE NO-UNDO.
DEF VAR chRect AS COM-HANDLE NO-UNDO.
chFrame = chDesktop:getCurrentFrame().
chWindow = chFrame:getContainerWindow().
chRect = chWindow:setPosSize(1,1,1,1,15).
RETURN.
/*******************************************************************/
PROCEDURE new_book:
/* Purpose: Open a new "BOOK" in Calc and create a worksheet. */
/*******************************************************************/
/*******************************************************************/
PROCEDURE open_book:
/* Purpose: open a "BOOK" in Calc. */
/*******************************************************************/
DEF INPUT PARAM ip_FileName AS CHAR NO-UNDO. /* spreadsheet name */
ASSIGN
ip_FileName = "file:///" + TRIM(ip_FileName)
ip_FileName = REPLACE(ip_FileName, "\", "/").
/*******************************************************************/
PROCEDURE open_calc:
/* Purpose: Start OpenOffice & open a DDE conversation with */
/* the Calc System topic */
/*******************************************************************/
/*******************************************************************/
PROCEDURE page_break:
/* Purpose: Inserts a manual page break for reporting */
/*******************************************************************/
DEF INPUT PARAM ip_Row AS INT NO-UNDO.
/*******************************************************************/
PROCEDURE row_function:
/* Purpose: Provides a utility to create a simple "row" */
/* function */
/* SUM: sums all numerical values */
/* COUNT: total # of all values (including chars) */
/* COUNTNUMS: total number of all numerical cells */
/* AVERAGE: average of all numerical cells */
/* MAX: largest numerical value */
/* MIN: smallest numerical value */
/* PRODUCT: product of all numerical values */
/* STDEV: standard deviation */
/* VAR: variance */
/* STDEVP: standard devt'n based on total population */
/* VARP: varianced based on total population */
/*******************************************************************/
DEF INPUT PARAM ip_Col AS INT NO-UNDO. /* Column Number */
DEF INPUT PARAM ip_Row AS INT NO-UNDO. /* Row Number */
DEF INPUT PARAM ip_StartCol AS INT NO-UNDO.
DEF INPUT PARAM ip_EndCol AS INT NO-UNDO.
DEF INPUT PARAM ip_Function AS CHAR NO-UNDO.
DEF VAR chrFormula AS CHAR NO-UNDO.
chCell:SetFormula(chrFormula).
/*******************************************************************/
PROCEDURE save_book:
/* Purpose: Save a "BOOK" in Calc */
/*******************************************************************/
DEF INPUT PARAM ip_OutputPath AS CHAR NO-UNDO. /* directory path */
DEF INPUT PARAM ip_FileName AS CHAR NO-UNDO. /* spreadsheet name */
ASSIGN
chrFileName = "file:///" + ip_OutputPath + ip_FileName + ".sxc"
chrFileName = chrFileName + "\"
chrFileName = REPLACE(chrFileName, "\\", "\")
chrFileName = REPLACE(chrFileName, "/\", "\")
chrFileName = REPLACE(chrFileName, "\", "/").
chWorkBook:storeAsURL(chrFileName, cc).
/*******************************************************************/
PROCEDURE set_col_width:
/* Purpose: Set the width of a column. */
/*******************************************************************/
DEF INPUT PARAM ip_Col AS INT NO-UNDO.
DEF INPUT PARAM ip_ColWidth AS DEC NO-UNDO.
/*******************************************************************/
PROCEDURE set_font_style:
/* Purpose: Set font style for a cell */
/* Parameters: Row # */
/* Column # */
/* Font Name */
/* Size (points) */
/* Bold (TRUE/FALSE) */
/* Underline (See below) */
/*******************************************************************/
DEF INPUT PARAM ip_Col AS INT NO-UNDO. /* Column Number */
DEF INPUT PARAM ip_Row AS INT NO-UNDO. /* Row Number */
DEF INPUT PARAM ip_Font AS CHAR NO-UNDO. /* Font Name */
DEF INPUT PARAM ip_Size AS INT NO-UNDO. /* Point Size */
DEF INPUT PARAM ip_Bold AS LOG NO-UNDO. /* Bold (weight = 150
for bold, 100 for normal) */
DEF INPUT PARAM ip_Underline AS INT NO-UNDO. /* NONE = 0, SINGLE =1,
DOUBLE=2, DOTTED = 3
DONTKNOW=4, DASH=5,
LONGDASH=6, DASHDOT=7,
DASHDOTDOT=8,
SMALLWAVE=9, WAVE =10, DOUBLEWAVE=11,
BOLD=12,
BOLDDOTTED=13, BOLDLONGDASH= 14,
BOLDDASHDOT=15,
BOLDDASHDOTDOT=16, BOLDWAVE = 17 */
/*******************************************************************/
PROCEDURE set_footer:
/* Purpose: Creates a footer for the document */
/*******************************************************************/
DEF INPUT PARAM ip_Text AS CHAR NO-UNDO.
/* first we need to get the default page style for this workbook/sheet */
chStyleFamilies = chWorkBook:StyleFamilies.
chPageStyles = chStyleFamilies:getByName("PageStyles").
chDefaultPage = chPageStyles:getByName("Default").
/* Turn on Footers */
chDefaultPage:FooterIsOn = TRUE.
/* Same Footer for both left (even) & right (odd) pages */
chDefaultPage:FooterIsShared = TRUE.
/* Clean up now */
IF chStyleFamilies <> ? THEN DO:
RELEASE OBJECT chStyleFamilies.
ASSIGN chStyleFamilies = ?.
END.
/*******************************************************************/
PROCEDURE set_header:
/* Purpose: Creates a header for the document */
/*******************************************************************/
DEF INPUT PARAM ip_Text AS CHAR NO-UNDO.
/* first we need to get the default page style for this workbook/sheet */
chStyleFamilies = chWorkBook:StyleFamilies.
chPageStyles = chStyleFamilies:getByName("PageStyles").
chDefaultPage = chPageStyles:getByName("Default").
/* Turn on headers */
chDefaultPage:HeaderIsOn = TRUE.
/* Same header for both left (even) & right (odd) pages */
chDefaultPage:HeaderIsShared = TRUE.
/* Set up the header */
chHeaderContent = chDefaultPage:RightPageHeaderContent.
chHeaderText = chHeaderContent:CenterText.
chHeaderText:STRING = ip_Text.
chDefaultPage:RightPageHeaderContent = chHeaderContent.
/* Clean up now */
IF chStyleFamilies <> ? THEN DO:
RELEASE OBJECT chStyleFamilies.
ASSIGN chStyleFamilies = ?.
END.
/*******************************************************************/
PROCEDURE set_row_height:
/* Purpose: Set the height of a row. */
/* Parameters: ip_Row row # */
/* ip_RowHeight in inches 1/4 = 0.25 passed in */
/*******************************************************************/
DEF INPUT PARAM ip_Row AS INT NO-UNDO.
DEF INPUT PARAM ip_RowHeight AS DEC NO-UNDO.
/*******************************************************************/
PROCEDURE show_col:
/* Purpose: Shows or hides a column */
/*******************************************************************/
DEF INPUT PARAM ip_Col AS INT NO-UNDO.
DEF INPUT PARAM ip_Show AS LOG NO-UNDO. /* TRUE = visible, FALSE =
hidden */
/*******************************************************************/
PROCEDURE show_row:
/* Purpose: Shows or hides a row */
/*******************************************************************/
DEF INPUT PARAM ip_Row AS INT NO-UNDO.
DEF INPUT PARAM ip_Show AS LOG NO-UNDO. /* TRUE = visible, FALSE =
hidden */
/*******************************************************************/
PROCEDURE write_cell_data:
/* Purpose: Write data into a worksheet. */
/*******************************************************************/
DEF INPUT PARAM ip_Col AS INT NO-UNDO. /* Column Number */
DEF INPUT PARAM ip_Row AS INT NO-UNDO. /* Row Number */
DEF INPUT PARAM ip_Data AS CHAR NO-UNDO.
DEF VAR decTest AS DEC NO-UNDO.
/*******************************************************************/
PROCEDURE write_col_hdr:
/* Purpose: Write row 1 column headers in a worksheet. */
/*******************************************************************/
DEF INPUT PARAM ip_Col AS INT NO-UNDO.
DEF INPUT PARAM ip_Label AS CHAR NO-UNDO.