Hello,
Your function can be simplified to:
def REPLACETEXTS_py(text, oldWords, newWords):
for oldWord, newWord in zip(oldWords, newWords):
text = text.replace(oldWord, newWord)
return text
This works fine for me.
xlSlim is passing the cell ranges as .Net arrays which usually behave just the same as Python lists. However if you really want Python lists the conversion is simply:
oldWords = list(oldWords)
newWords = list(newWords)
The best way to debug this sort of problem is to log the inputs being received by the function.
If I amend the code to:
import logging
LOG = logging.getLogger(__name__)
def REPLACETEXTS_py(text, oldWords, newWords):
LOG.info("Received oldWords type %s data %s", type(oldWords), repr(oldWords))
LOG.info("Received newWords type %s data %s", type(newWords), repr(newWords))
oldWords = list(oldWords)
newWords = list(newWords)
LOG.info("After conversion to list, oldWords type %s data %s", type(oldWords), repr(oldWords))
LOG.info("After conversion to list, newWords type %s data %s", type(newWords), repr(newWords))
for oldWord, newWord in zip(oldWords, newWords):
text = text.replace(oldWord, newWord)
return text
I get these log messages:
2023-02-15 09:18:54,950 ReplaceWords: INFO Received oldWords type <class 'System.Object[,]'> data <System.Object[,] object at 0x0000023667694348>
2023-02-15 09:18:54,950 ReplaceWords: INFO Received newWords type <class 'System.Object[,]'> data <System.Object[,] object at 0x0000023667695108>
2023-02-15 09:18:54,950 ReplaceWords: INFO After conversion to list, oldWords type <class 'list'> data ['Mary', 'lamb']
2023-02-15 09:18:54,950 ReplaceWords: INFO After conversion to list, newWords type <class 'list'> data ['Bob', 'horse']
FYI, the log location can be found by using the LogLocation() function.
All the best,
Russel