Pregunta

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?

¿Fue útil?

Solución

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'
}

Otros consejos

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);

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);

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();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top