【问题标题】:Set cell size in Google sheets using Java API使用 Java API 在 Google 表格中设置单元格大小
【发布时间】:2017-04-26 06:33:05
【问题描述】:

如何在 Google Sheets 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));

【问题讨论】:

    标签: java google-sheets google-spreadsheet-api


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      setColumnWidth(columnPosition, width)

      以像素为单位设置给定列的宽度。

       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)

      以像素为单位设置给定行的行高。

       var ss = SpreadsheetApp.getActiveSpreadsheet();
       var sheet = ss.getSheets()[0];
      
       // Sets the first row to a height of 200 pixels
       sheet.setRowHeight(1, 200);
       
      

      这里是使用Google Sheet API设置列宽或行高的链接。

      更新

      setPixelSize

      • 维度的高度(如果是行)或宽度(如果是列)以像素为单位。

      Google Sheets API Javadoc documentation,肯定会帮助您熟悉 Java 中的 Google Sheet API。

      希望这会有所帮助。

      【讨论】:

      • 是的,但是如何使用 Java 和单个单元格做到这一点?
      • 我需要设置单个单元格大小。
      • 另一个选项是设置列大小。这可能吗?
      【解决方案3】:

      您可以通过对 API 的 JSON 请求来更改列的大小,如以下链接所示: 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(); }
      

      【讨论】:

      • 官方java api有办法吗?
      【解决方案4】:

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

      【讨论】:

      • 我可以使用 Java 代码设置列的大小吗?
      • 您必须发送 JSON 请求,如以下链接所示:developers.google.com/sheets/api/samples/rowcolumn
      • JSONObject json = new JSONObject(); json.put("someKey", "someValue"); CloseableHttpClient httpClient = HttpClientBuilder.create().build();尝试 { HttpPost request = new HttpPost("yoururl"); StringEntity params = new StringEntity(json.toString()); request.addHeader("content-type", "application/json"); request.setEntity(params) ; httpClient.execute(request); // 在这里处理响应... } catch (Exception ex) { // 在这里处理异常 } finally { httpClient.close(); }
      • 如果这对您有帮助,请回复并标记为已回答,以便其他人也可以看到。
      • 您可以添加它作为官方答案吗?我几乎无法理解代码的目的是什么。
      猜你喜欢
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多