找回密码
 立即注册

QQ登录

只需一步,快速开始

金牌服务用户

26

主题

93

帖子

243

积分

金牌服务用户

积分
243

金牌服务用户   /  发表于:2022-7-14 12:40  /   查看:4188  /  回复:12
本帖最后由 Derrick.Jiao 于 2022-8-1 09:44 编辑

上次的问题 spreadjs 自定义 cellType 问题https://gcdn.grapecity.com.cn/fo ... 99307&fromuid=63067
(出处: 葡萄城产品技术社区)

针对spreadjs 换行做了cellType 更换,但是者只针对图通文本,如果要修改富文本的格式要怎么处理

12 个回复

倒序浏览
Derrick.Jiao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2022-7-14 14:52:43
沙发
你好,富文本换行可以参考附件的demo通过/n/r换行。若仍未符合需求,可以提供对应demo以及描述详细需求,这边来做详细调研。

本帖子中包含更多资源

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

x
回复 使用道具 举报

金牌服务用户   /  发表于:2022-7-14 17:28:54
板凳
Derrick.Jiao 发表于 2022-7-14 14:52
你好,富文本换行可以参考附件的demo通过/n/r换行。若仍未符合需求,可以提供对应demo以及描述详细需求,这 ...
  1. /******************************** 单元格文本换行,重写 Tex 的 paint 方法 ********************************/
  2. function AutoWrapTextCellType() {
  3. }

  4. AutoWrapTextCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  5. AutoWrapTextCellType.prototype.paint = function (ctx, value, x, y, w, h, style, context) {
  6.     ctx.font = style.font;
  7.     value = wrapString(ctx, value, w - 2);
  8.     GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  9. };
  10. function wrapString(c, str, maxWidth) {
  11.     if(typeof str == 'object' && str['richText']){
  12.         return wrapRichTextString(c, str, maxWidth);
  13.     }
  14.     return wrapTextString(c, str, maxWidth)
  15. }

  16. /**
  17. * 对普通文本进行换行
  18. * @param c
  19. * @param str
  20. * @param maxWidth
  21. * @returns {string|*}
  22. */
  23. function wrapTextString(c, str, maxWidth){
  24.     let width = c.measureText(str).width;
  25.     if (width <= maxWidth) return str;
  26.     const len = str.length;
  27.     let newStrList = [];
  28.     let index = 0;
  29.     for (let i = 0; i < len; i++) {
  30.         // 临时拼接字符串
  31.         let _newStr = newStrList[index] + str[i];
  32.         // 临时拼接字符串的长度
  33.         let newWidth = c.measureText(_newStr).width;
  34.         // 初始化数组
  35.         if (!newStrList[index]) newStrList[index] = '';
  36.         // 长度判断
  37.         if (newWidth >= maxWidth) { // 长度超过,换行
  38.             newStrList[index + 1] = newStrList[index].substr(newStrList[index].length - 1) + str[i];
  39.             newStrList[index] = newStrList[index].substr(0, newStrList[index].length - 1);
  40.             index++;
  41.         } else {
  42.             newStrList[index] = newStrList[index] + str[i];
  43.         }
  44.     }
  45.     return newStrList.join('\r\n');
  46. }

  47. /**
  48. * 对富文本进行换行
  49. * @param c
  50. * @param str
  51. * @param maxWidth
  52. * @returns {*}
  53. */
  54. function wrapRichTextString(c, str, maxWidth){
  55.     const richText = str['richText'];
  56.     const newRichText = [];
  57.     // 之前部分遗留的字符串
  58.     let prevStr = '';
  59.     // 需要测量长度的字符串
  60.     let tmpStr = '';
  61.     let index = 0;
  62.     for (let i = 0; i < richText.length; i++) {
  63.         const text = richText[i]['text'];
  64.         const style = richText[i]['style'];
  65.         const len = text.length;
  66.         for (let j = 0; j < len; j++) {
  67.             // 临时拼接字符串
  68.             let _tmpStr = tmpStr + text[j];
  69.             // 临时拼接字符串的长度 上个部分遗留的+当前拼接的
  70.             let newWidth = c.measureText(prevStr+_tmpStr).width;
  71.             // 长度判断
  72.             if (newWidth >= maxWidth) { // 长度超过,换行
  73.                 newRichText[index] = {
  74.                     text : tmpStr,
  75.                     style: style,
  76.                 }
  77.                 index++;
  78.                 newRichText[index] = {
  79.                     text : '\r\n',
  80.                     style: style,
  81.                 }
  82.                 index++;
  83.                 // 记录当前字符,并将之前部分的记录清空
  84.                 tmpStr = text[j];
  85.                 prevStr = '';
  86.             } else {
  87.                 tmpStr = _tmpStr;
  88.             }
  89.         }
  90.         // 每一部分结束后,将剩余的该部分单独组成一个完成的部分,并记录到prevStr
  91.         newRichText[index] = {
  92.             text : tmpStr,
  93.             style: style,
  94.         }
  95.         index++;
  96.         prevStr = prevStr + tmpStr;
  97.         tmpStr = '';
  98.     }
  99.     str['richText'] = newRichText
  100.     return str;
  101. }
复制代码

这是换行的cellType,spreadjs并不识别
回复 使用道具 举报

金牌服务用户   /  发表于:2022-7-14 17:29:25
地板
Derrick.Jiao 发表于 2022-7-14 14:52
你好,富文本换行可以参考附件的demo通过/n/r换行。若仍未符合需求,可以提供对应demo以及描述详细需求,这 ...
  1. {"richText":[{"text":"该水样所检项目中肉眼可见物、耗氧量(COD","style":{"font":"14.7px 宋体","foreColor":"Text 1 0","textDecoration":0}},{"text":"Mn","style":{"vertAlign":2,"font":"14.7px 宋体","foreColor":"Text 1 0","textDecoration":0}},{"text":"法,以O","style":{"font":"14.7px 宋体","foreColor":"Text 1 0","textDecoration":0}},{"text":"2","style":{"vertAlign":2,"font":"14.7px 宋体","foreColor":"Text 1 0","textDecoration":0}},{"text":"计)、总大肠菌群、菌落总数、浑浊度检测结果超《生活饮用水卫生标准》(GB 5749-2006)标准要求,其它项目检测结果均符合标准要求。","style":{"font":"14.7px 宋体","foreColor":"Text 1 0","textDecoration":0}}],"text":"该水样所检项目中肉眼可见物、耗氧量(CODMn法,以O2计)、总大肠菌群、菌落总数、浑浊度检测结果超《生活饮用水卫生标准》(GB 5749-2006)标准要求,其它项目检测结果均符合标准要求。"}
复制代码

这是转换之前的richText
回复 使用道具 举报

金牌服务用户   /  发表于:2022-7-14 17:29:51
5#
Derrick.Jiao 发表于 2022-7-14 14:52
你好,富文本换行可以参考附件的demo通过/n/r换行。若仍未符合需求,可以提供对应demo以及描述详细需求,这 ...
  1. {"richText":[{"text":"该水样所检项目中肉眼可见物、耗氧量(COD","style":{"font":"14.7px 宋体","foreColor":"Text 1 0","textDecoration":0}},{"text":"Mn","style":{"vertAlign":2,"font":"14.7px 宋体","foreColor":"Text 1 0","textDecoration":0}},{"text":"法,以O","style":{"font":"14.7px 宋体","foreColor":"Text 1 0","textDecoration":0}},{"text":"2","style":{"vertAlign":2,"font":"14.7px 宋体","foreColor":"Text 1 0","textDecoration":0}},{"text":"计)、总大肠菌群、菌落总数、浑浊度检","style":{"font":"14.7px 宋体","foreColor":"Text 1 0","textDecoration":0}},{"text":"\r\n","style":{"font":"14.7px 宋体","foreColor":"Text 1 0","textDecoration":0}},{"text":"测结果超《生活饮用水卫生标准》(GB 5749-2006)标准要求,其它项目检测结果均符合标准要求","style":{"font":"14.7px 宋体","foreColor":"Text 1 0","textDecoration":0}},{"text":"\r\n","style":{"font":"14.7px 宋体","foreColor":"Text 1 0","textDecoration":0}},{"text":"。","style":{"font":"14.7px 宋体","foreColor":"Text 1 0","textDecoration":0}}],"text":"该水样所检项目中肉眼可见物、耗氧量(CODMn法,以O2计)、总大肠菌群、菌落总数、浑浊度检测结果超《生活饮用水卫生标准》(GB 5749-2006)标准要求,其它项目检测结果均符合标准要求。"}
复制代码

这是转换之后的richText
回复 使用道具 举报

金牌服务用户   /  发表于:2022-7-14 17:30:42
6#
Derrick.Jiao 发表于 2022-7-14 14:52
你好,富文本换行可以参考附件的demo通过/n/r换行。若仍未符合需求,可以提供对应demo以及描述详细需求,这 ...


最终效果并没有变化

本帖子中包含更多资源

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

x
回复 使用道具 举报
Derrick.Jiao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2022-7-14 18:14:16
7#
浩 发表于 2022-7-14 17:30
最终效果并没有变化

这是我测试的效果,在添加“/n/r”处实现了换行。

本帖子中包含更多资源

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

x
回复 使用道具 举报

金牌服务用户   /  发表于:2022-7-21 11:29:59
8#
Derrick.Jiao 发表于 2022-7-14 18:14
这是我测试的效果,在添加“/n/r”处实现了换行。

直接写入是可以的,但是我这里还使用了自定义单元格类型,重写了paint方法,兼容自动换行
  1. function AutoWrapTextCellType() {
  2. }

  3. AutoWrapTextCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  4. AutoWrapTextCellType.prototype.paint = function (ctx, value, x, y, w, h, style, context) {
  5.     ctx.font = style.font;
  6.     value = wrapString(ctx, value, w - 2);
  7.     GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  8. };
  9. function wrapString(c, str, maxWidth) {
  10.     if($.common.isNotEmpty(str) && typeof str == 'object' && str['richText']){
  11.         // return wrapRichTextString(c, str, maxWidth);
  12.         return str;
  13.     }
  14.     return wrapTextString(c, str, maxWidth)
  15. }

  16. /**
  17. * 对普通文本进行换行
  18. * @param c
  19. * @param str
  20. * @param maxWidth
  21. * @returns {string|*}
  22. */
  23. function wrapTextString(c, str, maxWidth){
  24.     let width = c.measureText(str).width;
  25.     if (width <= maxWidth) return str;
  26.     const len = str.length;
  27.     let newStrList = [];
  28.     let index = 0;
  29.     for (let i = 0; i < len; i++) {
  30.         // 临时拼接字符串
  31.         let _newStr = newStrList[index] + str[i];
  32.         // 临时拼接字符串的长度
  33.         let newWidth = c.measureText(_newStr).width;
  34.         // 初始化数组
  35.         if (!newStrList[index]) newStrList[index] = '';
  36.         // 长度判断
  37.         if (newWidth >= maxWidth) { // 长度超过,换行
  38.             newStrList[index + 1] = newStrList[index].substr(newStrList[index].length - 1) + str[i];
  39.             newStrList[index] = newStrList[index].substr(0, newStrList[index].length - 1);
  40.             index++;
  41.         } else {
  42.             newStrList[index] = newStrList[index] + str[i];
  43.         }
  44.     }
  45.     return newStrList.join('\r\n');
  46. }

  47. /**
  48. * 对富文本进行换行
  49. * @param c
  50. * @param str
  51. * @param maxWidth
  52. * @returns {*}
  53. */
  54. function wrapRichTextString(c, str, maxWidth){
  55.     const richText = str['richText'];
  56.     const newRichText = [];
  57.     // 之前部分遗留的字符串
  58.     let prevStr = '';
  59.     // 需要测量长度的字符串
  60.     let tmpStr = '';
  61.     let index = 0;
  62.     for (let i = 0; i < richText.length; i++) {
  63.         const text = richText[i]['text'];
  64.         const style = richText[i]['style'];
  65.         const len = text.length;
  66.         for (let j = 0; j < len; j++) {
  67.             // 临时拼接字符串
  68.             let _tmpStr = tmpStr + text[j];
  69.             // 临时拼接字符串的长度 上个部分遗留的+当前拼接的
  70.             let newWidth = c.measureText(prevStr+_tmpStr).width;
  71.             // 长度判断
  72.             if (newWidth >= maxWidth) { // 长度超过,换行
  73.                 newRichText[index] = {
  74.                     text : tmpStr,
  75.                     style: style,
  76.                 }
  77.                 index++;
  78.                 newRichText[index] = {
  79.                     text : '\r\n',
  80.                     style: style,
  81.                 }
  82.                 index++;
  83.                 // 记录当前字符,并将之前部分的记录清空
  84.                 tmpStr = text[j];
  85.                 prevStr = '';
  86.             } else {
  87.                 tmpStr = _tmpStr;
  88.             }
  89.         }
  90.         // 每一部分结束后,将剩余的该部分单独组成一个完成的部分,并记录到prevStr
  91.         newRichText[index] = {
  92.             text : tmpStr,
  93.             style: style,
  94.         }
  95.         index++;
  96.         prevStr = prevStr + tmpStr;
  97.         tmpStr = '';
  98.     }
  99.     str['richText'] = newRichText
  100.     console.log(JSON.stringify(str))
  101.     return str;
  102. }
复制代码
回复 使用道具 举报
Derrick.Jiao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2022-7-21 17:46:02
9#
浩 发表于 2022-7-21 11:29
直接写入是可以的,但是我这里还使用了自定义单元格类型,重写了paint方法,兼容自动换行

由于上面提供的代码这边无法直接复现,请在附件的demo中复现此问题。并且,详细描述想要实现的效果,也就是具体需要在哪个位置进行换行。这样我们这边就可以直接根据需求来调整提供过来的demo。

本帖子中包含更多资源

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

x
回复 使用道具 举报

金牌服务用户   /  发表于:2022-7-21 19:36:00
10#
Derrick.Jiao 发表于 2022-7-21 17:46
由于上面提供的代码这边无法直接复现,请在附件的demo中复现此问题。并且,详细描述想要实现的效果,也就 ...

这是demo,可以发现换行位置和console输出的换行位置完全不一致

本帖子中包含更多资源

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

x
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 立即注册
返回顶部