本帖最后由 宛若无垠 于 2021-10-18 17:04 编辑
- <head>
- <meta charset="UTF-8" />
- <link
- rel="stylesheet" type="text/css" />
- <script src="http://cdn.grapecity.com/spreadjs/hosted/scripts/gc.spread.sheets.all.14.1.6.min.js"></script>
- <script src="http://cdn.grapecity.com/spreadjs/hosted/scripts/interop/gc.spread.excelio.14.1.6.min.js"></script>
- <script src="https://cdn.grapecity.com.cn/spreadjs/scripts/jquery-1.11.1.min.js" type="text/javascript"></script>
- <style>
- .sample-tutorial {
- position: relative;
- height: 100%;
- overflow: hidden;
- }
- .sample-spreadsheets {
- width: calc(100% - 280px);
- height: 100%;
- overflow: hidden;
- float: left;
- position: relative;
- }
- </style>
- </head>
- <body>
- <div id="ss" class="sample-spreadsheets"></div>
- <script>
- var spreadNS = GC.Spread.Sheets;
- $(document).ready(function () {
- var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 3 });
- var sheet = spread.getActiveSheet();
- spread['ExcelClient'] = {}
- spread['ExcelClient'].otherUserList = [
- {
- "userId": "1312312312",
- "selectList": [
- {
- "sheetName": "Sheet1",
- "row": 0,
- "col": 1
- }
- ],
- "rainbowIndex": 2
- }
- ]
- var combo = new spreadNS.CellTypes.ComboBox();
- combo.items([{ text: "Oranges", value: "11k" }, { text: "Apples", value: "15k" }, { text: "Grape", value: "100k" }])
- .editorValueType(spreadNS.CellTypes.EditorValueType.text);
- sheet.getCell(1, 2, spreadNS.SheetArea.viewport).cellType(combo).value("Apples");
- sheet.setValue(1, 1, "ComboBoxCellType");
- const msgCellType = sheet.getCellType(1, 2)
- const newCellType = cursorCellTypeCreator(msgCellType)
- sheet.setCellType(1, 2, newCellType)
- setTimeout(()=>{
- let msg = {
- "sheetIndex": 0,
- "sheetName": "Sheet1",
- "userId": "1231231231321",
- "userNick": "王**",
- "range": {
- "row": Math.floor(Math.random() * 10),
- "rowCount": 1,
- "col": Math.floor(Math.random() * 10),
- "colCount": 1
- },
- "type": "changeSelection",
- "msgId": "1231312313123",
- "agentFlag": "1231313213"
- }
- updateOtherSelection(msg,spread)
- },2000)
- });
-
- // 协作者光标单元格类型
- function cursorCellTypeCreator(cellType){
- if (cellType.typeName === 'CursorCellType') {
- return cellType
- }
- class CursorCellType extends cellType.constructor {
- getHitInfo(x, y, cellStyle, cellRect, context) {
- const info = { x: x, y: y, row: context.row, col: context.col, cellRect: cellRect, sheetArea: context.sheetArea }
- return info
- }
- processMouseEnter(hitInfo) {
- this.showName = true
- hitInfo.sheet.repaint()
- }
- processMouseLeave(hitInfo) {
- this.showName = false
- hitInfo.sheet.repaint()
- }
- paint(ctx, value, x, y, w, h, style, context) {
- if (ctx) {
- ctx.save()
- super.paint.apply(this, [ctx, value, x, y, w, h, style, context])
- // 绘制单元格矩形框开始
- ctx.strokeStyle = 'orange'
- ctx.lineWidth = 2
- ctx.strokeRect(x + 2, y + 2, w - 4, h - 4)
- if (this.showName) {
- ctx.fillStyle = 'red'
- ctx.font = '14px Arial'
- const textWidth = parseInt(ctx.measureText('hover效果').width) // 获取文字要占据的宽度
- const paintY = context.row === 0 ? y + h + 20 : y
- ctx.fillRect(x + w - (textWidth + 10), paintY - 20, textWidth + 10, 20)
- ctx.fillStyle = 'white'
- ctx.fillText('hover效果', x + w - 5, paintY - 5)
- }
- ctx.restore()
- }
- }
- }
- const newCellType = new CursorCellType()
- Object.assign(newCellType, cellType)
- return newCellType
- }
- // 处理协作者光标变化
- function updateOtherSelection(msg, spread) {
- const sheet = spread.getSheetFromName(msg.sheetName)
- if (sheet) {
- const row = msg.range.row < 0 ? 0 : msg.range.row
- const col = msg.range.col < 0 ? 0 : msg.range.col
- const otherUserList = spread.ExcelClient.otherUserList || []
- for (let i = 0; i < otherUserList.length; i++) {
- if (otherUserList[i].userId === msg.userId) {
- let oldCellType
- for (let j = 0; j < otherUserList[i].selectList.length; j++) {
- // 清除光标数据&清除光标单元格
- if (otherUserList[i].selectList[j].sheetName === msg.sheetName) {
- oldCellType = sheet.getCellType(otherUserList[i].selectList[j].row, otherUserList[i].selectList[j].col)
- if (oldCellType.typeName === 'CursorCellType') {
- for (let k = 0; k < oldCellType.userList.length; k++) {
- if (oldCellType.userList[k].userId === msg.userId) {
- oldCellType.userList.splice(k, 1)
- sheet.repaint(sheet.getCellRect(otherUserList[i].selectList[j].row, otherUserList[i].selectList[j].col, 1, 1))
- break
- }
- }
- }
- // 设置新的光标数据
- otherUserList[i].selectList[j].row = row
- otherUserList[i].selectList[j].col = col
- break
- }
- }
- // 设置新的光标数据
- if (!oldCellType) {
- otherUserList[i].selectList.push({sheetName: msg.sheetName, row: row, col: col})
- }
- // 设置新的光标单元格
- const msgCellType = sheet.getCellType(row, col)
- if (msgCellType.typeName === 'CursorCellType') {
- for (let m = 0; m < msgCellType.userList.length; m++) {
- if (msgCellType.userList[m].userId === msg.userId) {
- msgCellType.userList.splice(m, 1)
- break
- }
- }
- sheet.repaint(sheet.getCellRect(row, col, 1, 1))
- } else {
- const newCellType = cursorCellTypeCreator(msgCellType)
- sheet.setCellType(row, col, newCellType)
- }
- break
- }
- }
- }
- }
- </script>
- </body>
- </html>
复制代码 |