自适应行高我自己写了一个方法,但是不起作用,有什么比较理想的解决方案吗?
setChingHeight() {
// 设置默认行高
const defaultRowHeight = 30;
// 自适应行高函数
const autoFitRowHeight = (sheet, event, args) => {
const row = args.row;
const col = args.col;
const cell = sheet.getCell(row, col);
const cellContent = cell.text();
// 获取单元格的样式
const cellStyle = sheet.getStyle(row, col);
const fontStyle = cellStyle
? cellStyle.font
: sheet.getDefaultStyle().font;
// 获取单元格内容的高度
const contentHeight = sheet.measureText(fontStyle, cellContent).height;
// 设置新的行高
const newRowHeight = Math.max(defaultRowHeight, contentHeight + 10);
sheet.setRowHeight(row, newRowHeight);
};
// 为所有的 sheet 添加 CellChanged 事件监听
const addListenerToAllSheets = () => {
const sheetCount = this.spread.getSheetCount();
for (let i = 0; i < sheetCount; i++) {
const sheet = this.spread.getSheet(i);
sheet.defaults.rowHeight = defaultRowHeight;
sheet.unbind(GC.Spread.Sheets.Events.CellChanged);
sheet.bind(
GC.Spread.Sheets.Events.CellChanged,
autoFitRowHeight.bind(this, sheet)
);
}
};
// 初始化时为所有的 sheet 添加监听
addListenerToAllSheets();
// 当添加新的 sheet 时,为新的 sheet 添加监听
this.spread.bind(
GC.Spread.Sheets.Events.SheetAdded,
function (event, args) {
console.log(event, args);
addListenerToAllSheets();
}
);
},
|
|