2017-04-26 18 views
0

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

我试图设置自定义格式,但我找不到任何有用的大小。

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

回答

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中谷歌表的API。

希望这会有所帮助。

+0

是请求,但我该怎么办这与Java和个人单元? –

+0

@Peter你真的是指*单个单元格*或一行高度和一列宽度? – pnuts

+0

我需要设置单个单元格大小。 –

0

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

+0

我可以使用Java代码来设置列的大小吗? –

+0

您必须发送JSON请求,如此链接所示:https://developers.google.com/sheets/api/samples/rowcolumn –

+0

JSONObject json = new JSONObject(); json.put(“someKey”,“someValue”); CloseableHttpClient httpClient = HttpClientBuilder.create()。建立(); try { HttpPost request = new HttpPost(“http:// 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(); } –

1

你可以通过一个JSON请求API更改列的大小在这个环节看出: 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(); } 
+0

有没有官方的java api的方法? –

相关问题