[Solved] Textbox calculations in Writer template

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
streub
Posts: 128
Joined: Thu Jan 02, 2014 2:48 am
Location: Wisconsin

[Solved] Textbox calculations in Writer template

Post by streub »

I am using Writer for a price tag template.

I have three textboxes involved in the calculation.

Textbox "PCT" will display the value of the completed calculation.

Textbox "RET" is the retail price of an item. The user enters this information.

Textbox "DIS" is the discounted price of an item. The user enters this information.

The calculation is as follows:

PCT = RET - DIS / RET

I have looked throughout the forum but have not located any help with this.

Anybody willing to offer some advice or direction?

I am at ground zero with Basic and UNO.

Thank you

Streub
Last edited by streub on Wed Aug 30, 2017 8:03 pm, edited 1 time in total.
Streub

The more I learn from this forum, the more I get excited about using this strange application
User avatar
keme
Volunteer
Posts: 3699
Joined: Wed Nov 28, 2007 10:27 am
Location: Egersund, Norway

Re: textbox calculations in Writer template

Post by keme »

Does it have to be a textbox, and/or controlled by a macro?

Tables in Writer have simple calculation abilities. You can do it in one table with 3 cells, or in 3 separate 1x1 "tables".

See attached file. If you need to position the tables freely, you can have them inside separate frames.
Attachments
tablecalculations.odt
(9.38 KiB) Downloaded 203 times
streub
Posts: 128
Joined: Thu Jan 02, 2014 2:48 am
Location: Wisconsin

Re: textbox calculations in Writer template

Post by streub »

The use of tables is an excellent suggestion. I will attempt to use as you had suggested and will
inform you of the results.

Thank you
Streub

The more I learn from this forum, the more I get excited about using this strange application
streub
Posts: 128
Joined: Thu Jan 02, 2014 2:48 am
Location: Wisconsin

Re: textbox calculations in Writer template

Post by streub »

I did try your suggestion of using frames and tables. It performed as needed; however, this method will not allow me to set a tab order as a textbox object can. Good idea.

Thank you
Streub

The more I learn from this forum, the more I get excited about using this strange application
streub
Posts: 128
Joined: Thu Jan 02, 2014 2:48 am
Location: Wisconsin

Re: textbox calculations iand textbox format

Post by streub »

The code below will calculate the vales of two textboxes and enter the results into a third.
The textbox with the result needs to be formatted to a percentage via macro. I cannot recall
how to do that.

Code: Select all

REM  *****  BASIC  *****


Sub oPct

oDoc = ThisComponent
oDocView = oDoc.getCurrentController()

	oForm = oDoc.drawpage.forms(0)
		tbp = oForm.getbyname("tbp")
		tbr = oForm.getbyname("tbr")
		tbd = oForm.getbyname("tbd")
	
		tbp.text  = (tbr.value - tbd.value) / tbr.value 
		
		
	
	



End Sub



Streub

The more I learn from this forum, the more I get excited about using this strange application
User avatar
robleyd
Moderator
Posts: 5055
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: textbox calculations in Writer template

Post by robleyd »

Alternatively you could create a string in percentage format - something like

Code: Select all

tbp.text  = ((tbr.value - tbd.value) / tbr.value) * 100 & "%"
Note: I don't use macros myself, so syntax for the concatenation operator may be wrong :)
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.1.2; SlackBuild for 24.2.1 by Eric Hameleers
Post Reply