2017-04-26 20 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的方法? –

相關問題