[Solved] Getting Formatted Cell value

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Dave The Hat
Posts: 2
Joined: Sat Jan 04, 2020 11:55 am

[Solved] Getting Formatted Cell value

Post by Dave The Hat »

Hi. I am trying to write a macro to convert values in a range of cells into a format suitable for pasting into a forum. I am having problems getting the formatted value of the cell. For example the cell value is 0.1293847273723, but in the spreadsheet is it displayed as 12.94%. In the macro I want it to use the 12.94% formatted value. How do I get the formatted value. Here is the code as it stands:

Code: Select all

Function HTMLTableRow (rSource01 as range, Optional rSource02 as range)

	Dim i as Integer
	Dim sReturn as string
	
	sReturn = "[tr]"
	
	for i = 1 to uBound(rSource01, 2)
		sReturn = sReturn + HTMLTableCell(rSource01(1, i))
	next i
	if not IsMissing(rSource02) then
		for i = 1 to uBound(rSource02, 2)
			sReturn = sReturn + HTMLTableCell(rSource02(1, i))
		next i
	end if
	
	sReturn = sReturn + "[/tr]"

	HTMLTableRow = sReturn

End Function

Function HTMLTableCell (sValue, Optional bBold as Boolean, Optional bItalic as Boolean)

	if IsMissing(bBold) then
		bBold = false
	end if

	if IsMissing(bItalic) then
		bItalic = false
	end if

	HTMLTableCell = "[td]"
	if bBold then
		HTMLTableCell = HTMLTableCell + "[b]"
	end if
	if bItalic then
		HTMLTableCell = HTMLTableCell + "[i]"
	end if
	HTMLTableCell = HTMLTableCell + sValue
	if bItalic then
		HTMLTableCell = HTMLTableCell + "[/i]"
	end if
	if bBold then
		HTMLTableCell = HTMLTableCell + "[/b]"
	end if
	HTMLTableCell = HTMLTableCell + "[/td]"

End Function
I am guessing I have to change the "HTMLTableCell(rSource01(1, i))" part of the code. It would also be nice to know how to tell if the cell is bold or italic.

I apologise if this has been previously answered, but my google searching has failed to find the answer.
Last edited by RoryOF on Mon Jan 06, 2020 1:31 pm, edited 2 times in total.
Reason: Added green tick [RoryOF, Moderator]
Open Office 4.1.7 Mac OS 10.15.2
User avatar
Zizi64
Volunteer
Posts: 11360
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Getting Formatted Cell value

Post by Zizi64 »

It would also be nice to know how to tell if the cell is bold or italic.
It is better to use the Cell Styles instead of the direct formatting method.You can get/set the applied style of a cell by a very simple method in your macro code:

viewtopic.php?t=43351
viewtopic.php?t=48960
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.
User avatar
Zizi64
Volunteer
Posts: 11360
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Getting Formatted Cell value

Post by Zizi64 »

Otherwise, the "formatted cell value" is a string, but not a number.
Every cell can store 3 different contents in SAME TIME:
- a Numeric value: Double precision floating number (8 byte wide)
- a String (converted from the double precision floating type numeric value to textual format: "1.23456" - yes, it is a text with seven characters - ; or a pure textual content, like the: "Hello World!")
- a Formula (and/or the FormulaLocal for the localised displaying)

You can get the textual content of a cell by the command

Code: Select all

Dim sCelltext as string
Dim oCell as object

...
...

sCellText = oCell.String
It gives you the string content - without the visual formatting (bold/italic/color... etc) properties.
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.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Getting Formatted Cell value

Post by Villeroy »

[Calc, Basic] Introspective cell functions
Return style name of this row's cell in column #1: =CELL_STYLE(SHEET();ROW();1)
Apply a cell style to a numeric value with return value 0: =A1 +STYLE("MyStyle")
Apply a cell style to a text value eliminating the return value 0: =A1 &T(STYLE("MyStyle"))

Combined: =IF(ISNUMBER(A1) ; A1 +STYLE(CELL_STYLE(SHEET();ROW();1)) ; A1 &T(CELL_STYLE(SHEET();ROW();1))
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Lupp
Volunteer
Posts: 3552
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Getting Formatted Cell value

Post by Lupp »

Did you consider to use what I offered in viewtopic.php?f=21&t=85152/ (enhanced version viewtopic.php?f=21&t=85152/#p432302/ with control parameter "bisulp")?
It should provide what you want. Concerning textual content you also get attributed differently formatted TextPortions.
QA simple help is included with the code in REM lines (making th code longer, of course).

The inconvenience concerning the way the cell to work on is passed to the function: Using LibreOffice this could be done much simpler placing the function into a module with Option VBAsupport 1 enabled. A single SheetCell or SheetCellRange on a parameter position would then not only pass data, but also information about the source range itself.

By the way: In most cases it will be simpler to first make a new sheet containing the parts for export using Ctrl+C and Ctrl+Shift+P and then to export it in html format by board means of OpenOffice. (This is also easily feasible with a script - probably just needing a recorded macro.)
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
Dave The Hat
Posts: 2
Joined: Sat Jan 04, 2020 11:55 am

Re: Getting Formatted Cell value

Post by Dave The Hat »

That's it Lupp. Thank you
Open Office 4.1.7 Mac OS 10.15.2
Post Reply