[Calc] Function to compute average of a range

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
axljerry123
Posts: 2
Joined: Tue Apr 18, 2017 2:16 pm

[Calc] Function to compute average of a range

Post by axljerry123 »

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
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Can somebody help me ? :( :(

Post by RoryOF »

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.15 on Xubuntu 22.04.4 LTS
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Can somebody help me ? :( :(

Post by Zizi64 »

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; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Post Reply