Search code examples
phpgoogle-apigoogle-docsgoogle-docs-api

How we can change the background color for the entire row in table with google docs api using php?


I want to change the background color of my table for the entire row at one go.But I can set background color for specific cell from this below code request.

$requests = [
    new Google_Service_Docs_Request([
        'insertTable' => [
            'location' => ['index' => 1],
            'columns' => 2,
            'rows' => 2
        ]
    ]),
    new Google_Service_Docs_Request([
    "updateTableCellStyle" => [
        "tableCellStyle" => [
          "backgroundColor" => [
            "color" => [
                "rgbColor" => [
                    "red" => 0.8,
                    "green" => 0.8,
                    "blue" => 0.8,
                ]
            ]
        ]
    ],
        "fields" => "backgroundColor",
        "tableRange" => [
            "columnSpan" => 1,
            "rowSpan" => 1,
            "tableCellLocation" => [
              "columnIndex" => 1,
              "rowIndex" => 1,
              "tableStartLocation" => [
                "index" => 2
              ]
            ]
        ]
    ]
]),
];

Can anybody tell me what I have to modify in the above code or what mistake I am making to add background color for the entire row at one go with google docs api using PHP.


Solution

  • From your request body, it is found that the inserted table has 2 columns. In this case, when you want to set the background color to both the columns "A" and "B" of row 2, how about the following modification?

    From:

    "tableRange" => [
        "columnSpan" => 1,
        "rowSpan" => 1,
        "tableCellLocation" => [
          "columnIndex" => 1,
          "rowIndex" => 1,
          "tableStartLocation" => [
            "index" => 2
          ]
        ]
    ]
    

    To:

    "tableRange" => [
        "columnSpan" => 2,
        "rowSpan" => 1,
        "tableCellLocation" => [
          "rowIndex" => 1,
          "tableStartLocation" => [
            "index" => 2
          ]
        ]
    ]
    

    Note:

    • For example, when you want to set the background color to both the columns "A" and "B" of row 1, please modify it as follows.

        "tableRange" => [
            "columnSpan" => 2,
            "rowSpan" => 1,
            "tableCellLocation" => [
              "rowIndex" => 0, // Modified
              "tableStartLocation" => [
                "index" => 2
              ]
            ]
        ]
      

    Reference: