使用Google电子表格API冻结行和设置样式

use*_*803 7 java api freeze google-sheets google-sheets-api

如果我滚动电子表格,行的标题字段将被隐藏,我需要在同一电子表格中使用粗体文本格式.

问题
我可以通过电子表格api设置冻结行和样式 - 是否可以?

Jos*_*osh 7

现在可以在v4 API中使用它.

以下是JAVA API实施的参考: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中)

以下是API文档:https: //developers.google.com/sheets/reference/rest/v4/spreadsheets#gridproperties

如果您想使用App脚本,也可以:https: //developers.google.com/apps-script/reference/spreadsheet/sheet#setFrozenRows(Integer)

这是我用Ruby API做的请求:

update_sheet_properties: {
  properties: {
    sheet_id: 'YOUR SHEET ID HERE',
    grid_properties: { frozen_row_count: 4 }
  },
  fields: 'gridProperties.frozenRowCount'
}
Run Code Online (Sandbox Code Playgroud)


小智 6

如果有人正在寻找 PHP 解决方案来格式化标题并在滚动时冻结标题,那么您可以采用如下所述的方法:

        //$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);
Run Code Online (Sandbox Code Playgroud)