我需要搜索存储在工作表中的特定值,并获取整行或存储该值的单元格的位置。
注意:我的工作表包含超过10000行数据,我需要更新单个列。我不希望从工作表中获取所有数据并进行更新,因为这会影响我的网站的性能。
请帮助我找到解决方案。
答案 0 :(得分:1)
迟到总比没有好!? 好吧,我也有同样的问题,但是我需要更改多个字段,我什至查看了上面的链接,但是在php中什么也没有。如果您只需要更改一个或多个字段,则下面的代码也可以使用。 (谷歌翻译)
<?php
$myvalue = 'NEW VALUE';
$values = [[$c_id,]];
// make conn with credentials
$client = new \Google_Client();
$client->setApplicationName('Google Sheets with PHP');
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('offline');
$client->setAuthConfig(__DIR__ . '/cred.json');
$service = new Google_Service_Sheets($client);
$spreadsheetId = "your-id";
// insert custom range to match with name of spreadsheet and range to search
$range = "COMPLETO!A2:A50000";
$cell_id;
$cell_range;
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values_r = $response->getValues();
if (empty($values_r)) {
print "None Found.\n";
} else {
print "Data found\n";
$range_index = '1';
foreach ($values_r as $row) {
// Show the results in array
$range_index++;
// Match com id do banco de dados
if($row[0] === $c_id){
echo "ID found\n";
echo "$row[0]\n";
echo "Cell ID A${range_index}\n";
$cell_id = "A${range_index}";
// in $cell_range set the effective range to change
// $cell_range = "A${range_index}:CM${range_index}";
break;
}
}
}
$body = new Google_Service_Sheets_ValueRange([
'values' => $values
]);
// try Update
$append_sheet = $service->spreadsheets_values->update($spreadsheetId, $cell_range, $body,['valueInputOption' => 'RAW']);
echo "Update Google Sheet\n";
$conn = null;
?>