Forum Discussion
Find and Replace within a known range using typescript in the Script Editor
- Jun 16, 2022
Hi Jrolle2022
I'm not a JavaScript dev. and don't have access to TypeScript with my 365 subscription so can't test the following that's only my understanding after reading the doc.
The https://docs.microsoft.com/en-us/javascript/api/office-scripts/excelscript/excelscript.range?view=office-scripts has method https://docs.microsoft.com/en-us/javascript/api/office-scripts/excelscript/excelscript.range?view=office-scripts#excelscript-excelscript-range-replaceall-member(1) and after looking at the examples given for the https://docs.microsoft.com/en-us/javascript/api/office-scripts/excelscript/excelscript.replacecriteria the following should work:
function main(workbook: ExcelScript.Workbook) { let sheet = workbook.getActiveWorksheet(); let breakCol = sheet.getRange("O2:O25"); breakCol.replaceAll("\n", ",", { completeMatch: false }); }
or, given that completeMatch defaults to false:
function main(workbook: ExcelScript.Workbook) { let sheet = workbook.getActiveWorksheet(); let breakCol = sheet.getRange("O2:O25"); breakCol.replaceAll("\n", ","); }
Hope this helps a bit & your feedback will be appreciated, whatever it is
(It's my understanding that TypeScript issues should be raised at https://stackoverflow.com/questions/tagged/typescript)
Hi Jrolle2022
I'm not a JavaScript dev. and don't have access to TypeScript with my 365 subscription so can't test the following that's only my understanding after reading the doc.
The https://docs.microsoft.com/en-us/javascript/api/office-scripts/excelscript/excelscript.range?view=office-scripts has method https://docs.microsoft.com/en-us/javascript/api/office-scripts/excelscript/excelscript.range?view=office-scripts#excelscript-excelscript-range-replaceall-member(1) and after looking at the examples given for the https://docs.microsoft.com/en-us/javascript/api/office-scripts/excelscript/excelscript.replacecriteria the following should work:
function main(workbook: ExcelScript.Workbook)
{
let sheet = workbook.getActiveWorksheet();
let breakCol = sheet.getRange("O2:O25");
breakCol.replaceAll("\n", ",", { completeMatch: false });
}
or, given that completeMatch defaults to false:
function main(workbook: ExcelScript.Workbook)
{
let sheet = workbook.getActiveWorksheet();
let breakCol = sheet.getRange("O2:O25");
breakCol.replaceAll("\n", ",");
}
Hope this helps a bit & your feedback will be appreciated, whatever it is
(It's my understanding that TypeScript issues should be raised at https://stackoverflow.com/questions/tagged/typescript)
- Jrolle2022Jun 16, 2022Copper Contributor
Lorenzo You are amazing! That was just what I needed. I figured the "find" method wasn't going to cut it, but really wasn't aware of a programmatic alternative. Thanks a ton for your help!
- LorenzoJun 16, 2022Silver Contributor
Glad this worked as per the doc. Thanks for providing feedback & marking the solution (can help those who search this site)
Nice day...