Skip to content
Advertisement

Cannot hide gridlines Google Spreadsheet API PHP

I am trying to hide all gridlines from a file I just created with Google Sheets API, I am using the following code:

$service = $this->GoogleServiceSheets();
$sheetID = null;
$worksheetSheets = $service->spreadsheets->get($fileID)->sheets;
foreach($worksheetSheets as $sheet){ 
    $sheetID = $sheet->properties['sheetId'];     
    break;
}
$service = $this->GoogleServiceSheets(); 

$requests = [
    new Google_Service_Sheets_Request([
        'updateDimensionProperties' => [
            'range'=> new Google_Service_Sheets_DimensionRange([
                'sheetId' => $sheetID,
                'dimension' => 'COLUMNS',
                'startIndex' => 0,
                'endIndex' => 1000        
            ]),
            'properties'=> new Google_Service_Sheets_DimensionProperties([
                "hideGridlines"=> True,                 
            ]),
            'fields' => 'hideGridlines'
        ]
    ])
];



$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(['requests' => $requests]);
$service->spreadsheets->batchUpdate($fileID, $batchUpdateRequest);   

But does not hide, instead gives me an error. What I am doing wrong?

Advertisement

Answer

In order to hide the grid line of the Google Spreadsheet with Sheets API, UpdateSheetPropertiesRequest of batchUpdate is used. Unfortunately, updateDimensionProperties cannot achieve this. I think that this is the reason of your issue. In order to achieve your goal, when your script is modified, it becomes as follows.

Modified script:

In this modification, your $requests is modified as follows.

$requests = [
    new Google_Service_Sheets_Request([
        'updateSheetProperties' => [
            'properties' => [
                'sheetId' => $sheetID,
                'gridProperties' => [
                    'hideGridlines' => true
                ],
            ],
            'fields' => 'gridProperties.hideGridlines'
        ],
    ])
];

Note:

  • In this modification, it supposes that your $service = $this->GoogleServiceSheets(); and $sheetID are valid values. And also, it supposes that you have already been able to get and put values for Google Spreadsheet using Sheets API. Please be careful this.

Reference:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement