Here is a complete macro. It is written in Python, so use APSO to add the code.
Then for convenience, go to Tools → Customize and set a hotkey to run it.
import re
import uno
import unohelper
from com.sun.star.datatransfer import XTransferable, DataFlavor
def copy_and_replace():
oDoc = XSCRIPTCONTEXT.getDocument()
oRange = oDoc.getCurrentSelection()
oCellTopLeft = oRange.getCellByPosition(0, 0)
sText = oCellTopLeft.getString()
dReplace = {
"MS" : "Microsoft",
}
pattern = re.compile('|'.join(dReplace.keys()))
sResult = pattern.sub(lambda x: dReplace[x.group()], sText)
transferable = Transferable(sResult)
ctx = XSCRIPTCONTEXT.getComponentContext()
oClip = ctx.getServiceManager().createInstanceWithContext(
"com.sun.star.datatransfer.clipboard.SystemClipboard", ctx)
oClip.setContents(transferable, None)
class Transferable(unohelper.Base, XTransferable):
"""Keep clipboard data and provide them."""
def __init__(self, text):
df = DataFlavor()
df.MimeType = "text/plain;charset=utf-16"
df.HumanPresentableName = "encoded text utf-16"
self.flavors = [df]
self.data = [text] #[text.encode('ascii')]
def getTransferData(self, flavor):
if not flavor: return
mtype = flavor.MimeType
for i,f in enumerate(self.flavors):
if mtype == f.MimeType:
return self.data[i]
def getTransferDataFlavors(self):
return tuple(self.flavors)
def isDataFlavorSupported(self, flavor):
if not flavor: return False
mtype = flavor.MimeType
for f in self.flavors:
if mtype == f.MimeType:
return True
return False
# Functions that can be called from Tools -> Macros -> Run Macro.
g_exportedScripts = copy_and_replace,
For example, if a cell contains “The MS product,” then run the macro and paste into Notepad to produce “The Microsoft product.”
For more string changes, simply add entries to the dReplace
dictionary.
Sources: