TOP

Camel Case LibreOffice 的函数 Calc

CAMELCASE() 描述

函数 CAMELCASE() 旨在将单词与可变字母大小写组合起来,即返回单个单词作为结果,例如“camelCaseFunction”。

Camel Case 将由空格、连字符、下划线或字母大小写分隔的单词字符串拆分为单独的元素。然后,它将除了第一个转换为小写的单词之外的所有这些单词转换为大写单词,并将所有元素组合成一个单词。

CAMELCASE() 函数可用于根据可接受的代码编写约定转换变量名称的编程 (coding conventions)


BASIC 函数代码 Camel Case

下面是创建一个自定义函数的宏代码,该函数可以将不同的单词组合成一个,其中逻辑部分将由 LibreOffice Calc 程序中的变量 case (Camel Case) 分隔。

打开菜单 Tools - Macros - Edit Macros...,选择 Module1 并将以下文本复制到模块中:

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

然后关闭 Macro Editor,返回到 LibreOffice Calc 并在任何单元格中使用我们的新函数。

使用 YouLibreCalc 扩展

您还可以通过设置扩展名“来使用 CAMELCASE() 函数 YouLibreCalc.oxt ”。此后,该功能将在所有在 LibreOffice Calc 中打开的文件中可用。