注意 |
---|
该宏示例在 V10.5上 验证通过 提示:本文档的示例代码仅适用于本文档中的示例报表/场景。若实际报表/场景与示例代码无法完全适配(如使用功能不一致,或多个宏代码冲突等),需根据实际需求开发代码。 |
1. 示例效果
- 原效果:
- 预期效果:
2. 操作步骤
- 步骤一: 新建一个空白的自助仪表盘
- 步骤二: 拖入"web电子表格",双击”web电子表格“进入编辑界面
- 步骤三:数据如下,保存退出
- 步骤四:鼠标右键进入宏管理页面
- 步骤五:新建宏。事件:“ onAfterRender(组件渲染后)”
- 步骤六:编写宏代码实现效果
把下面宏代码复制到代码区域
代码块 | ||||||
---|---|---|---|---|---|---|
| ||||||
function main(page: IPage, portlet: ITableSheetPortlet) { let r = portlet.getRowCount(); //获取电子表格总行数 for (let i = 2; i < r - 1; i++) { // 设置类别 let cell = portlet.getCell(i, 1); //根据行列号获取单元格 truncateCellInnerText(cell, 5); //设置显示的字符个数 // 设置名称 cell = portlet.getCell(i, 2); truncateCellInnerText(cell, 2); //设置数量 cell = portlet.getCell(i, 3); truncateCellInnerText(cell, 4); } } // 如果单元格内容超出指定长度、将其截短 function truncateCellInnerText(cell: ISheetCell, textMaxLength: number) { if (!cell) { return; } let len = textMaxLength || 10; let txt = cell.getText(); if (txt.length > len) { cell.setText(txt.substring(0, len) + "..."); cell.setAttribute("title", txt); } } function strlen(str: string) { let len = 0; for (let i = 0; i < str.length; i++) { let c = str.charCodeAt(i); //单字节加1 if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) { len++; } else { len += 2; } } return len; } |
- 步骤七:点击 保存。重新访问报表,可看到效果已实现