u/Iowadigger

▲ 3 r/excel

Office Script - Delete rows based on values

function main(workbook: ExcelScript.Workbook) {
const currentSheet = workbook.getActiveWorksheet();
const DELETE_Animals = [
"Zebra",
"Fish",
"Monkey"
];

// Get all values from the sheet
const usedRange = currentSheet.getUsedRange();
const values = usedRange.getValues();

console.log(`Starting with ${values.length} rows.`);

// Filter out the rows with undesired colors
const newValues = values.filter(row => !DELETE_Animals.includes(row[9]));

console.log(`Finished filtering. ${newValues.length} rows remaining.`);

// Clear the original range
usedRange.clear();

// Write the filtered values back to the worksheet starting from the top
if (newValues.length > 0) {
currentSheet.getRangeByIndexes(0, 0, newValues.length, newValues[0].length).setValues(newValues);
}
console.log(`Process completed.`);
}

I have an application that exports an Excel file daily. Power Automate grabs the files and runs this script against it. It works great on 9/10 accounts. One account always fails. If I manually run this script on the account that always has problems, it errors out with the following.

Line 25: Range setValues: The argument is invalid or missing or has an incorrect format.

[16, 65] Argument of type 'string | number | boolean' is not assignable to parameter of type 'string'. Type 'number' is not assignable to type 'string'

Beers if you can figure out this one!

reddit.com
u/Iowadigger — 14 hours ago
▲ 2 r/excel

Office Scripts - Delete rows that contain certain text

function main(workbook: ExcelScript.Workbook) {

// Get the used range on the current sheet.
const currentSheet = workbook.getActiveWorksheet();
// Get the RangeAreas object for each cell with a formula.
const usedRange = currentSheet.getUsedRange();
//get the values of the range
let values = usedRange.getValues();
//get the row count of the range
let rowCount = usedRange.getRowCount();
// Column A is 0, B is 1, C is 2 etc.
// starting at the last row, check if the cell in column 'B' equals to 'ZEBRA'. If it is, then delete the entire row.
for (let i = rowCount - 1; i >= 0; i--) {
if (
 values[i][1] == "Extended Systems") {
  usedRange.getCell(i, 1).getEntireRow().delete(ExcelScript.DeleteShiftDirection.up)
}
}
}

Good morning all, I have a workbook, no tables. In column C I have a bunch of zoo animals. I want to delete Zebra out of there. My script is not working, can another set of eyes see why?

reddit.com
u/Iowadigger — 8 days ago