问题描述
List< RowData> 使用这个Java代码从Java应用程序生成Google工作表数据。 rowDataValues = new ArrayList<>();
列表< CellData> headerValues = new ArrayList<>();
headerValues.add(new CellData()。setNote(ID)
.setUserEnteredValue(new ExtendedValue()
.setStringValue(#))。setUserEnteredFormat(myFormat));
.setStringValue(Environment))。setUserEnteredFormat(myFormat));
.........
headerValues.add(新的CellData()
.setUserEnteredValue(new ExtendedValue()
.setStringValue ( 名称))setUserEnteredFormat(myFormat))。
RowData setHeaderValues = new RowData();
setHeaderValues.setValues(headerValues);
rowDataValues.add(setHeaderValues);
requests.add(new Request()
.setUpdateCells(new UpdateCellsRequest()
.setStart(new GridCoordinate()
.setSheetId(randomSheetId)
.setRowIndex(0)
.setColumnIndex(0))
.setRows(rowDataValues)
.setFields(*)));
BatchUpdateSpreadsheetRequest body = new BatchUpdateSpreadsheetRequest()。setRequests(requests);
BatchUpdateSpreadsheetResponse response = service.spreadsheets()。batchUpdate(spreadsheetId,body).execute();
如何为每列设置自定义宽度?
更新:
可能的解决方案:
新的UpdateDimensionPropertiesRequest ().setRange
(
new DimensionRange()
.setDimension(COLUMNS)
.setStartIndex(0).setEndIndex(1)
)
.setProperties(new DimensionProperties()。setPixelSize(400))。setFields(pixelSize));
您需要创建。在你的情况下,你可以使用这个示例代码,它增加了第一列的大小(startIndex = 0,endIndex = 1)。
requests.add(new Request()。setUpdateDimensionProperties(
new UpdateDimensionPropertiesRequest()
.setRange
new DimensionRange()
.setSheetId(randomSheetId)
.setDimension(COLUMNS)
.setStartIndex(0).setEndIndex(1)
)
.setProperties(new DimensionProperties()。setPixelSize(400))。setFields(pixelSize))));
我们使用 setDimension(COLUMNS)
要更改列的宽度,可以使用 setDimension(ROWS)
更改行高。
@PeterPenzov评论的其他问题
当sheetId设置不正确时,会出现此错误。
从API v4文档是;
因此,您需要设置DimensionRange的sheetId属性目的。在你的情况下,你需要使用sheetId作为 randomSheetId
,我更新了上面的代码。
您想从电子表格文档中获取表格,您可以使用此代码(java 8)来获取列表输出。
电子表格电子表格= service.spreadsheets()。get(spreadsheetId).execute();
spreadsheet.getSheets()。stream()
.map(s-> s.getProperties()。getSheetId())
.forEach(System.out :: println);
I'm using this Java code to generate Google sheets data from Java application.
List<RowData> rowDataValues = new ArrayList<>();
List<CellData> headerValues = new ArrayList<>();
headerValues.add(new CellData().setNote("ID")
.setUserEnteredValue(new ExtendedValue()
.setStringValue("#")).setUserEnteredFormat(myFormat));
.setStringValue("Environment")).setUserEnteredFormat(myFormat));
.........
headerValues.add(new CellData()
.setUserEnteredValue(new ExtendedValue()
.setStringValue("Name")).setUserEnteredFormat(myFormat));
RowData setHeaderValues = new RowData();
setHeaderValues.setValues(headerValues);
rowDataValues.add(setHeaderValues);
requests.add(new Request()
.setUpdateCells(new UpdateCellsRequest()
.setStart(new GridCoordinate()
.setSheetId(randomSheetId)
.setRowIndex(0)
.setColumnIndex(0))
.setRows(rowDataValues)
.setFields("*")));
BatchUpdateSpreadsheetRequest body = new BatchUpdateSpreadsheetRequest().setRequests(requests);
BatchUpdateSpreadsheetResponse response = service.spreadsheets().batchUpdate(spreadsheetId, body).execute();
How I can set custom width for each column?
Update:
Possible solution:
new UpdateDimensionPropertiesRequest().setRange
(
new DimensionRange()
.setDimension("COLUMNS")
.setStartIndex(0).setEndIndex(1)
)
.setProperties(new DimensionProperties().setPixelSize(400)).setFields("pixelSize"));
You need to create UpdateDimensionPropertiesRequest. In your case you can use this sample code, which increase size of first column (startIndex =0, endIndex = 1) .
requests.add(new Request().setUpdateDimensionProperties(
new UpdateDimensionPropertiesRequest()
.setRange(
new DimensionRange()
.setSheetId(randomSheetId)
.setDimension("COLUMNS")
.setStartIndex(0).setEndIndex(1)
)
.setProperties(new DimensionProperties().setPixelSize(400)).setFields("pixelSize"))));
We used setDimension("COLUMNS")
to change column(s) width, you can use setDimension("ROWS")
to change row(s) height.
Additional problem which @PeterPenzov commented
You'll get this error when your sheetId is not set proper.
From API v4 documentation SheetId is ;
So you need to set sheetId property of DimensionRange Object. In your case you need to use your sheetId as randomSheetId
, i updated the code above.
For additional info if you want to get your sheets from your spreadsheet document you can use this code(java 8) to get list output.
Spreadsheet spreadsheet = service.spreadsheets().get(spreadsheetId).execute();
spreadsheet.getSheets().stream()
.map(s->s.getProperties().getSheetId())
.forEach(System.out::println);
这篇关于在Google表格中设置自定义列宽度大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!