OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

newbie function button question

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
BabaG
Guest





PostPosted: Wed Dec 31, 2003 10:55 pm    Post subject: newbie function button question Reply with quote

happy new year all! hopefully this is a simple one. i have two
cells i want to perform a simple addition or subtraction function
on. the two cells are simply located one above the other (c3
and c4). i've made a macro/function with the addition/subtraction
routines in it and seem to be able to call it by typing its name,
addition, or alternatively, subtraction, followed by the cell
locations [=subtraction(c3;c4)]. i do this from cell c5. what i'd
prefer, however, is to have two buttons, one for addition and one
for subtraction next to my cells. pressing either would present the
answer in cell c5. well, i got the buttons on the spreadsheet but i
can't figure out how to :

1) assign my functions to them
2) how to tell them where to display the answer (it would always
be displayed in the same cell, c5)

sorry if this is too rudimentary. just got burned out searching
for and reading manuals i don't quite get.

tia,
BabaG
Back to top
dominik
General User
General User


Joined: 04 Dec 2003
Posts: 18
Location: Hagen (Westf.), Germany

PostPosted: Mon Jan 05, 2004 11:48 pm    Post subject: Reply with quote

Hello!

Answer to Question 1:
for the form controls (e.g. your button) you can toggle between design mode on and / off
for how to do that read this:
menu / help / contents / index / search term: "form fields; inserting"
(english version of 680 m17)
when you are in design mode, right click the button / control ... / events (Tab)
-> for example click the [...] button behind "Mouse button released"
-> in the next dialog you can assign a macro to this event.

Was this what you wanted to know?

cYa
Dominik Di Lorenzo
Back to top
View user's profile Send private message
dominik
General User
General User


Joined: 04 Dec 2003
Posts: 18
Location: Hagen (Westf.), Germany

PostPosted: Tue Jan 06, 2004 1:53 am    Post subject: Reply with quote

Hello!

to question 2:
Sub Main
dim doc as object
dim sheet as object
dim cell1, cell2, cell3 as object

doc = ThisComponent
sheet = doc.sheets(0) ' -> the first sheet in the document
cell1 = sheet.getCellByPosition(2,2) ' -> (col, line) -> C3
cell2 = sheet.getCellByPosition(2,3) ' -> (col, line) -> C4
cell3 = sheet.getCellByPosition(2,4) ' -> (col, line) -> C5

cell3.Value = cell1.Value + cell2.Value

End Sub

cYa
Dominik
Back to top
View user's profile Send private message
BabaG
Guest





PostPosted: Tue Jan 06, 2004 6:15 pm    Post subject: newbie function button question Reply with quote

thankyou dominik!

this gets me almost all of the way to where i need to be!
i tried your code and it works wonderfully. i do have one
question remaining, however. my cells are formatted in
such a way that they are string values rather than
numerical.

i notice that you have the cells dimensioned as objects.
how would i tell the macro that the cells should be looked
at as strings? would i leave this:

Sub Main

dim doc as object
dim sheet as object
dim cell1, cell2, cell3 as object

doc = ThisComponent
sheet = doc.sheets(0) ' -> the first sheet in the document
cell1 = sheet.getCellByPosition(2,2) ' -> (col, line) -> C3
cell2 = sheet.getCellByPosition(2,3) ' -> (col, line) -> C4
cell3 = sheet.getCellByPosition(2,4) ' -> (col, line) -> C5

and then use something like str(cell1) to tell the macro
that i'm dealing with a string? i seem to get errors when
i try that.

my cells have characters in them that separate
numerical values. that's why i'm defining the
cell values as strings. the format is 01:02:03:04 and
is a time format. i start by grabbing the numerical
parts, skipping over the colons, convert those parts to
integers, perform my calculations, and finally convert
the result back to a string value with the colons re-
inserted. i've done all of this without a push button
and have figured out how to use mid$ to grab the
parts of my string that i need. when i tried to use this
code with my input format for the cells it returned a
value of 0 though. this leads me to believe that with
cell data that is more complex than simple numeric
data i should bring it into the macro using your "dim
cell1, cell2, cell3 as object" dimensioning and then
somehow convert these object cells to strings so that i
can use my routines to grab the relevant parts.

i have the subroutines to convert the time strings into
integers and back, as well as do the math with the cell
data, and with your kind help i see how to reference
cells specifically from within the macro and assign the
macro to a push button. my last hurdle seems to be in
getting the data to smoothely flow from your code for
retrieving the cell data (which i've gotten to work well
with numeric data), and into my routines for conversion
and calculating and then back again (which uses
string data).

thanks again and i hope this isn't too much. i was both
surprised and very pleased to get your response to my
newbie question after waiting a week through the
holidays. happy new year!

BabaG
Back to top
dominik
General User
General User


Joined: 04 Dec 2003
Posts: 18
Location: Hagen (Westf.), Germany

PostPosted: Tue Jan 06, 2004 11:00 pm    Post subject: Reply with quote

Sorry, I don't have the time to think about your problem
at the moment, but maybe this can help you for now:

cell.Value = 23
cell.String = "Hello"
cell.Formula = "=C4+C3"

btw: have you read the Sun StarOffice7 Basic Programmers Guide?
you can get it here:
http://docs.sun.com/db/coll/so7en?q=star
Back to top
View user's profile Send private message
BabaG
Guest





PostPosted: Tue Jan 06, 2004 11:51 pm    Post subject: newbie function button question Reply with quote

thanks dominik,

at my level of newbieness even that is a help. and yes
i have looked at the documentation you have linked. i
was able to understand some of what was in it but it
seemed generic enough in its examples that i was mostly
stumped by it. for example, when i'd try pasting one of
their examples directly into a simple macro as a test it
produced errors. i think they were assuming i'd know to
include definitions of variables or some such thing
before i pasted in the example. of course i don't have the
programming experience to know to do that (or how).

i'm banking on cell.String = "Hello" to be a tip i can
pursue to get me in and out of my conversion routines.

thanks again for your willingness to help,
BabaG
Back to top
BabaG
Guest





PostPosted: Wed Jan 07, 2004 4:40 pm    Post subject: newbie function button question Reply with quote

dominik,

just a quick followup to thanks you again and let you know that
cell.String was in fact what i needed to know. i have my function
running properly now. i'm sure the code is inelegant but it does
what i was trying to do.

thanks again!
BabaG
Back to top
dominik
General User
General User


Joined: 04 Dec 2003
Posts: 18
Location: Hagen (Westf.), Germany

PostPosted: Wed Jan 07, 2004 11:02 pm    Post subject: Reply with quote

Hi!
If you think your macro could be useful for other people
you can perhaps post it to www.ooomacros.org.

cYa,
Dominik
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group