本帖最后由 Joestar.Xu 于 2023-8-8 10:07 编辑
如图所示:
插入一张浮动图片,放在B列中间,选中A列,向右插入10列,在撤销之后,图片尺寸变大,图片设置pic.dynamicMove(true); pic.dynamicSize(false);
插入列的command为自定义command,代码如下:
// 右侧插入列事件注册
public setPushCol(callBack) {
this.spread.commandManager().register("pushMutiColsCommand", {
canUndo: true,
execute: (context, options, isUndo) => {
const sheet = context.getSheetFromName(options.sheetName);
let Commands = GC.Spread.Sheets.Commands;
if (isUndo) {
// fix框架bug: 插入列后反复撤销重做会导致图表宽度变化
const charts = sheet.charts.all();
const width = [];
charts.forEach((item) => {
width.push(item?.width());
})
Commands.undoTransaction(context, options);
charts.forEach((item, index) => {
item?.width(width[index]);
})
sheet.suspendPaint();
sheet.suspendEvent();
resetFrozenRange.call(this,false,'col','right',options.selections[0].col+options.selections[0].colCount -1,parseInt(options.addNum));
callBack && callBack();
sheet.resumePaint();
sheet.resumeEvent();
return true;
} else {
Commands.startTransaction(context, options);
sheet.suspendPaint();
sheet.suspendEvent(); //todo:使用后不会触发行列change事件回调,影响协同
try {
if (parseInt(options.addNum)) {
const col = options.selections[0].col < 0 ? 0 : options.selections[0].col;
const lastCol = (options.selections[0].col + options.selections[0].colCount - 1) > sheet.getColumnCount(GC.Spread.Sheets.SheetArea.viewport) ? sheet.getColumnCount(GC.Spread.Sheets.SheetArea.viewport) : options.selections[0].col + options.selections[0].colCount - 1;
const columnWidth = sheet.getColumnWidth(lastCol);
let colCount = options.selections[0].colCount;
// 记录选中列的style
const styleList = [];
for (let i = 0; i < sheet.getRowCount(); i++) {
let style = sheet.getActualStyle(i, lastCol, GC.Spread.Sheets.SheetArea.viewport, true);
style.backgroundImage = null;
styleList.push(style);
}
insertColChart(sheet, col + 1);
resetFrozenRange.call(this,true,'col','right',col + colCount-1,parseInt(options.addNum));
sheet.resumePaint();
sheet.resumeEvent();
sheet.addColumns(col + colCount, parseInt(options.addNum));
sheet.suspendPaint();
sheet.suspendEvent();//todo:使用后不会触发行列change事件回调,影响协同
for (let i = 0; i < options.addNum; i++) {
// 设置插入列的列宽
sheet.setColumnWidth(col + colCount + i, columnWidth);
// 设置插入的列为选中列的样式,自号、颜色等、加粗等
for (let j = 0; j < sheet.getRowCount(); j++) {
sheet.setStyle(j, col + colCount + i, styleList[j]);
}
}
sheet.setSelection(-1, col + colCount, sheet.getRowCount(), parseInt(options.addNum));
callBack && callBack();
resetCharts(sheet);
}
} catch (e) {
console.log(e);
}
sheet.resumePaint();
sheet.resumeEvent();
Commands.endTransaction(context, options);
this.spread.focus(true);
return true;
}
}
});
}
|
|