我尝试编写Find& amp;在LibreOffice的Calc中用Python替换方法来替换所有"。+"用"&" (在一栏中 - 不那么重要) - 不幸的是,即使是标准的Find&替换方法似乎是不可能的(对我而言)。这就是我现在所拥有的:
import uno
def search()
desktop = XSCRIPTCONTEXT.getDesktop()
document = XSCRIPTCONTEXT.getDocument()
ctx = uno.getComponentContext()
sm = ctx.ServiceManager
dispatcher = sm.createInstanceWithContext("com.sun.star.frame.DispatchHelper", ctx)
model = desktop.getCurrentComponent()
doc = model.getCurrentController()
sheet = model.Sheets.getByIndex(0)
replace = sheet.createReplaceDescriptor()
replace.SearchRegularExpression = True
replace.SearchString = ".+$"
replace.ReplaceString ="&"
return None
会发生什么:完全没有!我会很高兴并感谢每一个提示,示例代码和激励的话语!
答案 0 :(得分:0)
此代码将A列中的所有非空单元格更改为&
:
def calc_search_and_replace():
desktop = XSCRIPTCONTEXT.getDesktop()
model = desktop.getCurrentComponent()
sheet = model.Sheets.getByIndex(0)
COLUMN_A = 0
cellRange = sheet.getCellRangeByPosition(COLUMN_A, 0, COLUMN_A, 65536);
replace = cellRange.createReplaceDescriptor()
replace.SearchRegularExpression = True
replace.SearchString = r".+$"
replace.ReplaceString = r"\&"
cellRange.replaceAll(replace)
请注意,代码调用replaceAll来实际执行某些操作。另外,来自User Guide:
&安培;将插入与Search RegExp相同的字符串。
因此替换字符串必须是文字的 - \&
。