[Calc] Function to compute average of a range

Creating a macro - Writing a Script - Using the API

[Calc] Function to compute average of a range

Postby axljerry123 » Tue Apr 18, 2017 2:23 pm

Can somebody help me ? :( :(
The following is the question:

Function ComputeAverage(firstRow As Long, lastRow As Long) As Double
The aim of this function is to calculate the average of a section of data in Column A
For example, a call of ComputeAverage(4,10) in your OO Basic program should return the average of the cells from A4 to A10
You can make use of the constant DATA_COL (=1) in this function to refer to Column A
The first input firstRow is the row number of the starting row
The second input lastRow is the row number of the last row

Title Edited. A descriptive title for posts helps others who are searching for solutions and increases the chances of a reply (Hagar, Moderator).
Last edited by axljerry123 on Tue Apr 18, 2017 3:12 pm, edited 1 time in total.
OpenOffice 4.1.3
Posts: 2
Joined: Tue Apr 18, 2017 2:16 pm

Re: Can somebody help me ? :( :(

Postby RoryOF » Tue Apr 18, 2017 2:48 pm

On Forum policy is not to do students' homework for them, so any answers you receive will be predicated on that.

Start by telling us how you would average a list of data points using pen and paper, and someone may guide you how approach this using a spreadsheet.
Apache OpenOffice 4.1.3 on Xubuntu 16.04 (mostly 64 bit version) and infrequently on Win2K/XP
14 October 2016 was Pooh's 90th birthday
User avatar
Posts: 24165
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Can somebody help me ? :( :(

Postby Zizi64 » Tue Apr 18, 2017 3:16 pm

Do you know anything about the API functions? (You must use them to control the calc doc/sheet/cells).

You must get the active Calc document (Thiscomponent), and then you must get the desired Sheet (by number, by name, or the actually displayed (Current selection...) Then you can get the cells by coordinates and the numeric values of the cells. Summarize them, and divide the result by the number of the rows (/lastrow-firstrow)

And what about the empty cells? They will represent a zero value, and it increases the divider. Do you need use them in the average value?
Tibor Kovacs, Hungary; LibreOffice4.4.7 - Portable versions: LO3.3.0-LO5.3.3 and AOO4.1.3; on Win7x64Prof.
Please, edit the topic's initial post, and add the word "[Solved]" at the beginning of the subject line - if your problem has been solved.
User avatar
Posts: 5124
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Return to Macros and UNO API

Who is online

Users browsing this forum: Google [Bot] and 8 guests