1

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...:(

ÁngelBlanco
  • 438
  • 4
  • 11
  • Can you share more of the error message? It looks like most are about the `properties` value, but the last one is different from the others. – tehhowch Mar 19 '18 at 23:34
  • It is the whole error, printed in console...I can show you the code throwing the error if that helps, but it is a function of the REST api: libs/google-api-php-client-2.2.1/src/Google/Http/REST.php on line 118 – ÁngelBlanco Mar 20 '18 at 02:13
  • The error in your post can't be the full error, as it is missing closing braces. It mentions an unknown name, but then doesn't indicate what the unknown name is. – tehhowch Mar 20 '18 at 04:19
  • You are right! I just posted the whole error, I hope it will help...I am really stuck here. Thank you very much @tehhowch ! – ÁngelBlanco Mar 20 '18 at 12:42
  • 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...:( – ÁngelBlanco Mar 21 '18 at 10:50
  • can you inspect the request that will be sent by the php client? E.g. to compare it to the working REST body? Based on the named methods they look equivalent... Could be worth an issue on the issue tracker. – tehhowch Mar 21 '18 at 11:52
  • You mean the request sent by the call() method of Resource.php? GuzzleHttp\Psr7\Request Object ( [method:GuzzleHttp\Psr7\Request:private] => POST [requestTarget:GuzzleHttp\Psr7\Request:private] => [uri:GuzzleHttp\Psr7\Request:private] => GuzzleHttp\Psr7\Uri Object ( [scheme:GuzzleHttp\Psr7\Uri:private] => https [host:GuzzleHttp\Psr7\Uri:private] => www.googleapis.com [port:GuzzleHttp\Psr7\Uri:private] => [path:GuzzleHttp\Psr7\Uri:private] => /drive/v3/files/sheetId/copy – ÁngelBlanco Mar 21 '18 at 20:27
  • Is the path ok? /drive/v3/files/sheetId/copy How can I know if the REST server URL is wrong? – ÁngelBlanco Mar 21 '18 at 20:28
  • Sorry @tehhowch , what is the issue tracker? I am gonna try there...but I don't really know what do you mean...:) – ÁngelBlanco Mar 26 '18 at 12:50

0 Answers0