Hide Sheets with Google PHP SpreadSheets client

只谈情不闲聊 提交于 2019-12-02 09:17:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!