如何使用PHP从Google Sheet中删除行?

时间:2016-12-06 16:56:43

标签: php api google-sheets

我使用Google Api PHP Library。 我想删除行。 我找到了,但我该如何使用呢? https://github.com/google/google-api-php-client-services/blob/master/src/Google/Service/Sheets/DeleteDimensionRequest.php

例如,我用它来添加行:

// Build the CellData array
    $values = array();
    foreach( $ary_values AS $d ) {
        $cellData = new Google_Service_Sheets_CellData();
        $value = new Google_Service_Sheets_ExtendedValue();
        $value->setStringValue($d);
        $cellData->setUserEnteredValue($value);
        $values[] = $cellData;
    }
    // Build the RowData
    $rowData = new Google_Service_Sheets_RowData();
    $rowData->setValues($values);
    // Prepare the request
    $append_request = new Google_Service_Sheets_AppendCellsRequest();
    $append_request->setSheetId(0);
    $append_request->setRows($rowData);
    $append_request->setFields('userEnteredValue');
    // Set the request
    $request = new Google_Service_Sheets_Request();
    $request->setAppendCells($append_request);
    // Add the request to the requests array
    $requests = array();
    $requests[] = $request;
    // Prepare the update
    $batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
        'requests' => $requests
    ));

    try {
        // Execute the request
        $response = $sheet_service->spreadsheets->batchUpdate($fileId, $batchUpdateRequest);
        if( $response->valid() ) {
            // Success, the row has been added
            return true;
        }
    } catch (Exception $e) {
        // Something went wrong
        error_log($e->getMessage());
        print_r($e->getMessage());
    }

    return false;

1 个答案:

答案 0 :(得分:0)

请尝试这适用于我

data-function="turbolinks-autoload"

然后将查询发送到谷歌以更新您的工作表

$spid = '1lIUtn8WmN1Yhgxv68dt4rylm6rO77o8UX8uZu9PIu2o'; // <=== Remember to update this to your workbook to be updated
$deleteOperation = array(
                    'range' => array(
                        'sheetId'   => 0, // <======= This mean the very first sheet on worksheet
                        'dimension' => 'ROWS',
                        'startIndex'=> 2, //Identify the starting point,
                        'endIndex'  => (3) //Identify where to stop when deleting
                    )
                );
$deletable_row[] = new Google_Service_Sheets_Request(
                        array('deleteDimension' =>  $deleteOperation)
                    );

你检查here。在尝试解决谷歌表删除操作时,这有助于我。

我希望这会对你有所帮助。谢谢并保持联系