67 lines
2.0 KiB
Plaintext
67 lines
2.0 KiB
Plaintext
/**
|
|
* 多行选择与批量修改(1.7.8)。
|
|
*
|
|
* 把「对某列统一赋值」展开为逐行变更,交给 TableEditor 记录;
|
|
* 展开时读取每行旧值以保留可回滚信息。
|
|
*/
|
|
import { CellChange } from './TableEditor';
|
|
|
|
export class BatchModifier {
|
|
/**
|
|
* 对选定行的某列批量赋值。
|
|
* @param rows 行号列表(会去重并升序)
|
|
* @param column 列名
|
|
* @param newValue 新值
|
|
* @param oldValueOf 读取旧值的回调
|
|
*/
|
|
static applyToRows(rows: number[], column: string, newValue: string,
|
|
oldValueOf: (row: number) => string): CellChange[] {
|
|
const unique: number[] = BatchModifier.normalizeRows(rows);
|
|
const out: CellChange[] = [];
|
|
for (let i = 0; i < unique.length; i++) {
|
|
out.push({
|
|
rowIndex: unique[i],
|
|
column: column,
|
|
oldValue: oldValueOf(unique[i]),
|
|
newValue: newValue
|
|
});
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* 对选定行的多列批量赋值(列到值的映射)。
|
|
* @param values 列名 → 新值
|
|
*/
|
|
static applyMultiple(rows: number[], values: Map<string, string>,
|
|
oldValueOf: (row: number, column: string) => string): CellChange[] {
|
|
const unique: number[] = BatchModifier.normalizeRows(rows);
|
|
const out: CellChange[] = [];
|
|
for (let i = 0; i < unique.length; i++) {
|
|
values.forEach((value: string, column: string) => {
|
|
out.push({
|
|
rowIndex: unique[i],
|
|
column: column,
|
|
oldValue: oldValueOf(unique[i], column),
|
|
newValue: value
|
|
});
|
|
});
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/** 行号去重并升序(负数忽略)。 */
|
|
static normalizeRows(rows: number[]): number[] {
|
|
const seen: Set<number> = new Set<number>();
|
|
const out: number[] = [];
|
|
for (let i = 0; i < rows.length; i++) {
|
|
if (rows[i] >= 0 && !seen.has(rows[i])) {
|
|
seen.add(rows[i]);
|
|
out.push(rows[i]);
|
|
}
|
|
}
|
|
out.sort((a: number, b: number) => a - b);
|
|
return out;
|
|
}
|
|
}
|