【问题标题】:How do I change a cell font in google sheets api v4 in C#?如何在 C# 中更改 google sheet api v4 中的单元格字体?
【发布时间】:2016-12-01 06:37:56
【问题描述】:

有可能吗? 我可以像上面的代码一样通过更新 ValueRange 对象来更改值,但找不到更改格式的方法。

  ValueRange l_oValueRange = new ValueRange();
  List<object> l_olCellsNewValue = new List<object>() { DateTime.UtcNow.ToString("dd'/'MM'/'yyyy HH:mm:ss") };
  l_oValueRange.Values = new List<IList<object>> { l_olCellsNewValue };

  SpreadsheetsResource.ValuesResource.UpdateRequest l_oUpdate = service.Spreadsheets.Values.Update(
                                                                l_oValueRange,
                                                                spreadsheetId,
                                                                "A60");
  l_oUpdate.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.RAW;
  UpdateValuesResponse l_oResponse = l_oUpdate.Execute();

【问题讨论】:

  • 如果您检查 Sheets API 的 documentation,您可以对单元格字体执行的唯一操作就是将其设为 BOLD 和 italicize我>。如需更多信息,请查看conditional formatting,了解 Sheets API 中唯一可用的格式。

标签: c# google-sheets google-sheets-api


【解决方案1】:
string spreadsheetId = "1DD3zfGe6.......UtENHhnBwz0CA";

//get sheet id by sheet name
Spreadsheet spr = service.Spreadsheets.Get(spreadsheetId).Execute();
Sheet sh = spr.Sheets.Where(s => s.Properties.Title == sheetName).FirstOrDefault();
int sheetId = (int)sh.Properties.SheetId;

//define cell color
var userEnteredFormat = new CellFormat()
{
    BackgroundColor = new Color()
    {
        Blue = 0,
        Red = 1,
        Green = (float)0.5,
        Alpha = (float)0.1
    },
    TextFormat = new TextFormat()
    {
        Bold = true,
        FontFamily = "your font family",
        FontSize = 12
    }
};
BatchUpdateSpreadsheetRequest bussr = new BatchUpdateSpreadsheetRequest();

//create the update request for cells from the first row
var updateCellsRequest = new Request()
{
    RepeatCell = new RepeatCellRequest()
    {
        Range = new GridRange()
        {
            SheetId = sheetId,
            StartColumnIndex = 0,
            StartRowIndex = 0,
            EndColumnIndex = 28,
            EndRowIndex = 1
        },
        Cell = new CellData()
        {
            UserEnteredFormat = userEnteredFormat
        },
        Fields = "UserEnteredFormat(BackgroundColor,TextFormat)"
    }
};
bussr.Requests = new List<Request>();
bussr.Requests.Add(updateCellsRequest);            
bur = service.Spreadsheets.BatchUpdate(bussr, spreadsheetId);
bur.Execute();

【讨论】:

    【解决方案2】:

    您需要使用 BatchUpdate 进行其他更新(请求)(我正在使用 Golang):

    func (s *Settings) UpdateSheet(req *sheets.Request)(*sheets.BatchUpdateSpreadsheetResponse, error) {
        update := sheets.BatchUpdateSpreadsheetRequest{
          Requests: []*sheets.Request{req},
        }
        return s.service.Spreadsheets.BatchUpdate(s.spreadsheetId, &update).Context(s.ctx).Do()
    }
    

    请求必须包含FontFamily,文档不多但必须包含"arial,sans,sans-serif"这种格式的字符串:

    req := &sheets.Request{
        RepeatCell: &sheets.RepeatCellRequest{
           Range: &sheets.GridRange{
               SheetId:        sheetId,
               StartRowIndex:  startRow,
               EndRowIndex:    endRow,
               EndColumnIndex: endCol,
           },
           Cell: &sheets.CellData{
               UserEnteredFormat: &sheets.CellFormat{
                   TextFormat: &sheets.TextFormat{
                       FontFamily:      "arial,sans,sans-serif", // The font family
                   },
               },
           },
        },
    }
    

    https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#textformat

    TextFormat: {
      "foregroundColor": {
        object(Color)
      },
      "fontFamily": string,
      "fontSize": number,
      "bold": boolean,
      "italic": boolean,
      "strikethrough": boolean,
      "underline": boolean,
    }
    

    关键字:Google Sheets API v4 Golang

    【讨论】:

    • 关于我能找到的关于 golang 风格的唯一文档。添加关键字 Google Sheets API v4 Golang,希望其他人更容易在 google 中找到它
    • 您如何准确确定要使用的字体系列?我试图简单地粘贴字体名称并粘贴在 Google Sheets CSS 中定义的字体系列。但是没有效果
    • 我发现您只能使用Google Fonts Repository 或fonts.google.com 中存在的字体。如果它仍然不起作用,您可以从其他 Google API 中找到有关 FamilyFont 问题的更多信息,例如这个:stackoverflow.com/questions/2455020/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多