找回密码
 立即注册

QQ登录

只需一步,快速开始

CloudJSTI

金牌服务用户

36

主题

98

帖子

332

积分

金牌服务用户

积分
332
CloudJSTI
金牌服务用户   /  发表于:2023-4-20 17:22  /   查看:785  /  回复:1
自适应行高我自己写了一个方法,但是不起作用,有什么比较理想的解决方案吗?






  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();
        }
      );
    },



1 个回复

倒序浏览
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2023-4-20 18:54:44
沙发
实际上我们本身就提供了autoFitRow方法以及autoFitColumn方法,
https://demo.grapecity.com.cn/sp ... Commands#autofitrow

你只需要在需求的场景下调用就行,比如你说的单元格内容变化,ValueChanged
https://demo.grapecity.com.cn/sp ... #event:ValueChanged

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部