Forum Discussion
excelhelp2280
Feb 05, 2024Copper Contributor
Increase Office Script Run Speed
Hi I have set up multiple office scripts to automate mapping data. We have a "Master sheet" which is the source to all of the data required. I have then used formulas to pull over the relevant i...
excelhelp2280
Feb 05, 2024Copper Contributor
SergeiBaklan
Feb 05, 2024Diamond Contributor
Just in case attached is the file on which I tested
- excelhelp2280Feb 06, 2024Copper ContributorThank you, ive got that to work. Do you think this will work better/more efficient than using the formulas?
- SergeiBaklanFeb 06, 2024Diamond Contributor
Great to know you sorted that out. Yes, performance shall be better. In addition you may adjust conditional formatting, but that's more cosmetic. Something like
function main(workbook: ExcelScript.Workbook) { let selectedSheet = workbook .getActiveWorksheet(); const source = workbook .getWorksheet("Sheet1") .getRange("A1:BC1") .getUsedRange() .getValues()[0] const maxRowsInSource = 10000 const targetNames = selectedSheet .getRange("A1:BC1") const numberOfNames = targetNames .getCellCount() const headers = targetNames .getValues()[0] for (let i = 0; i < numberOfNames; ++i) { targetNames .getCell(0, i) .copyFrom( workbook .getWorksheet("Sheet1") .getRangeByIndexes( 0, source.indexOf( headers[i] ), maxRowsInSource, 1 ) .getUsedRange() ) } const duplicateRule: ExcelScript.ConditionalPresetCriteriaRule = { criterion: ExcelScript.ConditionalFormatPresetCriterion.duplicateValues } const blankRule: ExcelScript.ConditionalPresetCriteriaRule = { criterion: ExcelScript.ConditionalFormatPresetCriterion.blanks } const rangesBlank = selectedSheet .getRanges("F2:F3000, I2:I3000") const rangesDuplicate = selectedSheet .getRanges("F2:F3000") rangesBlank.clearAllConditionalFormats() rangesDuplicate.clearAllConditionalFormats() const conditionalFormatBlanks = rangesBlank.addConditionalFormat( ExcelScript.ConditionalFormatType.presetCriteria); const conditionalFormatDuplicate = rangesDuplicate.addConditionalFormat( ExcelScript.ConditionalFormatType.presetCriteria); const presetFormatBlank: ExcelScript.PresetCriteriaConditionalFormat = conditionalFormatBlanks.getPreset(); presetFormatBlank.getFormat().getFill().setColor("lightyellow"); const presetFormatDuplicate: ExcelScript.PresetCriteriaConditionalFormat = conditionalFormatDuplicate.getPreset(); presetFormatDuplicate.getFormat().getFill().setColor("red"); presetFormatDuplicate.getFormat().getFont().setColor("white"); presetFormatBlank.setRule(blankRule); presetFormatDuplicate.setRule(duplicateRule); }