请选择 进入手机版 | 继续访问电脑版
 找回密码
 立即注册

QQ登录

只需一步,快速开始

Dtttax

银牌会员

280

主题

691

帖子

2227

积分

银牌会员

积分
2227
Dtttax
银牌会员   /  发表于:2019-4-9 23:56  /   查看:3851  /  回复:7


如下图:想把叉放到单元格内,调了起始值后始终不会到单元格内,无论怎么加宽单元格都不行,图标的c1就可以。



代码如下:
1、画c1的代码
width = GSSCText.prototype.getAutoFitWidth.call(this, text, text, style, sheet.zoom(), context)
    GSSCText.prototype.paintContent.call(this, ctx, text, x, y-h/3, w, h, style, context)
    style.foreColor=color
    style.font=prefont
    return width

2、画叉的代码
width = GSSCText.prototype.getAutoFitWidth.call(this, text, text, style, sheet.zoom(), context)
    //const start = maxOffsets[3] + maxOffsets[4] > 0 ? x + width - maxOffsets[3] - maxOffsets[4] : x
   const start = x + maxOffsets[4]    //maxOffsets[4]  标识c1的宽度
    GSSCText.prototype.paintContent.call(this, ctx, text, start, y-h/3, w, h, style, context)

7 个回复

倒序浏览
dexteryao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2019-4-10 09:54:36
沙发
您重写的是那个方法?想在文字后面画个叉?
回复 使用道具 举报
Dtttax
银牌会员   /  发表于:2019-4-10 10:57:59
板凳


单元格内加叉,如图,重写的paintContent 方法

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 使用道具 举报
dexteryao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2019-4-10 16:10:31
地板
您好,看您的截图,应该就是真个绘制时候偏移量计算不对,你要加红X,前面所有内容都要往前移动,留出X的位置
回复 使用道具 举报
Dtttax
银牌会员   /  发表于:2019-4-10 16:31:06
5#
所有内容都要往前移动是怎么写的?

GSSCText.prototype.paintContent.call(this, ctx, text, x, y-h/3, w, h, style, context)

是这个x的位置么
回复 使用道具 举报
dexteryao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2019-4-10 16:49:59
6#
嗯,但是没有你的上下文,不清楚具体要怎么改,我重新谢了个Demo,你看下
  1. <!doctype html>
  2. <html>

  3. <head>
  4.         <title>上下标公式渲染</title>
  5.         <meta name="spreadjs culture" content="zh-cn" />
  6.         <meta charset="utf-8" />
  7.         <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  8.         <link rel="stylesheet" type="text/css" href="https://cdn.grapecity.com.cn/spreadjs/css/gc.spread.sheets.excel2013white.12.0.6.css">
  9.         <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
  10.         <script src="https://cdn.bootcss.com/FileSaver.js/2014-11-29/FileSaver.js"></script>
  11.         <script src="https://cdn.grapecity.com.cn/spreadjs/scripts/gc.spread.sheets.all.12.0.6.min.js" type="text/javascript"></script>
  12.         <script src="https://cdn.grapecity.com.cn/spreadjs/scripts/interop/gc.spread.excelio.12.0.6.min.js" type="text/javascript" type="text/javascript"></script>
  13.         <script src="https://cdn.grapecity.com.cn/spreadjs/scripts/plugins/gc.spread.sheets.charts.12.0.6.min.js" type="text/javascript" type="text/javascript"></script>
  14.         <script src="https://cdn.grapecity.com.cn/spreadjs/scripts/plugins/gc.spread.sheets.shapes.12.0.6.min.js" type="text/javascript" type="text/javascript"></script>
  15.         <script src="https://cdn.grapecity.com.cn/spreadjs/scripts/plugins/gc.spread.sheets.print.12.0.6.min.js" type="text/javascript" type="text/javascript"></script>
  16.         <script src="https://cdn.grapecity.com.cn/spreadjs/scripts/plugins/gc.spread.sheets.pdf.12.0.6.min.js" type="text/javascript" type="text/javascript"></script>
  17.         <script src="https://cdn.grapecity.com.cn/spreadjs/scripts/resources/zh/gc.spread.sheets.resources.zh.12.0.6.min.js" type="text/javascript"></script>



  18.         <script type="text/javascript">
  19.                 var spreadNS = GC.Spread.Sheets;
  20.                 var spread;
  21.                 $(document).ready(function () {
  22.                         spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {
  23.                                 sheetCount: 2
  24.                         });
  25.                         initSpread(spread);
  26.                 });

  27.                 //自定义单元格
  28.                 function ShowTagCellType() {
  29.                         this.typeName = "ShowTagCellType";
  30.                         this._cellTagStartCache = undefined
  31.                         this._cellTagEndCache = undefined
  32.                         this._textWidth = undefined
  33.                         this._textHeight = undefined
  34.                 }

  35.                 ShowTagCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  36.                 ShowTagCellType.prototype.paintContent = function (ctx, value, x, y, w, h, style, context) {
  37.                         var tag = context.sheet.getTag(context.row, context.col);
  38.                         if (tag == '' || tag == null || tag === undefined) {
  39.                                 this._cellTagStartCache = undefined
  40.                                 this._cellTagEndCache = undefined
  41.                                 this._textWidth = undefined
  42.                                 this._textHeight = undefined
  43.                                 GC.Spread.Sheets.CellTypes.Text.prototype.paintContent.call(this, ctx, value, x, y, w, h, style, context)
  44.                                 return;
  45.                         }
  46.                         this._cellTagStartCache = [], this._textWidth = 0;
  47.                         var startTextWidth = 0,
  48.                                 endTextWidth = 0;
  49.                         var sheet = context.sheet,
  50.                                 zoomFactor = sheet.zoom();
  51.                         var foreColor = style.foreColor,
  52.                                 textDecoration = style.textDecoration;

  53.                         //为了实现简单,单元格垂直居中,如果有其他需求,绘制文字位置重新计算
  54.                         style.vAlign = GC.Spread.Sheets.VerticalAlign.center;

  55.                         for (var i = 0; i < tag.cellTagStart.length; i++) {
  56.                                 var node = tag.cellTagStart[i];

  57.                                 if (node.type === 'text') {
  58.                                         style.foreColor = foreColor
  59.                                         style.textDecoration = textDecoration
  60.                                         var cellText = node.text;
  61.                                         GC.Spread.Sheets.CellTypes.Text.prototype.paintContent.call(this, ctx, cellText, x + startTextWidth, y, w - startTextWidth, h, style, context)
  62.                                         var textWidth = GC.Spread.Sheets.CellTypes.Text.prototype.getAutoFitWidth.call(this, cellText, cellText, style, zoomFactor, context)
  63.                                         startTextWidth += textWidth
  64.                                 } else if (node.type === 'link') {
  65.                                         style.foreColor = "blue"
  66.                                         style.textDecoration = GC.Spread.Sheets.TextDecorationType.underline;
  67.                                         var linkText = node.link;
  68.                                         if (node.isCustomName) {
  69.                                                 linkText = this.getFormulaFromCustomerName(sheet, node.link);
  70.                                         }
  71.                                         GC.Spread.Sheets.CellTypes.Text.prototype.paintContent.call(this, ctx, linkText, x + startTextWidth + 2, y, w - startTextWidth, h, style, context)
  72.                                         var textWidth = GC.Spread.Sheets.CellTypes.Text.prototype.getAutoFitWidth.call(this, linkText, linkText, style, zoomFactor, context)

  73.                                         this._cellTagStartCache[i] = {
  74.                                                 startX: x + startTextWidth,
  75.                                                 textWidth: textWidth + 3,
  76.                                                 formula: linkText
  77.                                         }
  78.                                         startTextWidth += (textWidth + 3)
  79.                                 }
  80.                         }
  81.                         this._textWidth += startTextWidth;

  82.                         // Set Font to default
  83.                         style.foreColor = foreColor
  84.                         style.textDecoration = textDecoration

  85.                         if (tag.cellTagEnd) {
  86.                                 var supTextNode = tag.cellTagEnd.supText,
  87.                                         subTextNode = tag.cellTagEnd.subText;
  88.                                 var supTextWidth = 0,
  89.                                         subTextWidth = 0;
  90.                                 if (supTextNode && supTextNode.length > 0) {
  91.                                         style.hAlign = GC.Spread.Sheets.HorizontalAlign.center;
  92.                                         style.vAlign = GC.Spread.Sheets.VerticalAlign.bottom;
  93.                                         for (var i = supTextNode.length - 1; i >= 0; i--) {
  94.                                                 var node = supTextNode[i];
  95.                                                 style.foreColor = node.color || foreColor;
  96.                                                 // style.textDecoration = textDecoration
  97.                                                 var cellText = node.text;
  98.                                                 var textWidth = GC.Spread.Sheets.CellTypes.Text.prototype.getAutoFitWidth.call(this, cellText, cellText, style, zoomFactor, context)
  99.                                                 GC.Spread.Sheets.CellTypes.Text.prototype.paintContent.call(this, ctx, cellText, x + w - textWidth - supTextWidth - 5, y, textWidth + 5, h / 2, style, context)
  100.                                                
  101.                                                 supTextWidth += (textWidth + 3)
  102.                                         }
  103.                                 }
  104.                                 if (subTextNode && subTextNode.length > 0) {
  105.                                         style.hAlign = GC.Spread.Sheets.HorizontalAlign.center;
  106.                                         style.vAlign = GC.Spread.Sheets.VerticalAlign.top;
  107.                                         for (var i = subTextNode.length - 1; i >= 0; i--) {
  108.                                                 var node = subTextNode[i];
  109.                                                 style.foreColor = node.color || foreColor;
  110.                                                 // style.textDecoration = textDecoration
  111.                                                 var cellText = node.text;
  112.                                                 var textWidth = GC.Spread.Sheets.CellTypes.Text.prototype.getAutoFitWidth.call(this, cellText, cellText, style, zoomFactor, context)
  113.                                                 GC.Spread.Sheets.CellTypes.Text.prototype.paintContent.call(this, ctx, cellText, x + w - textWidth - subTextWidth - 5, y + h / 2, textWidth + 5, h / 2, style, context)
  114.                                                
  115.                                                 subTextWidth += (textWidth + 3)
  116.                                         }
  117.                                 }

  118.                                 endTextWidth = supTextWidth > subTextWidth ? supTextWidth : subTextWidth;

  119.                                 var textHeight = GC.Spread.Sheets.CellTypes.Text.prototype.getAutoFitHeight.call(this, "A", "A", style, zoomFactor, context)
  120.                                 this._textHeight = textHeight * 2

  121.                                 // var supText = tag.cellTagEnd.supText, subText = tag.cellTagEnd.subText;
  122.                                 // var supTextWidth = GC.Spread.Sheets.CellTypes.Text.prototype.getAutoFitWidth.call(this, supText, supText, style, zoomFactor, context)
  123.                                 // var subTextWidth = GC.Spread.Sheets.CellTypes.Text.prototype.getAutoFitWidth.call(this, subText, subText, style, zoomFactor, context)

  124.                                 // endTextWidth = supTextWidth > subTextWidth ? supTextWidth : subTextWidth;

  125.                                 // var textHeight = GC.Spread.Sheets.CellTypes.Text.prototype.getAutoFitHeight.call(this, supText, supText, style, zoomFactor, context)
  126.                                 // this._textHeight = textHeight * 2

  127.                                 // style.hAlign = GC.Spread.Sheets.HorizontalAlign.center;
  128.                                 // style.vAlign = GC.Spread.Sheets.VerticalAlign.bottom;
  129.                                 // GC.Spread.Sheets.CellTypes.Text.prototype.paintContent.call(this, ctx, supText, x + w - endTextWidth - 5, y, endTextWidth + 5, h / 2, style, context)

  130.                                 // style.vAlign = GC.Spread.Sheets.VerticalAlign.top;
  131.                                 // GC.Spread.Sheets.CellTypes.Text.prototype.paintContent.call(this, ctx, subText, x + w - endTextWidth - 5, y + h / 2, endTextWidth + 5, h / 2, style, context)
  132.                         }
  133.                         this._textWidth += endTextWidth;

  134.                         // Set Font to default
  135.                         style.foreColor = foreColor
  136.                         style.textDecoration = textDecoration

  137.                         //Paint Value
  138.                         style.font = "bold " + style.font
  139.                         style.hAlign = GC.Spread.Sheets.HorizontalAlign.right;
  140.                         style.vAlign = GC.Spread.Sheets.VerticalAlign.center;
  141.                         GC.Spread.Sheets.CellTypes.Text.prototype.paintContent.call(this, ctx, value, x, y, w - endTextWidth - 3, h, style, context)

  142.                 };

  143.                 ShowTagCellType.prototype.getFormulaFromCustomerName = function (sheet, customName) {
  144.                         var nameInfo = sheet.getCustomName(customName);
  145.                         var expression = nameInfo.getExpression();
  146.                         var expStr = GC.Spread.Sheets.CalcEngine.expressionToFormula(sheet, expression, 0, 0);
  147.                         var range = GC.Spread.Sheets.CalcEngine.formulaToRange(sheet, expStr, 0, 0);
  148.                         expStr = GC.Spread.Sheets.CalcEngine.rangeToFormula(range, 0, 0, GC.Spread.Sheets.CalcEngine.RangeReferenceRelative.allRelative, false)
  149.                         return expStr;
  150.                 }

  151.                 ShowTagCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  152.                         var info = {
  153.                                 x: x,
  154.                                 y: y,
  155.                                 row: context.row,
  156.                                 col: context.col,
  157.                                 cellRect: cellRect,
  158.                                 sheetArea: context.sheetArea,
  159.                                 isReservedLocation: false,
  160.                                 reservedLocation: -1
  161.                         };

  162.                         if (y > cellRect.y + (cellRect.height - this._textHeight) / 2 && y < cellRect.y + cellRect.height - (cellRect.height - this._textHeight) / 2)

  163.                                 for (var i = 0; i < this._cellTagStartCache.length; i++) {
  164.                                         var item = this._cellTagStartCache[i];
  165.                                         if (item) {
  166.                                                 var startX = item.startX;
  167.                                                 if (x - startX > 0 && x < startX + item.textWidth) {
  168.                                                         info.isReservedLocation = true;
  169.                                                         info.reservedLocation = i;
  170.                                                         break;
  171.                                                 }
  172.                                         }
  173.                                 }

  174.                         return info;
  175.                 }
  176.                 ShowTagCellType.prototype.processMouseUp = function (hitInfo) {
  177.                         var sheet = hitInfo.sheet,
  178.                                 self = this;
  179.                         if (sheet && hitInfo.isReservedLocation && hitInfo.reservedLocation >= 0) {
  180.                                 setTimeout(function () {
  181.                                         var expStr = self._cellTagStartCache[hitInfo.reservedLocation].formula;
  182.                                         var range = GC.Spread.Sheets.CalcEngine.formulaToRange(sheet, expStr, 0, 0);
  183.                                         sheet.setActiveCell(range.row, range.col);

  184.                                         var rec = sheet.getCellRect(range.row, range.col)
  185.                                         if (rec.x === undefined) {
  186.                                                 sheet.showCell(range.row, range.col, GC.Spread.Sheets.VerticalPosition.top, GC.Spread.Sheets.HorizontalPosition.left);
  187.                                         }
  188.                                 }, 10);
  189.                                 return true;
  190.                         }
  191.                         return false;
  192.                 };
  193.                 ShowTagCellType.prototype.processMouseMove = function (hitInfo) {
  194.                         var sheet = hitInfo.sheet;
  195.                         var div = sheet.getParent().getHost();
  196.                         var canvasId = div.id + "vp_vp";
  197.                         var canvas = $("#" + canvasId)[0];
  198.                         if (sheet && hitInfo.isReservedLocation) {
  199.                                 canvas.style.cursor = 'pointer';
  200.                                 return true;
  201.                         } else {
  202.                                 canvas.style.cursor = 'default';
  203.                         }
  204.                         return false;
  205.                 };

  206.                 ShowTagCellType.prototype.getAutoFitWidth = function (value, text, cellStyle, zoomFactor, context) {
  207.                         if (this._textWidth) {
  208.                                 cellStyle.font = "bold " + cellStyle.font
  209.                                 return 5 + this._textWidth + GC.Spread.Sheets.CellTypes.Text.prototype.getAutoFitWidth.call(this, value, text, cellStyle, zoomFactor, context);
  210.                         } else
  211.                                 return GC.Spread.Sheets.CellTypes.Text.prototype.getAutoFitWidth.call(this, value, text, cellStyle, zoomFactor, context)
  212.                 }
  213.                 ShowTagCellType.prototype.getAutoFitHeight = function (value, text, cellStyle, zoomFactor, context) {
  214.                         if (this._textHeight) {
  215.                                 return this._textHeight;
  216.                         } else {
  217.                                 return GC.Spread.Sheets.CellTypes.Text.prototype.getAutoFitHeight.call(this, value, text, cellStyle, zoomFactor, context)
  218.                         }
  219.                 }

  220.                 function getFormulaFromRowCol(row, col) {
  221.                         var range = new GC.Spread.Sheets.Range(row, col, 1, 1)
  222.                         return GC.Spread.Sheets.CalcEngine.rangeToFormula(range, 0, 0, GC.Spread.Sheets.CalcEngine.RangeReferenceRelative.allAbsolute, false)
  223.                 }

  224.                 function initSpread(spread) {


  225.                         var sheet = spread.getSheet(0);
  226.                         var a1 = getFormulaFromRowCol(0, 0)
  227.                         sheet.addCustomName('SourceCell', a1, 0, 0)

  228.                         sheet.suspendPaint();

  229.                         var style = new GC.Spread.Sheets.Style();

  230.                         style.font = "14px simkai";
  231.                         sheet.setStyle(-1, -1, style, GC.Spread.Sheets.SheetArea.viewport);

  232.                         var cell = sheet.getCell(1, 1);
  233.                         cell.cellType(new ShowTagCellType());
  234.                         cell.formatter('"$USA"#,##0.00_);("$USA"[red]#,##0.00)')
  235.                         // cell.value(-5000.02);
  236.                         cell.formula("SourceCell")
  237.                         sheet.setValue(0, 0, -5000.02)

  238.                         var cellTagStart = [{
  239.                                         type: 'text',
  240.                                         text: '(Text Mark)'
  241.                                 },
  242.                                 {
  243.                                         type: 'text',
  244.                                         text: '(Text11111)'
  245.                                 },
  246.                                 {
  247.                                         type: 'link',
  248.                                         isCustomName: true,
  249.                                         link: 'SourceCell'
  250.                                 },
  251.                                 {
  252.                                         type: 'link',
  253.                                         isCustomName: false,
  254.                                         link: 'B12'
  255.                                 },
  256.                                 {
  257.                                         type: 'link',
  258.                                         isCustomName: false,
  259.                                         link: 'C3'
  260.                                 },
  261.                         ];
  262.                         var cellTagEnd = {
  263.                                 supText: [{
  264.                                         type: 'text',
  265.                                         text: 'SUP'
  266.                                 }],
  267.                                 subText: [{
  268.                                         type: 'text',
  269.                                         text: 'SUB'
  270.                                 }]
  271.                         }

  272.                         cell.tag({
  273.                                 cellTagStart: cellTagStart,
  274.                                 cellTagEnd: cellTagEnd
  275.                         });




  276.                         var cell2 = sheet.getCell(2, 1);
  277.                         cell2.cellType(new ShowTagCellType());
  278.                         cell2.formatter('"$USA"#,##0.00_);("$USA"#,##0.00)')
  279.                         cell2.value(1000.03);

  280.                         var cellTagStart2 = [{
  281.                                         type: 'text',
  282.                                         text: '{Mark}'
  283.                                 },
  284.                                 {
  285.                                         type: 'text',
  286.                                         text: '{Text}'
  287.                                 },
  288.                                 {
  289.                                         type: 'text',
  290.                                         text: '{Text}'
  291.                                 },
  292.                                 {
  293.                                         type: 'link',
  294.                                         isCustomName: true,
  295.                                         link: 'SourceCell'
  296.                                 },
  297.                                 {
  298.                                         type: 'link',
  299.                                         isCustomName: false,
  300.                                         link: 'B22'
  301.                                 },
  302.                         ];
  303.                         var cellTagEnd2 = {
  304.                                 supText: [{
  305.                                                 type: 'text',
  306.                                                 color: 'red',
  307.                                                 text: 'A'
  308.                                         },
  309.                                         {
  310.                                                 type: 'text',
  311.                                                 text: 'B'
  312.                                         },
  313.                                         {
  314.                                                 type: 'text',
  315.                                                 text: 'C'
  316.                                         }
  317.                                 ],
  318.                                 subText: [{
  319.                                         type: 'text',
  320.                                         text: 'TRUE'
  321.                                 },{
  322.                                         type: 'text',
  323.                                         color: 'red',
  324.                                         text: 'X'
  325.                                 }]
  326.                         }

  327.                         cell2.tag({
  328.                                 cellTagStart: cellTagStart2,
  329.                                 cellTagEnd: cellTagEnd2
  330.                         });




  331.                         sheet.resumePaint();
  332.                         sheet.autoFitColumn(1);
  333.                         sheet.autoFitRow(1);
  334.                         sheet.autoFitRow(2);

  335.                 };

  336.                 /*code_end*/
  337.         </script>

  338. </head>

  339. <body>
  340.         <div class="sample-turtorial">
  341.                 <div id="ss" style="width:100%; height: 420px;border: 1px solid gray;"></div>
  342.         </div>
  343. </body>

  344. </html>
复制代码
回复 使用道具 举报
Dtttax
银牌会员   /  发表于:2019-4-10 21:37:19
7#
解决了,是要c1那个地方起始位置要改,
回复 使用道具 举报
KevinChen讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2019-4-15 09:53:37
8#
很高兴解决了您的问题,本帖结贴了~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部