如何仅在 Google 工作表中为特定单元格更新前景色

How to update forecolor only in Google Sheets for specific cells

我需要更改某些单元格的文本颜色,同时使用表格 API v4 保留其余格式。 我按照 how-to-read/write-google-sheets-with-c#.

中单元格范围的前景色示例进行了跟踪
        if (foreColor != System.Drawing.Color.Empty)
        {
            var spreadsheet = await service.Spreadsheets.Get(_spreadsheetId).ExecuteAsync();
            var sheet = spreadsheet.Sheets.First(s => s.Properties.Title == worksheetTitle);
            int sheetId = sheet.Properties.SheetId ?? 0;

            var cellFormat = new CellFormat
            {
                TextFormat = new TextFormat
                {
                    ForegroundColor = new Color
                    {
                        Alpha = foreColor.A / 255f,
                        Red = foreColor.R / 255f,
                        Green = foreColor.G / 255f,
                        Blue = foreColor.B / 255f,
                    }
                }
            };

            CellData getFormatted() => new CellData { UserEnteredFormat = cellFormat };

            var request = new Request
            {
                UpdateCells = new UpdateCellsRequest
                {
                    Start = new GridCoordinate
                    {
                        SheetId = _sheetId,
                        ColumnIndex = startColumnIndex,
                        RowIndex = rowNumber - 1,
                    },
                    Fields = "*",
                    Rows = new List<RowData> { new RowData { 
                        Values = new List<CellData>
                        {
                            getFormatted(), //  details
                            getFormatted(), //  system type
                            getFormatted(), //  price
                            getFormatted(), //  primary cost
                            getFormatted(), //  secondary cost
                            getFormatted(), //  accepted
                        }
                    } },
                },
            };

            var requests = new BatchUpdateSpreadsheetRequest
            {
                ResponseIncludeGridData = true,
                Requests = new List<Request> { request },
            };

            var response = await service.Spreadsheets.BatchUpdate(requests, spreadsheet.SpreadsheetId).ExecuteAsync();
        }

上面代码的问题在于,虽然它确实改变了文本颜色,但它也清除了所有单元格值和现有格式 -(如边框、文本对齐等)。

有没有办法只更新单元格文本颜色?

先看一下,Fields = "*",会替换所有字段。由于您只想更新 foregroundColor,请尝试使用此代替:

Fields = "userEnteredFormat.textFormat.foregroundColor"

示例请求正文:

"requests": [
    {
      "repeatCell": {
        // A:B
        "range": {
          "sheetId": 0,
          "startColumnIndex": 0,
          "endColumnIndex": 2
        },
        // change font color to blue
        "cell": {
          "userEnteredFormat": {
            "textFormat": {
              "foregroundColor": {
                "blue": 1
              }
              // included bold for fields demonstration
              "bold": true
            }
          }
        },
        "fields": "userEnteredFormat.textFormat.foregroundColor"
      }
    }
  ]

请注意,我在 foregroundColor 下方添加了粗体。由于我们刚刚在字段中指定了 userEnteredFormat.textFormat.foregroundColor,它只会更新请求中的 foregroundColor(忽略粗体)

你的问题是,你使用了 *,因此替换了所有格式和值,完全删除了单元格值和格式,因为你的请求中没有其他字段的值。

样本:

输出:

您可以查看的示例参考资料: