10

If i scroll the spreadsheet ,header field of the rows are hidden and I need bold text format in the same spreadsheet.

Question
Can I set the freeze rows and styles through the spreadsheet api - Is it possible?

Eric Koleda
  • 12,420
  • 1
  • 33
  • 51
user3323803
  • 133
  • 1
  • 8
  • 1
    Maybe possilbe with Google-apps-script, but it is not possible with the gdata style google-spreadsheet-api – eddyparkinson May 12 '14 at 07:26
  • I would really like this functionality as well. Setting the header row to bold and freezing it would be great. – Locane Jul 19 '16 at 21:38

4 Answers4

12

This is now available in the v4 API.

Here is the reference for the JAVA API implementation: https://developers.google.com/resources/api-libraries/documentation/sheets/v4/java/latest/com/google/api/services/sheets/v4/model/GridProperties.html#setFrozenRowCount(java.lang.Integer)

Here is the API documentation: https://developers.google.com/sheets/reference/rest/v4/spreadsheets#gridproperties

If you want to do it w/ an App Script you can as well: https://developers.google.com/apps-script/reference/spreadsheet/sheet#setFrozenRows(Integer)

This is the request I made w/ the Ruby API:

update_sheet_properties: {
  properties: {
    sheet_id: 'YOUR SHEET ID HERE',
    grid_properties: { frozen_row_count: 4 }
  },
  fields: 'gridProperties.frozenRowCount'
}
Josh
  • 8,329
  • 4
  • 36
  • 33
  • Btw, it took me a while to get to the 'final' solution. Combine this answer with https://stackoverflow.com/questions/41547008/unknown-name-update-sheet-properties for a 'full solution'. Keep in mind that in the google API, camel case and underscore notation are equivalent. – aiguofer May 24 '17 at 22:09
  • If you get an error like "You can't delete all the rows on the sheet." it's because when you update the grid properties you also need to set rowCount and columnCount properties as well. `EX: sheet.updateProperties({ gridProperties: { frozen_row_count: 0, rowCount: 1, columnCount: headers.length } });` – blak3r Sep 10 '21 at 20:50
6

If anyone looking for solution with PHP to format the header and froze the header while scrolling then you can go with this approach as mentioned below:

        //$client will be your google service client request after authorization.           
        $service = new Google_Service_Sheets($client);

        $formatRowColrequests = [
            new Google_Service_Sheets_Request([
              "repeatCell" => [
                "range" => [
                  "sheetId" => $setSheetId, //set your sheet ID
                  "startRowIndex" => 0,
                  "endRowIndex" => 1,
                  "startColumnIndex" => 0,
                  "endColumnIndex" => 100
                ],
                "cell" => [
                  "userEnteredFormat" => [
                    "horizontalAlignment" => "CENTER",
                    "textFormat" => [
                      "fontSize" => 9,
                      "bold" => true
                    ]
                  ]
                ],
                "fields" => "userEnteredFormat(textFormat,horizontalAlignment)"
              ]
            ]),
            new Google_Service_Sheets_Request([
                'updateSheetProperties' => [
                    'properties' => [
                        'sheetId' => $setSheetId,
                        'gridProperties' => [
                            'frozenRowCount' => 1
                        ]
                    ],
                    "fields"=> "gridProperties.frozenRowCount"
                ]
            ])
        ];
        $batchUpdateCellFormatRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
            'requests' => $formatRowColrequests
        ]);
        $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateCellFormatRequest);
Rajender Verma
  • 389
  • 3
  • 9
2

This is how to do it in android. Took awhile to figure out. The documentation gives few examples[for this].

 SpreadsheetProperties properties = new SpreadsheetProperties();
        properties.setTitle(calendar.getTime().toString());

        Spreadsheet requestBody = new Spreadsheet();

        GridProperties gridProperties = new GridProperties();
        gridProperties.setFrozenRowCount(2);

        SheetProperties sheetProperties = new SheetProperties();
        sheetProperties.setTitle("my new sheet");
        sheetProperties.setGridProperties(gridProperties);
        Sheet sheet = new Sheet();
        sheet.setProperties(sheetProperties);
        List<Sheet> sheets = new ArrayList<>();
        sheets.add(sheet);
        requestBody.setSheets(sheets);

        requestBody.setProperties(properties);
0

Using the Sheets library, for some bizarre reason you need to set the fields also. I get that behind the scenes it's making JSON, but it would seem obvious to me that if I set that property the Library should generate it. So to make a long story short:

import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.model.*; 

//This creates a basic Filter, as an example for using batchUpdate.
SetBasicFilterRequest setBasicFilterRequest = new SetBasicFilterRequest().setFilter(new BasicFilter()
                    .setRange(new GridRange().setSheetId(sheetId).setStartRowIndex(0).setStartColumnIndex(0).setEndColumnIndex(headers.size()).setEndRowIndex(data.size())));

//This is the frozen row request:
UpdateSheetPropertiesRequest updateSheetPropertiesRequest = new UpdateSheetPropertiesRequest().setFields("gridProperties.frozenRowCount")
                    .setProperties(new SheetProperties().setSheetId(sheetId)
                    .setGridProperties(new GridProperties().setFrozenRowCount(1)));

List<Request> requests = List.of(
                    new Request().setSetBasicFilter(setBasicFilterRequest),
                    new Request().setUpdateSheetProperties(updateSheetPropertiesRequest));
BatchUpdateSpreadsheetRequest apiRequest = new BatchUpdateSpreadsheetRequest().setRequests(requests);
YOUR_SHEETS_SERVICE.spreadsheets().batchUpdate(YOUR_WORKBOOK_ID, apiRequest ).execute();
CeePlusPlus
  • 803
  • 1
  • 7
  • 26