【问题标题】:How can I change specific text color in Google Spreadsheets?如何更改 Google 电子表格中的特定文本颜色?
【发布时间】:2020-11-15 16:07:35
【问题描述】:

首先,对不起我的英语。英语不是我的第一语言。 我只是想更改 Google 电子表格中的特定文本颜色。

例子

“▶Mike:嘿,最近怎么样?”

在这句话中,我只想改变“▶Mike:”这一部分。 我不知道 Java 和编程语言发生了什么。 但我真的需要这个。请帮忙。 谢谢

【问题讨论】:

  • 虽然我不确定我是否能正确理解I have no idea what's going on in Java and programming language.,但当您想使用Java 实现目标时,使用Sheets API 怎么样? Ref 顺便说一句,我认为当你想显示你当前的脚本并在你的问题中添加你想使用的语言的标签时,它会帮助用户思考解决方案。

标签: google-sheets spreadsheet


【解决方案1】:

如果您想更改 Google 表格中单元格的颜色,可以使用以下两个选项之一:

1。使用表格 API

要更新文本的颜色,您必须使用spreadsheets.batchUpdate 方法。

请求

POST https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}:batchUpdate

身体

{
   "requests": [{
      "repeatCell": {
         "cell": {
            "userEnteredFormat": {
               "textFormat": {
                  "bold": true,
                  "italic": true,
                  "foregroundColor": {
                     "blue": 1.0,
                     "green": 0.0,
                     "red": 0.0
                  }
               }
            }
         },
         "range": {
            "sheetId": 0,
            "startRowIndex": 0,
            "endRowIndex": 1
         },
         "fields": "userEnteredFormat(textFormat)"
      }
   }]
}

如果您想使用 Java,您可能会受益于查看来自 here 的这个 sn-p 并根据您的任务对其进行调整。

但是,此选项让您可以仅更改单元格中的部分文本。为此,您应该尝试使用 Apps 脚本。

2。使用 Apps 脚本

由于您想从单元格中更改特定数量的字符,您可以使用以下脚本

function changeColor() {
   var sheet = SpreadsheetApp.getActiveSheet();
   var range = sheet.getRange("A1:A2");
   var redColor = SpreadsheetApp.newTextStyle()
      .setForegroundColor("#ff0000")
      .build();
   var purpleColor = SpreadsheetApp.newTextStyle()
      .setForegroundColor("#871f78")
      .build();
   var richTextA1 = SpreadsheetApp.newRichTextValue()
      .setText("▶Mike: Hey, How is going on?")
      .setTextStyle(0, 6, redColor)
      .setTextStyle(7, 28, purpleColor)
      .build();
   var richTextA2 = SpreadsheetApp.newRichTextValue()
      .setText("red words, purple words")
      .setTextStyle(0, 10, redColor)
      .setTextStyle(11, 23, purpleColor)
      .build();
   range.setRichTextValues([
      [richTextA1],
      [richTextA2]
   ]);
}

上述脚本创建了两种文本样式,然后根据startOffsetendOffset 参数应用它们,这两个参数实质上表示哪些字符将具有两种文本样式中的一种。

执行上述脚本后,单元格将如下所示:

参考

【讨论】:

  • 非常感谢您的努力。非常感谢。但我在我的问题中遗漏了一些东西。示例:“▶Mike:嘿,你好吗?▶Caren:我很好。▶Mike:好的,再见。”所有文本都在一个单元格中。而我只想更改“▶Mike”、“▶Caren”这个文本。另外,我有很多这些细胞要改变。另外,比起电子表格,我更习惯 Microsoft Excel。这是我在 Excel 中使用的 VBA。我想在 Google 电子表格中使用此命令。
  • Sub WordColor() Dim cell As Range, word As String, startIndex As Integer word = InputBox(Prompt:="Write down the word", Title:="change color of text") If Len (word) > 0 Then For Each cell In Selection startIndex = InStr(1, cell, word, vbTextCompare) If startIndex > 0 Then cell.Characters(startIndex, Len(word)).Font.Color = RGB(0, 0, 255) cell.Characters(startIndex, Len(word)).Font.Bold = True End If Next cell End If End Sub 再次感谢
  • 您必须更改 setTextStyle 中的 startOffsetendOffset 参数,以匹配每个人姓名的开头和结尾。通过这种方式,他们的名字将被不同地着色。请看一下var richTextA1 - .setTextStyle(0, 6, redColor) 用于为“▶Mike:”着色,因此 0 和 6 索引,setTextStyle(7, 28, purpleColor) 用于为单元格中的其余文本着色。如果您有更多单词,则必须调整这些索引以匹配需要具有不同颜色的单词的开头和结尾。
猜你喜欢
  • 2014-10-02
  • 1970-01-01
  • 2015-02-28
  • 2014-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-02
相关资源
最近更新 更多