使用 Java API 在 Google 工作表中设置单元格大小

Set cell size in Google sheets using Java API

如何在 Google 工作表 API 中设置单元格大小?

我尝试设置自定义格式,但找不到任何对尺寸有用的东西。

CellFormat myFormat = new CellFormat();
            myFormat.setBackgroundColor(new Color()
                .setRed(Float.valueOf("2"))
                .setGreen(Float.valueOf("1"))
                .setBlue(Float.valueOf("0.2"))); // red background
            myFormat.setTextFormat(new TextFormat()
                .setFontSize(15)
                .setBold(Boolean.TRUE));

如果您尝试设置单元格大小,请尝试阅读:

setColumnWidth(columnPosition, width)

Sets the width of the given column in pixels.

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];

 // Sets the first column to a width of 200 pixels
 sheet.setColumnWidth(1, 200);
 

setRowHeight(rowPosition, height)

Sets the row height of the given row in pixels.

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];

 // Sets the first row to a height of 200 pixels
 sheet.setRowHeight(1, 200);
 

这里是link,使用Google Sheet API设置列宽或行高。

更新

setPixelSize

  • The height (if a row) or width (if a column) of the dimension in pixels.

Google Sheets API Javadoc documentation,绝对可以帮助您熟悉Google Sheet API in Java.

希望对您有所帮助。

据我所知这是不可能的,因为行和列会将其大小调整为最大的单元格。 另一种方法是将所有单元格设置为固定大小,比如 10x10,然后将它们合并在一起以获得所需的结果。

您可以通过向 API 发送 JSON 请求来更改列的大小,如 link 所示: https://developers.google.com/sheets/api/samples/rowcolumn

然后像这样发送请求

JSONObject json = new JSONObject(); json.put("someKey", "someValue"); 
CloseableHttpClient httpClient = HttpClientBuilder.create().build(); 
try { 
    HttpPost request = new HttpPost("yoururl"); 
    StringEntity params = new StringEntity(json.toString());           
    request.addHeader("content-type", "application/json"); 
    request.setEntity(params); httpClient.execute(request);    //handle response here...
 } 
catch (Exception ex) { // handle exception here } 
finally { httpClient.close(); }

这是我用来调整列大小的方法。希望对您有所帮助:

requests.add(new Request().setUpdateDimensionProperties(
                new UpdateDimensionPropertiesRequest()
                        .setRange(
                                new DimensionRange()
                                        .setSheetId(sheetID)
                                        .setDimension("COLUMNS")
                                        .setStartIndex(0)
                                        .setEndIndex(1)
                        )
                        .setProperties(
                                new DimensionProperties()
                                        .setPixelSize(400))
                        .setFields("pixelSize")));