温馨提示
本文档的示例代码仅适用于本文档中的示例报表/场景。若实际报表/场景与示例代码无法完全适配(如使用功能不一致,或多个宏代码冲突等),需根据实际需求开发代码。
示例效果
当鼠标移动到电子表格上单击选中行时,该行高亮显示
实现步骤
- 首先在电子表格设计器(Microsoft Office Excel)中,创建电子表格报表。
- 在浏览器的资源定制节点下,选中电子表格,右键选择 编辑宏 进入报表宏界面。
- 新建客户端模块。在弹出的新建模块对话框中,选择对象为spreadSheetReport、事件为onRender、并把下面宏代码复制到代码编辑区域。
宏类型
类型 | 对象 | 事件 |
ClientSide | SpreadsheetReport | onRender |
宏代码
function main(spreadsheetReport) { spreadsheetReport._focusTR = null; spreadsheetReport.initTableGrid(); spreadsheetReport.addListener(spreadsheetReport.elemSheetFrame.contentWindow.document.body, "click", function(e) { var t = e.target; while (t && t.tagName != "TR") t = t.parentNode; if (!t) return; if (this._focusTR) { var tdRow = this.tableGrid[this._focusTR.rowIndex]; for (var i = 0; i < tdRow.length; i++) { tdRow[i].style.backgroundColor = tdRow[i]._originalBackgroundColor; } } this._focusTR = t; var tdRow = this.tableGrid[t.rowIndex]; for (var i = 0; i < tdRow.length; i++) { var td = tdRow[i]; if (typeof td._originalBackgroundColor == "undefined") { td._originalBackgroundColor = td.style.backgroundColor; } td.style.backgroundColor = "#ffc9af"; } }, spreadsheetReport, "sheetFrameBody"); }
评论
王峰 发表:
如果用到条件格式-数据条或者其它特殊场景,会在那一行里增加一个新的table,新的table会有td,点击的时候,下标就不对了,导致点击高亮显示在第一行
可通过在
while
(t && t.tagName !=
"TR"
)前面,增加以下代码定位到主table中(因为产品电子表格生成的td都会带有id的,其它特殊情况可特殊处理)
while (t && t.id == "")
t = t.parentNode;