【问题标题】:remove double quotes using a macro使用宏删除双引号
【发布时间】:2021-07-10 07:18:10
【问题描述】:

我正在使用此功能,它按预期工作。它会删除所有标点符号。

  1. 第一个问题是不去掉双引号“
  2. 第二个问题是我需要选择需要处理的文本。我更喜欢默认纠正当前文件(所有文本)。
Sub removePunc()
REM the text ranges to work on must be seleczed in advance.
REM This will be done mostly by a F&R action with an appropriate
REM search strung and 'Find All'.
REM the this macro can be run.
fa = createUnoService("com.sun.star.sheet.FunctionAccess")
rgs = ThisComponent.CurrentSelection
n = rgs.Count -1
For k = 0 To n
rg = rgs(k)
h = fa.callFunction("REGEX", Array(rg.String, "!", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "'", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , ",", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\(", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\)", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\*", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\-", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\;", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\?", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\[", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\]", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\–", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\—", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\‘", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\“", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\”", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\.", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\:", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\'", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\uFEFF", " ", "g"))
rg.String = h
Next k
End Sub

【问题讨论】:

  • 1.似乎按预期工作。
  • 在 2 个引号中,双引号“被删除,但标准双引号”未被删除。
  • 您没有在表达式中包含标准双引号,(请参阅@Yuri 的答案)
  • 我已经更新了答案

标签: libreoffice libreoffice-writer


【解决方案1】:
  1. 这对我有用:
h = fa.callFunction("REGEX", Array(h, """", " ", "g"))
  1. 我对 VBA 和 LibreOffice 几乎一无所知,所以这可能不是最佳解决方案,但它可以在没有选择的情况下以某种方式工作:
Sub removePunc()

    fa = createUnoService("com.sun.star.sheet.FunctionAccess")
    rg = ThisComponent.Text

    h = fa.callFunction("REGEX", Array(rg.String, "!", " ", "g"))
    h = fa.callFunction("REGEX", Array(h        , "'", " ", "g"))
    h = fa.callFunction("REGEX", Array(h        , ",", " ", "g"))
    h = fa.callFunction("REGEX", Array(h        , "\(", " ", "g"))
    h = fa.callFunction("REGEX", Array(h        , "\)", " ", "g"))
    h = fa.callFunction("REGEX", Array(h        , "\*", " ", "g"))
    h = fa.callFunction("REGEX", Array(h        , "\-", " ", "g"))
    h = fa.callFunction("REGEX", Array(h        , "\;", " ", "g"))
    h = fa.callFunction("REGEX", Array(h        , "\?", " ", "g"))
    h = fa.callFunction("REGEX", Array(h        , "\[", " ", "g"))
    h = fa.callFunction("REGEX", Array(h        , "\]", " ", "g"))
    h = fa.callFunction("REGEX", Array(h        , "\–", " ", "g"))
    h = fa.callFunction("REGEX", Array(h        , "\—", " ", "g"))
    h = fa.callFunction("REGEX", Array(h        , "\‘", " ", "g"))
    h = fa.callFunction("REGEX", Array(h        , "\“", " ", "g"))
    h = fa.callFunction("REGEX", Array(h        , "\”", " ", "g"))
    h = fa.callFunction("REGEX", Array(h        , "\.", " ", "g"))
    h = fa.callFunction("REGEX", Array(h        , """", " ", "g"))
    
    ThisComponent.Text.String = h

End Sub

其实你所有的改变都可以一步完成:

查找:"[!',\(\)\*\-;\?\[\]\–\—‘“”\.\""]"

替换为: (空格)

但是,如果您想在此处使用更改列表,则更理想的实现是:

Sub Replace
  Dim to_search() As String
  Dim to_replace() As String
  Dim n As Long
  Dim oDocument As Object
  Dim oReplace As Object

  to_search()  = Array("[!',\(\)\*\-;\?\[\]\–\—‘“”\.\""]")
  to_replace() = Array(" ")

  oDocument = ThisComponent
  oReplace = oDocument.createReplaceDescriptor
  oReplace.SearchRegularExpression = True
  For n = lbound(to_search()) To ubound(to_search())
    oReplace.SearchString  = to_search(n)
    oReplace.ReplaceString = to_replace(n)
    oDocument.replaceAll(oReplace)
  Next n
End Sub

基于官方示例:https://api.libreoffice.org/examples/basic/text/modifying_text_automatically/

它也可以一步替换,但如果需要,您可以向数组中添加其他元素,这样:

to_search()  = Array("no", "never", "no way!")
to_replace() = Array("yes", "always", "why not?")

【讨论】:

    【解决方案2】:

    感谢用户@Yuri khristich,这按预期工作:

    Sub removePunc()
        fa = createUnoService("com.sun.star.sheet.FunctionAccess")
        rg = ThisComponent.Text
        h = fa.callFunction("REGEX", Array(rg.String, "[!',\:\(\)\*\-;\?\[\]\–\—‘“”\.\""]", " ", "g"))
        h = fa.callFunction("REGEX", Array(h, "\uFEFF", " ", "g"))
    
        ThisComponent.Text.String = h
        ListMisSpelledWords3
    End Sub
    

    【讨论】:

    • 该命令可用于处理上述宏返回的结果 awk -F ':' '{print $1}' tt3.txt | tr -d''| grep -v 'च$' | grep -v 'ही$' | tr '\n' ' ' > tos4.txt
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 2019-12-25
    • 2019-08-11
    相关资源
    最近更新 更多