3

Ubuntu 12.04 で Libreoffice 3.5.7.2 を使用します。

計算セルに次の形式のテキストがあります: (IBM) Ibm Corporation.

基本的なマクロを使用して、正規表現を使用して () の間のテキストを抽出しようとしています。これは私がこれまでに試したことです。

Sub getMktValue()
  Dim oDoc as Object
  Dim oSheet as Object
  Dim oCell as Object

  oDoc = ThisComponent
  oSheet = oDoc.Sheets.getByName("Income")
  'regex test code'
  oCell = oSheet.getCellByPosition(0, 1)
  stk = oCell.String()  
  myRegex = oCell.createSearchDescriptor
  myRegex.SearchRegularExpression = True
  myRegex.SearchString = "\((.*)\)"  '"[\([A-Z]\)]" "\(([^)]*)\)" "\(([^)]+)\)"'
  found = oCell.FindFirst(myRegex)
  MsgBox found.String
End Sub

myRegex.SearchString 行には、私が試したさまざまなバージョンが含まれています。結果は常に同じです。() の間のテキストだけでなく、セルの内容全体が返されます。() の間のテキストだけを抽出する方法はありますか?

ありがとう、ジム

4

1 に答える 1

4

試したメソッド.FindFirstは、XSearchable(スプレッドシートや範囲などの) 内で最初に出現する を検出しSearchStringます。

文字列値内を検索する場合は、別のサービスが必要ですcom.sun.star.util.TextSearch

Sub getMktValue()
  Dim oDoc as Object
  Dim oSheet as Object
  Dim oCell as Object

  oDoc = ThisComponent
  oSheet = oDoc.Sheets.getByName("Income")
  'regex test code'
  oCell = oSheet.getCellByPosition(0, 1)
  stk = oCell.getString() 

  oTextSearch = CreateUnoService("com.sun.star.util.TextSearch")
  oOptions = CreateUnoStruct("com.sun.star.util.SearchOptions")
  oOptions.algorithmType = com.sun.star.util.SearchAlgorithms.REGEXP
  oOptions.searchString = "\((.*)\)"
  oTextSearch.setOptions(oOptions)
  oFound = oTextSearch.searchForward(stk, 0, Len(stk))
  sFound = mid(stk, oFound.startOffset(0) + 1, oFound.endOffset(0) - oFound.startOffset(0))
  MsgBox sFound
  sFound = mid(stk, oFound.startOffset(1) + 1, oFound.endOffset(1) - oFound.startOffset(1))
  MsgBox sFound
End Sub

ご挨拶

アクセル

于 2014-09-02T16:49:59.467 に答える