我要在工作表中添加标题和公式,稍后再添加数据。表创建工作,添加公式工作。稍后,我还可以将行添加到此表中。标头添加失败,我也不明白为什么。
SpreadsheetProperties properties = new SpreadsheetProperties();
properties.setTitle("My formula");
Spreadsheet requestBody = new Spreadsheet();
requestBody.setProperties(properties);
Object o1 = "a1", o2 = "b1", o3 = "c1",o4 ="d1";
Object header = Arrays.asList(o1,o2,o3,o4);
String rangeH = "A1:D1";
已编辑:这是我的突破
列表数据H = new ArrayList <>();
dataH.add(header);
List<List<Object>> valuesH = new ArrayList<>();
valuesH.add(dataH);
ValueRange valueRangeH = new ValueRange();
valueRangeH.setMajorDimension("ROWS");
valueRangeH.setRange(rangeH);
valueRangeH.setValues(valuesH);
Object formula = "=SUM(C1:C)";
String range = "D2";
List<Object> data = new ArrayList<>();
data.add(formula);
List<List<Object>> values = new ArrayList<>();
values.add(data);
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(mSpreadsheetId, rangeH, valueRangeH)
.setValueInputOption("USER_ENTERED")
.execute();
//adds the formula
avResponse = this.mService.spreadsheets().values()
.append(response.getSpreadsheetId(), range, valueRange)
.setValueInputOption("USER_ENTERED")
.execute();
答案 0 :(得分:0)
这样我就不会浪费时间回答这个问题了。虽然我相信我可以在一个要求做到这一点:
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
month ++;
SpreadsheetProperties properties = new SpreadsheetProperties();
properties.setTitle(calendar.getTime().toString());
Spreadsheet requestBody = new Spreadsheet();
requestBody.setProperties(properties);
String rangeH = "A1:D1";
List<List<Object>> valuesH = new ArrayList<>();
Object o1 = "date", o2 = "description", o3 = "distance", o4 = "total";
valuesH.add(Arrays.asList(o1,o2,o3,o4));
ValueRange valueRangeH = new ValueRange();
valueRangeH.setMajorDimension("ROWS");
valueRangeH.setRange(rangeH);
valueRangeH.setValues(valuesH);
Object formula = "=SUM(C1:C)";
String range = "D2";
List<Object> data = new ArrayList<>();
data.add(formula);
List<List<Object>> values = new ArrayList<>();
values.add(data);
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();
} catch (Exception ex) {
onCancelled("dead");
Log.e(TAG, ex.getMessage());
}
我的错误是将其添加到之前valuesH附加标题对象阵列。