如何在.net中更改google sheet api v4中的范围颜色

时间:2016-07-13 09:52:03

标签: .net google-sheets-api

我尝试使用google sheet api v4修改VB.Net中的现有电子表格。 我使用BatchGetRequest和BatchUpdateRequest读取和写入文件:

        'Get File's Datas
        Dim reqBatchGet As SpreadsheetsResource.ValuesResource.BatchGetRequest = service.Spreadsheets.Values.BatchGet(fileId)
        reqBatchGet.Ranges = "'2016'!A1:AP60"
        Dim test = reqBatchGet.Execute()

        Dim myBatchUpdate As BatchUpdateValuesRequest = New BatchUpdateValuesRequest()
        myBatchUpdate.ValueInputOption = "RAW"
        myBatchUpdate.Data = New List(Of ValueRange)


        For Each res As GetDatasMonitoringAgencyResult In result

            Dim vrxN As ValueRange = New ValueRange()
            Dim lstN As IList(Of IList(Of Object)) = New List(Of IList(Of Object))

            lstN.Add(New List(Of Object)(New Object() {res.N01, res.N02, res.N03, res.N04, res.N05, res.N06, res.N07, res.N08, res.N09, res.N10, res.N11, res.N12}))

            For Each n In lstN

                For Each dataInN In n

                    If dataInN Is Nothing Then
                        dataInN = "-"
                    End If
                Next
            Next

            vrxN.Values = lstN

            Dim RangeN As String = String.Format("'{0}'!D{1}:O{1}", Now.Year, res.ligne)
            vrxN.Range = RangeN
            vrxN.MajorDimension = "ROWS"

            Dim vrxN_1 As ValueRange = New ValueRange()
            Dim lstN_1 As IList(Of IList(Of Object)) = New List(Of IList(Of Object))

            lstN_1.Add(New List(Of Object)(New Object() {res.N_01, res.N_02, res.N_03, res.N_04, res.N_05, res.N_06, res.N_07, res.N_08, res.N_09, res.N_10, res.N_11, res.N_12}))

            For Each n_1 In lstN_1

                For Each dataInN_1 In n_1

                    If dataInN_1 Is Nothing Then
                        dataInN_1 = ""
                    End If
                Next
            Next

            vrxN_1.Values = lstN_1
            Dim RangeN_1 As String = String.Format("'{0}'!AD{1}:AO{1}", Now.Year.ToString(), res.ligne)
            vrxN_1.Range = RangeN_1
            vrxN_1.MajorDimension = "ROWS"

            myBatchUpdate.Data.Add(vrxN)
            myBatchUpdate.Data.Add(vrxN_1)
        Next
        Dim reqBatchUpdate As SpreadsheetsResource.ValuesResource.BatchUpdateRequest = service.Spreadsheets.Values.BatchUpdate(myBatchUpdate, fileId)

        reqBatchUpdate.Execute()

现在,我想改变一些细胞的颜色。我尝试使用conditionalRule:

  Dim req As Request = New Request()
        Dim acfrr As AddConditionalFormatRuleRequest = New AddConditionalFormatRuleRequest()
        acfrr.Rule = New ConditionalFormatRule()
        Dim range1 As GridRange = New GridRange()
        range1.SheetId = 0
        range1.StartColumnIndex = 1
        range1.StartRowIndex = 1
        acfrr.Rule.Ranges = New List(Of GridRange)
        acfrr.Rule.Ranges.Add(range1)
        acfrr.Rule.GradientRule = New GradientRule()
        acfrr.Rule.GradientRule.Minpoint = New InterpolationPoint()
        acfrr.Rule.GradientRule.Minpoint.Type = "MIN"
        acfrr.Rule.GradientRule.Minpoint.Color = New Google.Apis.Sheets.v4.Data.Color()
        acfrr.Rule.GradientRule.Minpoint.Color.Red = 1
        acfrr.Rule.GradientRule.Minpoint.Color.Green = 0
        acfrr.Rule.GradientRule.Minpoint.Color.Blue = 0
        acfrr.Rule.GradientRule.Midpoint = New InterpolationPoint()
        acfrr.Rule.GradientRule.Midpoint.Type = "NUMBER"
        acfrr.Rule.GradientRule.Midpoint.Value = 0.5
        acfrr.Rule.GradientRule.Midpoint.Color = New Google.Apis.Sheets.v4.Data.Color()
        acfrr.Rule.GradientRule.Midpoint.Color.Red = 0
        acfrr.Rule.GradientRule.Midpoint.Color.Green = 1
        acfrr.Rule.GradientRule.Midpoint.Color.Blue = 0
        acfrr.Rule.GradientRule.Maxpoint = New InterpolationPoint()
        acfrr.Rule.GradientRule.Maxpoint.Type = "MAX"
        acfrr.Rule.GradientRule.Maxpoint.Color = New Google.Apis.Sheets.v4.Data.Color()
        acfrr.Rule.GradientRule.Maxpoint.Color.Red = 0
        acfrr.Rule.GradientRule.Maxpoint.Color.Green = 0
        acfrr.Rule.GradientRule.Maxpoint.Color.Blue = 1
        acfrr.Index = 0
        req.AddConditionalFormatRule = acfrr
        Dim testReq As BatchUpdateSpreadsheetRequest = New BatchUpdateSpreadsheetRequest()
        testReq.Requests = New List(Of Request)
        testReq.Requests.Add(req)
        Dim responseAddConditionalRules = service.Spreadsheets.BatchUpdate(testReq, fileId).Execute

如果条件为True(如果值为MIN,数字格式或最大值),它可以工作但是我想只是在没有条件的情况下改变颜色范围。但我没有找到一个例子来了解如何做到这一点。

2 个答案:

答案 0 :(得分:2)

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

条件格式规则只是:有条件的。它们仅在条件为真时应用格式(或者在渐变的情况下:始终根据渐变的比例应用不同的颜色)。

如果要无条件地设置单元格的背景颜色,请执行以下操作:在单元格上设置背景颜色。 Samples - Formatting页面有很多如何做到这一点的例子。具体来说:Format a header row

从该页面复制JSON(可直接转换为任何客户端库),



    POST https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}:batchUpdate
    {
      "requests": [
        {
          "repeatCell": {
            "range": {
              "sheetId": sheetId,
              "startRowIndex": 0,
              "endRowIndex": 1
            },
            "cell": {
              "userEnteredFormat": {
                "backgroundColor": {
                  "red": 0.0,
                  "green": 0.0,
                  "blue": 0.0
                },
                "horizontalAlignment" : "CENTER",
                "textFormat": {
                  "foregroundColor": {
                    "red": 1.0,
                    "green": 1.0,
                    "blue": 1.0
                  },
                  "fontSize": 12,
                  "bold": true
                }
              }
            },
            "fields": "userEnteredFormat(backgroundColor,textFormat,horizontalAlignment)"
          }
        },
        {
          "updateSheetProperties": {
            "properties": {
              "sheetId": sheetId,
              "gridProperties": {
                "frozenRowCount": 1
              }
            },
            "fields": "gridProperties.frozenRowCount"
          }
        }
      ]
    }