Adding an empty row wherever there is a data change in 1 column

11 views Asked by At

Whenever I run my script, it's adding a new row in between every row that has data, instead of adding a new row in between the cell information changes

Initial Data Format

Goal Data Format

Here is the code:

function main(workbook: ExcelScript.Workbook) {

let sheet = workbook.getActiveWorksheet();
let usedRange = sheet.getUsedRange();
let rowCount = usedRange.getRowCount();

for (let i = rowCount; i > 0; i--){
  let cellData = sheet.getCell(i,0)
  let prevCell = sheet.getCell(i-1,0);
  
  if (cellData == prevCell) {
    continue;
  }

  if (cellData == sheet.getCell(99,99)){
    continue;
  }
  
  if(cellData != prevCell){
    cellData.getEntireRow().insert(ExcelScript.InsertShiftDirection.down);
  }
}

}

Actual Output

0

There are 0 answers