如何使用PHP通过API(v4)在Google表格中删除一行

时间:2019-03-31 08:30:00

标签: php google-api google-sheets-api delete-row

这是我使用PHP通过API(v4)更新Google Spread Sheet的功能。

public function updateSheet($spreadsheetId, $range, $rowData, $valueInputOption = 'RAW')
{
        $batch = new \Google_Service_Sheets_BatchUpdateValuesRequest();
        $batch->setValueInputOption($valueInputOption);
        $valueRange = new \Google_Service_Sheets_ValueRange();
        $valueRange->setValues($rowData);
        $valueRange->setRange($range);
        $batch->setData($valueRange);

        $response = $this->service->spreadsheets_values->batchUpdate($spreadsheetId, $batch);
        return $response;
}

问题是,如何使用PHP删除行?

EDIT

我已经设法基于this question启用并运行了一个功能,所以现在的问题是这是最好的方法吗?

public function deleteRow($spreadsheetId, $startRow, $endRow)
{
    $requests = [];
    $requests[] = new \Google_Service_Sheets_Request([
        'deleteDimension' => [
            'range' => [
                'sheetId' => 0,//(THIS IS NOT THE SPREADSHEET ID - IT IS THE BIT AFTER GID IN THE URL)
                'dimension' => "ROWS",
                'startIndex' => $startRow,
                'endIndex' => $endRow,
            ]
        ]
    ]);
    $batchUpdateRequest = new \Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
        'requests' => $requests
    ));

    $response = $this->service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest);
    var_dump($response);

}

0 个答案:

没有答案