问题
I am using the following code to hide some sheets of a spreadsheet. I use php client library. My target is to export only the desired sheets into a PDF file. Since there is not method to achieve that, I am trying to hide the undesired sheets and then export the spreadhseet with the sheet I need to send by email. But all there requests are failing. Anyone knows the reason? Thank you!
function hideSheets($spreadsheetId, $sheetIds) {
// Get the sheet service.
global $sheet_service;
$requestBody = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
$requests = array();
foreach ($sheetIds as $key => $sheetId) {
$request= new Google_Service_Sheets_UpdateSheetPropertiesRequest();
$request_prop = new Google_Service_Sheets_SheetProperties();
$request_prop->setSheetId($sheetId);
$request_prop->setHidden(true);
$request->setProperties($request_prop);
$request->setFields("hidden");
$requests[] = $request;
}
// print_r($requests[0]);
$requestBody->setRequests($requests);
$requestBody->setIncludeSpreadsheetInResponse(false);
try { $response = $sheet_service->spreadsheets->batchUpdate($spreadsheetId, $requestBody);
} catch (Exception $e) { print "An error occurred: " . $e->getMessage(); die(); }
echo '<br><br>';
print_r($response);
return;
}
This code is throwing the following error:
An error occurred:
{ "error":
{ "code": 400, "message": "Invalid JSON payload received. Unknown name \"fields\" at 'requests[0]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[0]': Cannot find field
Invalid JSON payload received. Unknown name \"fields\" at 'requests[1]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[1]': Cannot find field
Invalid JSON payload received. Unknown name \"fields\" at 'requests[2]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[2]': Cannot find field
Invalid JSON payload received. Unknown name \"fields\" at 'requests[3]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[3]': Cannot find field
Invalid JSON payload received. Unknown name \"fields\" at 'requests[4]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[4]': Cannot find field
Invalid JSON payload received. Unknown name \"fields\" at 'requests[5]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[5]': Cannot find field
Invalid JSON payload received. Unknown name \"fields\" at 'requests[6]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[6]': Cannot find field
Invalid JSON payload received. Unknown name \"fields\" at 'requests[7]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[7]': Cannot find field
Invalid JSON payload received. Unknown name \"fields\" at 'requests[8]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[8]': Cannot find field
Invalid JSON payload received. Unknown name \"fields\" at 'requests[9]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[9]': Cannot find field.",
"errors": [ { "message": "Invalid JSON payload received. Unknown name \"fields\" at 'requests[0]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[0]': Cannot find field
Invalid JSON payload received. Unknown name \"fields\" at 'requests[1]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[1]': Cannot find field
Invalid JSON payload received. Unknown name \"fields\" at 'requests[2]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[2]': Cannot find field
Invalid JSON payload received. Unknown name \"fields\" at 'requests[3]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[3]': Cannot find field
Invalid JSON payload received. Unknown name \"fields\" at 'requests[4]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[4]': Cannot find field
Invalid JSON payload received. Unknown name \"fields\" at 'requests[5]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[5]': Cannot find field
Invalid JSON payload received. Unknown name \"fields\" at 'requests[6]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[6]': Cannot find field
Invalid JSON payload received. Unknown name \"fields\" at 'requests[7]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[7]': Cannot find field
Invalid JSON payload received. Unknown name \"fields\" at 'requests[8]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[8]': Cannot find field
Invalid JSON payload received. Unknown name \"fields\" at 'requests[9]': Cannot find field
Invalid JSON payload received. Unknown name \"properties\" at 'requests[9]': Cannot find field.", "domain": "global", "reason": "badRequest" } ],
"status": "INVALID_ARGUMENT" } }
The following request body is working fine:
{ "requests": [
{ "updateSheetProperties": {
"properties": {
"sheetId": 848799001,
"hidden": true
},
"fields": "hidden"}}]}
on google's TRY THIS API app: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate
But I dont achieve to translate it to google php client...:(
来源:https://stackoverflow.com/questions/49373536/hide-sheets-with-google-php-spreadsheets-client