Forum Discussion
djmitz6
Feb 21, 2024Copper Contributor
Excel Script ReplaceAll ignore formulas
Greetings, I'm currently running an excel script where I am replacing all single quote characters with two single quote characters consecutively so that I can push the data in a table over to an ...
SergeiBaklan
MVP
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?
djmitz6
Feb 22, 2024Copper Contributor
You got it!
- SergeiBaklanFeb 22, 2024MVP
As for the performance I'm not sure. Perhaps your variant will be better. That's only to test.
- SergeiBaklanFeb 22, 2024MVP
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(); }