Forum Discussion
Excel Script ReplaceAll ignore formulas
Thanks for the sharing.
May I clarify. When you use
usedRange.copyFrom(usedRange, ExcelScript.RangeCopyType.values, false, false)
you copy values from the range, even if they are generated by formulae. Thus initial question is not applicable here since you have no formulae in the resulting table. The question is would you like to keep initial formulae in the resulting table; or convert them into the structured formulae (not sure how to do that); or just values is enough.
- SergeiBaklanFeb 22, 2024Diamond Contributor
As for the performance I'm not sure. Perhaps your variant will be better. That's only to test.
- SergeiBaklanFeb 22, 2024Diamond Contributor
When it could be
function main(workbook: ExcelScript.Workbook) { // will be used as Power Automate parameters const WorkSheet_Name = "Sheet1 (2)" const DeleteTopRows = 11 let selectedSheet: ExcelScript.Worksheet; if (WorkSheet_Name == "") { selectedSheet = workbook.getWorksheets()[4]; } else { selectedSheet = workbook.getWorksheet(WorkSheet_Name); } if (DeleteTopRows > 0) { //Delete Top N Rows selectedSheet.getRange("1:" + DeleteTopRows).delete(ExcelScript.DeleteShiftDirection.up); } else { } // Clear Any Filters on Selected Sheet selectedSheet.getAutoFilter().remove(); // Get used Range let usedRange = selectedSheet.getUsedRange(); //usedRange.copyFrom(usedRange, ExcelScript.RangeCopyType.values, false, false) usedRange.getSpecialCells(ExcelScript.SpecialCellType.constants) .getAreas() .map(a => a.getUsedRange().replaceAll("'", "''", {})) // Add a new table let newTable = workbook.addTable(usedRange, true); // Replace SQL Reserved Characters /* selectedSheet.replaceAll("'", "''", { completeMatch: false, matchCase: false }) */ // Create ID column for Power Automate ODATA Filter newTable.addColumn(-1, Number[0], "ID"); let ID = newTable.getColumnByName("ID").getRangeBetweenHeaderAndTotal(); ID.setNumberFormatLocal("0"); ID.setFormula("=ROUNDUP((ROW()-1)/5000,0)") // Return Table ID to Power Automate return newTable.getId(); } - djmitz6Feb 22, 2024Copper ContributorYou got it!
- SergeiBaklanFeb 22, 2024Diamond Contributor
So, you'd like to
- remove first N rows
- convert the rest into the structured table (keeping formulae in it as is)
- double ' for each cell within the table ignoring ones with formula
Correct?
- djmitz6Feb 22, 2024Copper ContributorYes, my script is currently working as desired just looking for a more efficient solution. The problem I was having is if I didn't copy and paste the formula results, the formulas themselves would be come the cell value with additional apostrophes. It would have really been nice if the replaceAll function had an option to ignore formulas.
- SergeiBaklanFeb 22, 2024Diamond Contributor
When your script shall work. You have values only, all single apostrophes in texts will be doubled, and you have nothing but texts and numbers.