已编辑
以下代码将采用当前选定的单元格,将每个单元格替换为实际的字符串值(注意:可能作用于单元格的 calc 函数将永久删除),然后将根据需要替换字符串单元格。
受http://www.oooforum.org/forum/viewtopic.phtml?t=137064和http://www.oooforum.org/forum/viewtopic.phtml?t=71015和http://www.oooforum 的启发。 org/forum/viewtopic.phtml?t=65318 ...
Sub ReplaceEachCellWithActualValue()
' Gets the current user selection and replace each cell with the actual String value
Dim oDoc, oSheet, oCell As Object
oDoc = ThisComponent
oSheet = oDoc.getSheets.getByName(oDoc.CurrentSelection.Spreadsheet.Name)
RangeAddress = oDoc.getCurrentSelection.getRangeAddress
c1 = RangeAddress.StartColumn
r1 = RangeAddress.StartRow
c2 = RangeAddress.EndColumn
r2 = RangeAddress.EndRow
for i = c1 to c2
for j = r1 to r2
Dim cellasstring As String
oCell = oSheet.getCellByPosition(i, j)
cellasstring = oCell.string
oSheet.getCellByPosition(i, j).String = cellasstring
next j
next i
End Sub
Sub CalcFindAndReplace
' first replace all formulas on the selected cells with actual strings...
ReplaceEachCellWithActualValue()
' Then replace as desired...
Dim oDoc,aFind,aReplace,aRayCount,FandR,oSheet
oDoc = ThisComponent
aFind = Array("Mecanica","Cancion","Murcielago")
aReplace = Array("Mecánica","Canción","Murciélago")
aRayCount = 0
oSheet = oDoc.getSheets.getByName(oDoc.CurrentSelection.Spreadsheet.Name)
FandR = oSheet.createReplaceDescriptor
FandR.SearchCaseSensitive = true
FandR.SearchWords = true ' 1 to A but not 11 to AA
FandR.SearchRegularExpression = true
Dim oSelection as Object
oSelection = oDoc.CurrentController.getSelection
While aRayCount <= uBound(aFind)
FandR.setSearchString(aFind(aRayCount))
FandR.setReplaceString(aReplace(aRayCount))
oSelection.ReplaceAll(FandR)
aRayCount = aRayCount + 1
Wend
End Sub