TOP

Camel Case Function for LibreOffice Calc

CAMELCASE() Description

The function CAMELCASE() is designed to combine words with a variable case of letters, that is, a single word is returned as a result, for example "camelCaseFunction".

Camel Case splits a string of words separated by spaces, hyphens, underscores, or letter case into separate elements. Then, it converts all of these to uppercase words except the first one, which is converted to lowercase, and combines all the elements into one word.

The CAMELCASE() function can be useful in programming to convert variable names according to accepted code writing conventions (coding conventions).


BASIC code for function Camel Case

Here is the macro code to create a custom function that can combine different words into one, where the logical parts will be separated by a variable case (Camel Case) in the LibreOffice Calc program.

Open the menu Tools - Macros - Edit Macros..., select Module1 and copy the following text into the module:

Function CamelCase(ByVal str As String) As String
  Dim words As Variant
  Dim i     As Integer
  Dim FCalc As Object
  
  FCalc = CreateUnoService("com.sun.star.sheet.FunctionAccess")
  
  str = Replace(str,"-"," ")
  str = Replace(str,"_"," ")  
  str = FCalc.callFunction("TRIM", Array(str))  
  str = FCalc.callFunction("REGEX", Array(str,"([:lower:])([:upper:])","$1 $2","g"))
    
  str = LCase(str)
  words = Split(str, " ")
    
  For i = LBound(words) + 1 To UBound(words)       
    words(i) = UCase(Left(words(i), 1)) & Mid(words(i), 2)       
  Next i
    
  CamelCase = Join(words,"")
End Function

Then close Macro Editor, return to LibreOffice Calc and use our new function in any cell.