Calc basic, macro How to query data types

mike@RPI4b3:~> uname -a
Linux MikesPI 6.1.0-rpi7-rpi-v8 #1 SMP PREEMPT Debian 1:6.1.63-1+rpt1 (2023-11-24) aarch64 GNU/Linux

Version: 7.4.7.2 / LibreOffice Community
Build ID: 40(Build:2)
CPU threads: 4; OS: Linux 6.1; UI render: default; VCL: x11
Locale: en-US (C); UI: en-US
Raspbian package version: 4:7.4.7-1+rpi1+deb12u8
Calc: threaded

Not that the above info has anything to do with the question,

In OOME 4 Listing 410 Mr. Petonyak lists Value, String, Formula, there is also Empty as cell types.
In https://wiki.openoffice.org/wiki/OpenOffice.org_Basic_in_NetBeans/files/language.nbs data types are listed as Boolean, Date, Double, Integer, Long, Object, Single, String, Variant, Empty, Null.
In a basic macro is the a way to find the second type of the contents of a cell?
Thanks,
Mike

The Calc Cells and the Basic use different bunch of data types.
The DeteTime in a cell is a double precision float number really: the integer part represents the elapsed days since the base date, and the decimal fdraction part represents the hours minutes, seconds part.

The boolean values in a Cell: value 0 is always means FALSE, values equals non-zero (even it is negative) alvays means TRUE

The Formula is a special String.

A cell that contains a formula (even with result nothing ("") ) will not be EMPTY type.

https://help.libreoffice.org/latest/en-US/text/sbasic/shared/01020100.html

Otherwise when you pass a Cell to a Basic Subroutine, that can be an Object, what has various properties and methods: coordinates on a sheet, the number and the name of the sheet, formatting properties, and much more… One of these properties is the cell content.

Supplement to the answer of colleague @Zizi64 .
In Calc, cells can have the following types of values (for cells containing formulas, we talk about the type of the formula calculation result):

  • empty
  • number (double)
  • string
  • error value

In Excel, boolean type is added to this list (plus exotic ones).

This is a list of LO Basic data types (not complete).

In particular, you can pay attention to CellContentType, FormulaResultType2, getError()


By the way, the documentation for getError() says:

If the cell does not contain a formula, the error is always zero.

This is not true. If we copy a cell containing a formula returning an error value and paste its value into a second cell using special paste, then the getError() method for the second cell will return a non-zero value.

These properties you mention may be what I’m looking for but they return Long, where do I find the mapping between the Long and the English word that describes the type?
Thanks,
Mike

изображение

Follow the hyperlink provided in See also

Calc knows only text and number as constant values. Excel knows boolean as a third data type.
In Calc any valid integer, date, time, currency, boolean is a formatted floating point number (double). Anything else is text unless it’s formula result error.

Any VB(A) book of the 90ies can teach you about the highly confusing Basic types, which are also implemented in StarBasic as introduced in StarOffice in 1997(?)

IOW, Basic and Calc have nothing to do with each other. StarBasic is just one possible programming language to access the UNO API of LibreOffice.

P.S. any spreadsheet cell has a value of type double, a string and a formula.
A cell with a constant text has the value 0.0 and the string is also the formula.
A cell with a numeric string (text “0123”) has the formula '0123.
A cell with a constant number has a string property according to the displayed string (the formatted number). The formula string represents the decimal representation of the numeric value.
A cell with a true formula (formula starting with =) has the returned string and value according to the above definitions. A formula returning an error value has the value 0.0 and the string that is dispayed in the UI.

OK, fellows.
After exploring all the references you gave digging into CellContentType, FormulaResultType2, typeName() and the calc Type() function I’m convinced I can’t get there from here.
In spite of the fact that basic allows me to declare Ints, Longs, Singles and Doubles they are all stored as double and there is no function to differentiate them after they are in memory.
Thanks very much, it has been an interesting trip.
T’all be well,
Mike

Knowing all this, you may resort to the distinction between numbers and text and how numbers are formatted in order to display dates, times, timestamps, currencies etc.
The following snippet has been recorded by MRI:

Sub Snippet(Optional oInitialTarget As Object)
  Dim nNumberFormat As Long
  Dim oSpreadsheet As Variant
  Dim oDrawPage As Variant
  Dim oForms As Variant
  Dim oParent As Variant
  Dim sImplementationName As String
  Dim oNumberFormats As Variant
  Dim oObj1 As Variant
  Dim nType As Integer

  nNumberFormat = oInitialTarget.NumberFormat 'returns 49
  oSpreadsheet = oInitialTarget.getSpreadsheet()
  oDrawPage = oSpreadsheet.getDrawPage()
  
  oForms = oDrawPage.getForms()
  oParent = oForms.getParent()
  sImplementationName = oParent.getImplementationName()
  
  oNumberFormats = oParent.getNumberFormats()
  oObj1 = oNumberFormats.getByKey(49) 'the 49 from above
  
  nType = oObj1.Type
REM returns 2
REM https://api.libreoffice.org/docs/idl/ref/namespacecom_1_1sun_1_1star_1_1util_1_1NumberFormat.html
End Sub

I queried the number format code of a cell, which was 49, navigated from there to the cell’s document and queried the number format 49 from the document’s NumberFormats. The number format has property Type, which is 2.
https://api.libreoffice.org/docs/idl/ref/servicecom_1_1sun_1_1star_1_1util_1_1NumberFormatProperties.html
According to LibreOffice: com::sun::star::util::NumberFormat Constant Group Reference, 2 means DATE.
No matter which numeric value (or which formula returning a number) I enter into this cell, the cell value will always be a floating point number displayed in some date format.
0.0 → 1899-12-30
40000.0 → 2009-07-06
=TODAY() → 2025-07-14

From above MRI recording, one may derive a function like this:

Function getNumberFormatType(doc, cell)
  nKey = cell.NumberFormat
  objFormat = doc.NumberFormats.getByKey(nKey)
  getNumberFormatType = objFormat.Type
End Function

might work with Writer tables as well.

That’s wrong due to the lack of a fundamental distinction:

  • Basic has its typed variables, and their values are, of course, stored in RAM. You can ask for the type using the Basic function TypeName()
  • Spreadsheet cells are objects with lots of properties and methods. What they may “contain”, what “attributes” they have, and a lot more isn’t ruled by any specifications of Basic types or Basic functions.
  • If a cell was edited or got a formula result, there are only two actual types, and three non-exclusive special cases for which you can ask using the Calc functions ISNUMBER(), ISTEXT(), ISBLANK(), ISFORMULA(), and ISERROR().
  • A spreadsheet cell in Calc with ISNUMBER()=TRUE always represents a value of type Double.
  • If ISTEXT()=TRUE an additional distinction is necessary:
    – If the cell shows a formula result, the type is String.
    – If the cell has literal content, this content supports text attributes for parts of the string which are not supported or forwarded by Calc formulas.

There are tools for introspective work: xray and mri
If I’m remembering correctly the first is written in Basic. So there is a way to go, even if you don’t see it…

@Wanderer note that @MikeMcClain46’s idea is to figure what is currently in a Calc’s cell: a Long, or a Single, or an Integer. Despite being told immediately from start, that “The Calc Cells and the Basic use different bunch of data types”, they kept the wrong idea that that is somehow possible until comment 8.

Of course, if your cell has 0.5, you may be sure it wasn’t an Integer. But was it a Single or a Double, which used to fill the cell? Or was it user input, and the user didn’t care which type they used, when typed characters?

A numeric data in Calc’s cells / formula results is always and only a double. Anything else is some heuristic like “it happens to also be representable in another Basic-specific type without loss”.

1 Like