Google 表格 - 设置背景颜色

Mar*_*rco 4 php google-api-php-client google-sheets-api

我正在编写一个以前由另一位开发人员持有的应用程序。经过一定的处理后,他想用值填充 Google Sheets 文件。在他开始开发之前,他就走了,留给我的任务是了解 google-api-client-php 库。

我设法插入值(这对我来说是一大步),但我想为某些单元格添加背景颜色。我没有找到任何方法来实现这一点......

现在,这就是我插入值的方式:

class Sheet {
    public function __construct($client) {
        $this->service = new \Google_Service_Sheets($client);
    }
    public function write($line, $newValues, $startColumn)
    {
        $values = new \Google_Service_Sheets_ValueRange();
        $values->setValues([    $newValues  ]); 

        $this->service->spreadsheets_values->update($this->id, $range, $values, ['valueInputOption' => 'USER_ENTERED']);
    }
}
Run Code Online (Sandbox Code Playgroud)

我想创建一个colorLine()函数。

这是我的第一次尝试:

 public function colorLine($line, $r, $g, $b, $a = 1) {
   $myRange = [
        'sheetId' => 1,
        'startRowIndex' => $line,
        'endRowIndex' => $line,
        'startColumnIndex' => 0,
        'endColumnIndex' => 1000,
    ];

    $requests = [
        new \Google_Service_Sheets_Request([
            'addConditionalFormatRule' => [
                'rule' => [
                    'ranges' => [ $myRange ],
                    'booleanRule' => [
                        'condition' => [
                            'type' => 'CUSTOM_FORMULA',
                            'values' => [ [ 'userEnteredValue' => '=1' ] ]
                        ],
                        'format' => [
                            'backgroundColor' => [ 'red' => $r, 'green' => $g, 'blue' => $b ]
                        ]
                    ]
                ],
                'index' => 1
            ]
        ])
    ];

    $batchUpdateRequest = new \Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
        'requests' => $requests
    ]);
    $response = $this->service->spreadsheets->batchUpdate($this->id,
        $batchUpdateRequest);
}
Run Code Online (Sandbox Code Playgroud)

首先,我什至不太明白我写的是什么......另外,它说“无效的请求[0].addConditionalFormatRule:没有id为1的网格”,但它并没有那么糟糕,我不认为它会做我正在寻找的事情。

我认为它会创建一个“条件格式”,但我只想要一个背景......这个API对于简单的应用程序来说看起来非常复杂......

反正!如果有人可以帮助我,我将非常感激!

Mar*_*rco 7

如果您不太关心解释,请转到最后一部分:)

这也许不是最好的解决方案,但至少它有效。

解决方案的解释(TL,NR):

我们需要什么 ?

  1. 我们想要的RGBa值
  2. 范围(sheetId、行索引、列索引)
  3. 电子表格 ID。

现在,我们应该如何进行?嗯,之前的源代码实际上并没有那么糟糕......只需稍微更改一下即可:

  • 我们不想创建条件格式,而是要更新单元格,所以我们应该使用“ repeatCell ”请求(我将解释为什么使用“ repeatCell ”而不是“ updateCell ”)
  • 在此请求中,我们可以有 3 个参数:
    • 掩码字段(限制更新),
    • 范围
    • 细胞。这是一个可以包含“userEnteredFormat”(CellFormat的 CellData 对象,现在您可以访问 backgroundColor 属性!

开始编码:

好的,让我们定义范围:

您不能将“开始”和“结束”放在同一位置(因此在开始处使用 -1,它只会更改一行)

    $myRange = [
        'sheetId' => $sheetId,
        'startRowIndex' => $line-1,
        'endRowIndex' => $line,
        'startColumnIndex' => 0,
        'endColumnIndex' => 17,
    ];
Run Code Online (Sandbox Code Playgroud)

现在让我们定义颜色(每个分量必须介于 0 和 1 之间):

    $format = [
        "backgroundColor" => [
            "red" => $r,
            "green" => $g,
            "blue" => $b,
            "alpha" => $a,
        ],
    ];
Run Code Online (Sandbox Code Playgroud)

就这样,我们快准备好了!

我们只需告诉服务我们想要一个“ repeatCell ”请求。不要忘记“fields”参数。如果不限制更新,单元格的所有数据都会改变,包括文字!在本例中,“字段”的路径从“单元格”开始,因此我们只需键入'userEnteredFormat.backgroundColor'。然后使用之前创建的 $format 变量。

    $requests = [
        new \Google_Service_Sheets_Request([
            'repeatCell' => [
                'fields' => 'userEnteredFormat.backgroundColor',
                'range' => $myRange,
                'cell' => [
                    'userEnteredFormat' => $format,
                ],
            ],
        ])
    ];
Run Code Online (Sandbox Code Playgroud)

好的!完毕。现在将此(或这些)请求包含在批处理中:

    $batchUpdateRequest = new \Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
        'requests' => $requests
    ]);
Run Code Online (Sandbox Code Playgroud)

最后,通过服务发送请求,包括电子表格 ID($this->id在我的例子中)。

    $response = $this->service->spreadsheets->batchUpdate($this->id,
        $batchUpdateRequest);
Run Code Online (Sandbox Code Playgroud)

完整的解决方案:

感谢您的阅读,这就是您的解决方案:

public function colorLine($line, $r, $g, $b, $a = 1.0, $worksheetName = null)
{
    if($r > 1) $r = Tools::rescale($r, 0, 255, 0, 1);
    if($g > 1) $g = Tools::rescale($g, 0, 255, 0, 1);
    if($b > 1) $b = Tools::rescale($b, 0, 255, 0, 1);
    if($a > 1) $a = Tools::rescale($a, 0, 255, 0, 1);

    $worksheetName = ($worksheetName ? : $this->defaultWorksheet);
    $sheetId = $this->getWorksheetId($worksheetName);

    $myRange = [
        'sheetId' => $sheetId,
        'startRowIndex' => $line-1,
        'endRowIndex' => $line,
        'startColumnIndex' => 0,
        'endColumnIndex' => 17,
    ];
    $format = [
        "backgroundColor" => [
            "red" => $r,
            "green" => $g,
            "blue" => $b,
            "alpha" => $a,
        ],
    ];

    $requests = [
        new \Google_Service_Sheets_Request([
            'repeatCell' => [
                'fields' => 'userEnteredFormat.backgroundColor',
                'range' => $myRange,
                'cell' => [
                    'userEnteredFormat' => $format,
                ],
            ],
        ])
    ];

    $batchUpdateRequest = new \Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
        'requests' => $requests
    ]);
    $response = $this->service->spreadsheets->batchUpdate($this->id,
        $batchUpdateRequest);
}
Run Code Online (Sandbox Code Playgroud)