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

QQ登录

只需一步,快速开始

旺仔老博文

注册会员

11

主题

32

帖子

121

积分

注册会员

积分
121
旺仔老博文
注册会员   /  发表于:2025-1-16 13:24  /   查看:75  /  回复:3
1金币
设置单元格某个字符的颜色,比如我只想设置星号为红色该怎么设置
  1. export function signRequiredStar(sheet: GC.Spread.Sheets.Worksheet) {
  2.   const rowCount = sheet.getRowCount() // 获取总行数

  3.   // 遍历第一列,检查行名是否包含 'OTP' 或 'OTC'
  4.   for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
  5.     const cellValue = sheet.getValue(rowIndex, 0) // 获取第一列的值(行名)

  6.     // 如果行名包含 'OTP' 或 'OTC'
  7.     if (cellValue && (cellValue.includes('text') || cellValue.includes('pro'))) {
  8.       const cell = sheet.getCell(rowIndex, 0) // 获取单元格对象
  9.       cell.text(`*${cellValue} `) // 在行名后添加红色星号
  10.       cell.foreColor('red')
  11.     }
  12.   }
  13. }
复制代码


最佳答案

查看完整内容

可以采用富文本

3 个回复

倒序浏览
最佳答案
最佳答案
旺仔老博文
注册会员   /  发表于:2025-1-16 13:24:34
来自 2#
可以采用富文本
  1. signRequiredStar(sheet: GC.Spread.Sheets.Worksheet) {
  2.   const rowCount = sheet.getRowCount() // 获取总行数

  3.   // 遍历第一列,检查行名是否包含 'OTP' 或 'OTC'
  4.   for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
  5.     const cellValue = sheet.getValue(rowIndex, 0) // 获取第一列的值(行名)

  6.     // 如果行名包含 'OTP' 或 'OTC'
  7.     if (cellValue && (cellValue.includes('test') || cellValue.includes('pro'))) {
  8.       // 创建富文本对象并设置星号部分为红色
  9.       const richText = [
  10.         {
  11.           text: cellValue.substring(0, cellValue.indexOf('*')), // 星号之前的文本
  12.         },
  13.         {
  14.           style: { foreColor: 'red' },
  15.           text: '*', // 红色星号
  16.         },
  17.         {
  18.           text: cellValue.substring(cellValue.indexOf('*') + 1), // 星号之后的文本
  19.         },
  20.       ]

  21.       // 设置单元格的富文本
  22.       sheet.setValue(rowIndex, 0, { richText }, GC.Spread.Sheets.SheetArea.viewport)
  23.     }
  24.   }
  25. }
复制代码


评分

参与人数 1金币 +200 收起 理由
Lynn.Dou + 200 赞一个!

查看全部评分

回复 使用道具 举报
Lynn.Dou讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2025-1-17 10:10:50
3#
感谢分享方案,给您送上金币奖励。
回复 使用道具 举报
Wilson.Zhang
超级版主   /  发表于:7 天前
4#
您好!从跟帖回复中了解到问题已得到有效解答,那就结帖了。如有新问题,欢迎发新帖沟通。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部