gpt4 book ai didi

java - 如何使用 java 库在 google Sheets api 上设置自动换行策略

转载 作者:行者123 更新时间:2023-12-01 23:54:42 28 4
gpt4 key购买 nike

在这方面工作了一段时间,看到了一些与堆栈溢出接近的东西,但没有任何有上下文的好例子。我的大问题是获取工作表的 gridId。

final Request wrap = new Request()
.setRepeatCell(new RepeatCellRequest()
.setCell(new CellData()
// test to see what field gets set
// .setUserEnteredValue( new ExtendedValue().setStringValue(mContext.getString(R.string.lazy_dog)))
.setUserEnteredFormat(new CellFormat().setWrapStrategy("WRAP")))
.setRange(new GridRange()
.setSheetId(response.getSheets().get(0).getProperties().getSheetId())
.setStartRowIndex(2)
.setStartColumnIndex(1)
.setEndColumnIndex(2)
).setFields("*")
);

最佳答案

这是对我有用的整个代码块。我确信有更简洁的方法可以做到这一点。欢迎任何意见,但这对我有用。代码位于任务中,仅包含创建新工作表、添加列和格式设置的部分。

 SpreadsheetProperties properties = new SpreadsheetProperties();
properties.setTitle(new StringBuilder(mContext.getString(R.string.app_initials))
.append(" " + mUnit)
.append(" ")
.append(calendar.getTime().toString())
.toString());

Spreadsheet requestBody = new Spreadsheet();

GridProperties gridProperties = new GridProperties();
gridProperties.setFrozenRowCount(2).setColumnCount(7);

SheetProperties sheetProperties = new SheetProperties();
sheetProperties.setTitle("my new sheet");
sheetProperties
.setGridProperties(gridProperties);

Sheet sheet = new Sheet();
sheet.setProperties(sheetProperties);

List<Sheet> sheets = new ArrayList<>();
sheets.add(sheet);

requestBody.setSheets(sheets);
requestBody.setProperties(properties);

String rangeH = "A1:D1";

List<List<Object>> valuesH = new ArrayList<>();
Object o1 = "date", o2 = "description", o3 = "distance", o4 = "total " + mUnit, o5 = "", o6 = "total time";
valuesH.add(Arrays.asList(o1, o2, o3, o4, o5, o6));

ValueRange valueRangeH = new ValueRange();
valueRangeH.setMajorDimension("ROWS");
valueRangeH.setRange(rangeH);
valueRangeH.setValues(valuesH);

//Object formula = "=SUM(C1:C)";
String range = "D2:F2";

Object o7 = "=SUM(C1:C)", o8 = mUnit, o9 = "=SUM(F3:F)";

List<List<Object>> values = new ArrayList<>();

values.add(Arrays.asList(o7, o8,o9));

ValueRange valueRange = new ValueRange();
valueRange.setMajorDimension("ROWS");
valueRange.setRange(range);
valueRange.setValues(values);

Spreadsheet response = null;
AppendValuesResponse avResponse = null;

try {

Sheets.Spreadsheets.Create request = mService.spreadsheets().create(requestBody);
response = request.execute();

avResponse = this.mService.spreadsheets().values()
.append(response.getSpreadsheetId(), rangeH, valueRangeH)
.setValueInputOption("USER_ENTERED")
.execute();

//adds the formula
avResponse = this.mService.spreadsheets().values()
.append(response.getSpreadsheetId(), range, valueRange)
.setValueInputOption("USER_ENTERED")
.execute();

final Request wrap = new Request()
.setRepeatCell(new RepeatCellRequest()
.setCell(new CellData()
// test to see what field gets set
// .setUserEnteredValue( new ExtendedValue().setStringValue(mContext.getString(R.string.lazy_dog)))
.setUserEnteredFormat(new CellFormat().setWrapStrategy("WRAP")))
.setRange(new GridRange()
.setSheetId(response.getSheets().get(0).getProperties().getSheetId())
.setStartRowIndex(2)
.setStartColumnIndex(1)
.setEndColumnIndex(2)
).setFields("*")
);


List<Request> requests = new ArrayList<>();
requests.add(wrap);




this.mService.spreadsheets().batchUpdate(response.getSpreadsheetId(),
new BatchUpdateSpreadsheetRequest().setRequests(requests))
.execute();

关于java - 如何使用 java 库在 google Sheets api 上设置自动换行策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58207756/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com