将多个字体颜色应用于单个Google表格单元格中的文本 [英] Apply multiple font colors to the text in a single Google Sheets cell

查看:100
本文介绍了将多个字体颜色应用于单个Google表格单元格中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google Apps脚本中的函数将单元格的格式设置为具有多种字体颜色.我找不到任何文档.另外,使用getFontColor()不会返回任何有用的信息.

I am trying to format a cell to have multiple font colors using a function in Google Apps Script. I am unable to find any documentation on it. Also, using getFontColor() doesn't return anything useful.

有没有办法以编程方式重现此功能

Is there any way to programmatically reproduce this functionality

可以通过Google Sheets网络用户界面使用的用户吗?

that is available to users via the Google Sheets web UI?

推荐答案

Sheets API 开始使用时有点令人生畏,但可以对电子表格进行非常细粒度的控制.您必须启用,因为它是高级服务".我强烈建议您查看示例代码实验室.

The Sheets API is a bit daunting to start using, but allows very fine-grained control over your spreadsheets. You'll have to enable it, as it is an "Advanced Service". I strongly recommend reviewing the Sample Codelab.

使用Sheets API, TextFormatRun

With the Sheets API, the TextFormatRun property can be manipulated on a cell-by-cell basis. Note:

应用于单元格各部分的富文本运行.运行仅对用户输入的字符串有效,对公式,布尔值或数字无效.运行从文本中的特定索引开始,一直持续到下一次运行.除非在随后的运行中明确更改,否则运行的属性将继续(除非第一次更改,否则第一次运行的属性将继续保留单元格的属性).

Runs of rich text applied to subsections of the cell. Runs are only valid on user entered strings, not formulas, bools, or numbers. Runs start at specific indexes in the text and continue until the next run. Properties of a run will continue unless explicitly changed in a subsequent run (and properties of the first run will continue the properties of the cell unless explicitly changed).

写入时,新运行将覆盖之前的所有运行.编写新的userEnteredValue时,以前的运行将被删除.

When writing, the new runs will overwrite any prior runs. When writing a new userEnteredValue, previous runs will be erased.

此示例使用它来调整文本的绿色值,该值在活动单元格中的字符串长度上从0增加到100%.调整以适合您的需求.

This example uses it to adjust the green value of text, increasing from 0 to 100% over the length of a string in the active cell. Adjust to suit your needs.

function textFormatter() {
  // Get the current cell's text.
  var wb = SpreadsheetApp.getActive(), sheet = wb.getActiveSheet();
  var cell = sheet.getActiveCell(), value = cell.getValue();
  var len = value.toString().length;
  if(len == 0) return;

  // Change the color every 2 characters.
  var newCellData = Sheets.newCellData();
  newCellData.textFormatRuns = [];
  var step = 1 / len;
  for(var c = 0; c < len; c += 2) {
    var newFmt = Sheets.newTextFormatRun();
    newFmt.startIndex = c;
    newFmt.format = Sheets.newTextFormat();
    newFmt.format.foregroundColor = Sheets.newColor();
    newFmt.format.foregroundColor.green = (c + 2) * step;
    newCellData.textFormatRuns.push(newFmt);
  }

  // Create the request object.
  var batchUpdateRQ = Sheets.newBatchUpdateSpreadsheetRequest();
  batchUpdateRQ.requests = [];
  batchUpdateRQ.requests.push(
    {
       "updateCells": {
        "rows": [ { "values": newCellData } ],
        "fields": "textFormatRuns",
        "start": {
          "sheetId": sheet.getSheetId(),
          "rowIndex": cell.getRow() - 1,
          "columnIndex": cell.getColumn() - 1
        }
      }
    }
  );
  Sheets.Spreadsheets.batchUpdate(batchUpdateRQ, wb.getId());
}


根据如何设置要格式化的单元格的值,可能还需要在同一请求中包括该单元格的值. 在问题跟踪器上查看此示例

这篇关于将多个字体颜色应用于单个Google表格单元格中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆