可以通过监听onOpenMenu获取打开右键菜单的单元格行列信息。
主要代码如下:
- function MyContextMenu() {}
- MyContextMenu.prototype = new GC.Spread.Sheets.ContextMenu.ContextMenu(spread);
- MyContextMenu.prototype.onOpenMenu = function (menuData, itemsDataForShown, hitInfo, spread) {
- // 获取行列信息
- console.log(hitInfo);
- var worksheetHitInfo = hitInfo.worksheetHitInfo;
- // 表示选择的是列头右键菜单
- if(worksheetHitInfo.rowViewportIndex == -1) {
- // 获取行列索引
- var row = worksheetHitInfo.row;
- var col = worksheetHitInfo.col;
- // 判断是否为合并单元格,如果是则获取合并单元格左上角单元格的row、col,进一步获取合并单元格的实际value
- var range = new GC.Spread.Sheets.Range(row,col,1,1);
- var spanArr = sheet.getSpans(range, GC.Spread.Sheets.SheetArea.colHeader);
- if(spanArr.length > 0) {
- console.log(spanArr[0]);
- var relRow = spanArr[0].row;
- var relCol = spanArr[0].col;
- var relValue = sheet.getValue(relRow, relCol, GC.Spread.Sheets.SheetArea.colHeader);
- alert("选择列头单元格的值为" + relValue);
- } else {
- var value = sheet.getValue(row, col, GC.Spread.Sheets.SheetArea.colHeader);
- alert("选择列头单元格的值为" + value);
- }
- }
- };
- var contextMenu = new MyContextMenu();
- spread.contextMenu = contextMenu;
复制代码 完整代码请查看附件demo
|
|