我有以下功能适用于少量行:
async function RemoveEmptyRows() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
var selectedRange = context.workbook.getSelectedRange();
var surroundingRegion = selectedRange.getSurroundingRegion();
var firstCell = selectedRange.getCell(0, 0);
firstCell.load(['columnIndex']);
surroundingRegion.load(['rowCount', 'columnCount']);
await context.sync();
var newRange = sheet.getRangeByIndexes(0, firstCell.columnIndex, surroundingRegion.rowCount, 1);
newRange.load(['address', 'rowCount', 'values']);
await context.sync();
var i = 0;
var loopRange = newRange.rowCount;
var counter = 0;
var cellText = "";
while (i < loopRange) {
cellText = newRange.values[i][0];
if (cellText == "") {
var entireRow = sheet.getRangeByIndexes(i - counter, 0, 1, surroundingRegion.columnCount);
entireRow.delete(Excel.DeleteShiftDirection.up);
counter++;
}
i++;
}
await context.sync();
});
}
这个函数的问题是它会逐行向上移动整行。还有另一个函数(从 ScriptLab 修改)来删除重复项,它适用于更多的行。
async function RemoveDuplicates() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
var selectedRange = context.workbook.getSelectedRange();
var firstCell = selectedRange.getCell(0, 0);
var surroundingRegion = selectedRange.getSurroundingRegion();
firstCell.load('columnIndex');
surroundingRegion.load('address');
await context.sync();
var columnIndex = firstCell.columnIndex;
const deleteResult = surroundingRegion.removeDuplicates([columnIndex], true);
deleteResult.load();
});
}
有没有办法创建类似于 RemoveDuplicates 函数的东西,但对于 EmptyRows?
谢谢。