如何使用 PHP 中的 api v4 从 Google 工作表中删除行

How to delete row from Google Sheets using api v4 in PHP

我正在尝试使用 Google API PHP 客户端库删除 Google 表格中的一行,Google 为其 API 提供。目前我只能清除该行的内容但不能删除它:

 $requestBody = new Google_Service_Sheets_ClearValuesRequest();
 $range = 'Form responses 1!C5:H5';
 $response = $service->spreadsheets_values->clear($spreadsheetId, $range, $requestBody);

文档除了给出一行与删除无关的HTTP,只给出batchUpdate之外,根本没有以任何方式解释如何删除。

编辑:这就是答案

$requests = [
  // Change the spreadsheet's title.
  new Google_Service_Sheets_Request([
    'deleteDimension' => [
      'range' => [
        'sheetId' => 'XXXXXXX', (THIS IS NOT THE SPREADSHEET ID - IT IS THE BIT AFTER GID IN THE URL)
        'dimension' => "ROWS",
        'startIndex' => '16',
        'endIndex' => '17'
      ]
    ]
])
]; 

您应该按照文档使用 batchUpdate。

Delete rows or columns

以下 spreadsheets.batchUpdate 请求删除 sheet 中的前三行。第二个请求删除列 B:D.

请求协议如下所示。 Updating Spreadsheets 指南展示了如何使用 Google API 客户端库实现不同语言的批量更新。

POST https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId:batchUpdate
{
  "requests": [
    {
      "deleteDimension": {
        "range": {
          "sheetId": sheetId,
          "dimension": "ROWS",
          "startIndex": 0,
          "endIndex": 3
        }
      }
    },
    {
      "deleteDimension": {
        "range": {
          "sheetId": sheetId,
          "dimension": "COLUMNS",
          "startIndex": 1,
          "endIndex": 4
        }
      }
    },
  ],
}

<?php
/*
 * BEFORE RUNNING:
 * ---------------
 * 1. If not already done, enable the Google Sheets API
 *    and check the quota for your project at
 *    https://console.developers.google.com/apis/api/sheets
 * 2. Install the PHP client library with Composer. Check installation
 *    instructions at https://github.com/google/google-api-php-client.
 */

// Autoload Composer.
require_once __DIR__ . '/vendor/autoload.php';

$client = getClient();

$service = new Google_Service_Sheets($client);

// The spreadsheet to apply the updates to.
$spreadsheetId = 'my-spreadsheet-id';  // TODO: Update placeholder value.

// TODO: Assign values to desired properties of `requestBody`:
$requestBody = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();

$response = $service->spreadsheets->batchUpdate($spreadsheetId, $requestBody);

// TODO: Change code below to process the `response` object:
echo '<pre>', var_export($response, true), '</pre>', "\n";

function getClient() {
  // TODO: Change placeholder below to generate authentication credentials. See
  // https://developers.google.com/sheets/quickstart/php#step_3_set_up_the_sample
  //
  // Authorize using one of the following scopes:
  //   'https://www.googleapis.com/auth/drive'
  //   'https://www.googleapis.com/auth/drive.file'
  //   'https://www.googleapis.com/auth/spreadsheets'
  return null;
}
?>

最终这段代码对我有用:

$batchUpdateRequest = new \Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
    'requests' => array(
      'deleteDimension' => array(
          'range' => array(
              'sheetId' => 0, // the ID of the sheet/tab shown after 'gid=' in the URL
              'dimension' => "ROWS",
              'startIndex' => $rowtodelete, // row number to delete
              'endIndex' => $rowtodelete + 1
          )
      )    
    )
));

$result = $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest);