如何在C#中更改google sheet api v4中的单元格字体?

时间:2016-12-01 06:37:57

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

有可能吗? 我可以通过更新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();

2 个答案:

答案 0 :(得分:0)

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();

答案 1 :(得分:0)

您需要使用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表格API v4 Golang