2016-12-06 43 views
2

我使用Google Api PHP库。 我想删除行。 我找到了,但是如何使用它? https://github.com/google/google-api-php-client-services/blob/master/src/Google/Service/Sheets/DeleteDimensionRequest.php如何从PHP Google表格中删除行?

例如,我用它来添加行:

// 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; 
+0

你有试过什么吗? – depperm

回答

0

请试试这个工作对我来说

$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) 
        ); 

然后将查询到Google更新您的工作表

$delete_body = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
         'requests' => $deletable_row 
        ) 
       ); 
//var_dump($delete_body); 
$result = $service->spreadsheets->batchUpdate($spid, $delete_body); 

你检查here。当试图解决谷歌表单删除操作时,这会帮助我。

我希望这可以帮助你。谢谢并保持联系