Php 谷歌工作表-设置背景色
我正在编写一个以前由另一个开发人员持有的应用程序。经过某种处理后,他想用值填充Google Sheets文件。在他开始开发之前,他已经离开了,留给我的任务是理解GoogleAPI客户端php库 我设法插入值,这对我来说是一大步,但我想为某些单元格添加背景色。我没有找到任何方法来实现这个 现在,我就是这样插入值的:Php 谷歌工作表-设置背景色,php,google-api-php-client,google-sheets-api,Php,Google Api Php Client,Google Sheets Api,我正在编写一个以前由另一个开发人员持有的应用程序。经过某种处理后,他想用值填充Google Sheets文件。在他开始开发之前,他已经离开了,留给我的任务是理解GoogleAPI客户端php库 我设法插入值,这对我来说是一大步,但我想为某些单元格添加背景色。我没有找到任何方法来实现这个 现在,我就是这样插入值的: class Sheet { public function __construct($client) { $this->service = new \Go
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']);
}
}
我想创建一个颜色线函数
以下是我的第一次尝试:
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);
}
首先,我甚至都不明白我写了什么。。。另外,它说的是无效的请求[0]。addConditionalFormatRule:没有id为1的网格,但也没那么糟糕,我认为它不会完成我所寻找的任务
我想它会创建一个条件格式,但我只想要一个背景。。。对于简单的应用程序,此API看起来非常复杂
无论如何!如果有人能帮助我,我将非常感激 如果你真的不在乎解释,就转到最后一节:
这也许不是最好的解决方案,但至少它起了作用
解决方案的解释TL,NR:
我们需要什么?
我们想要的RGBa值
范围sheetId、行索引、列
索引的
电子表格Id。
现在,我们应该如何进行?以前的源代码其实没那么糟糕。。。只需要稍微改变一下:
我们不想创建条件格式,而是要更新单元格,所以我们应该使用repeatCell请求。我将解释为什么使用repeatCell而不是updateCell
,我们可以有3个参数:
限制更新的掩码字段,
靶场
牢房。这是一个可以包含userEnteredFormat的文件,现在您可以访问backgroundColor属性!!
开始编码:
好,让我们定义范围:
起点和终点不能在同一位置,因此起点为-1时,它将只更改一行
$myRange = [
'sheetId' => $sheetId,
'startRowIndex' => $line-1,
'endRowIndex' => $line,
'startColumnIndex' => 0,
'endColumnIndex' => 17,
];
现在,让我们定义每个组件的颜色必须介于0和1之间:
$format = [
"backgroundColor" => [
"red" => $r,
"green" => $g,
"blue" => $b,
"alpha" => $a,
],
];
就这样,我们几乎准备好了
我们只需要告诉服务,我们需要一个repeatCell请求。不要忘记fields参数。如果不限制更新,单元格的所有数据都将更改,包括文本!在本例中,字段的路径从单元格开始,因此我们只需键入“userEnteredFormat.backgroundColor”。然后使用先前创建的$format变量
$requests = [
new \Google_Service_Sheets_Request([
'repeatCell' => [
'fields' => 'userEnteredFormat.backgroundColor',
'range' => $myRange,
'cell' => [
'userEnteredFormat' => $format,
],
],
])
];
好的!完成。现在将此或这些请求包括在批处理中:
$batchUpdateRequest = new \Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
'requests' => $requests
]);
最后,用服务发送请求,在我的案例中包括speadsheet ID$this->ID
$response = $this->service->spreadsheets->batchUpdate($this->id,
$batchUpdateRequest);
完整解决方案:
感谢阅读,以下是您的解决方案:
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);
}
最后,我找到了一个有效的解决方案
$sheetId = $service->spreadsheets->get($spreadsheetId, ['ranges' => 'worksheetname']);
//For range, end rows and end columns are not considered for updating. Index start from 0
//Here we are setting range for A3:E5 grid
$range = new Google_Service_Sheets_GridRange();
$range->setSheetId($sheetId->sheets[0]->properties->sheetId);
$range->setEndRowIndex(2);
$range->setEndRowIndex(5);
$range->setStartColumnIndex(0);
$range->setEndColumnIndex(5);
//set the color value in RGBA
$color = new Google_Service_Sheets_Color();
$color->setRed($red / 255);
$color->setGreen($green / 255);
$color->setBlue($blue / 255);
$color->setAlpha($alpha);
//cellFormat is used to set different properties of a cell
$cellFormat = new Google_Service_Sheets_CellFormat();
//textFormat is used to set different text formats like Bold
$textFormat = new Google_Service_Sheets_TextFormat();
$textFormat->setBold(true);
$cellFormat->setBackgroundColor($color);
$cellFormat->setTextFormat($textFormat);
//New cell class. Assign the cellFormat to it
$cell = new Google_Service_Sheets_CellData();
$cell->setUserEnteredFormat($cellFormat);
//repeatCell request is used to assign requests to range of cells
$repeatCell = new Google_Service_Sheets_RepeatCellRequest();
$repeatCell->setRange($range);
//Fields is used to specify which properties of a cell to update
//Here we are updating two properties. So both are specified and seperated by comma ,
$repeatCell->setFields('userEnteredFormat.textFormat.bold,userEnteredFormat.backgroundColor');
//Set repeatCellrequest to the requests class
$requests = new Google_Service_Sheets_Request();
$requests->setRepeatCell($repeatCell);
//requests are set to batchupdatespreadsheetrequest class
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
$batchUpdateRequest->setRequests($requests);
//Finally batchUpdate is called to update the format of cells
$response = $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest, []);
为什么在你的问题中回答你的问题?把它写成一个字母。自我回答完全可以。