我遵循了 google Google Sheets API Android Quickstart 提供的 Android 快速入门,并且能够从 google 电子表格中检索数据,但我无法理解如何编写和更新单个或多个数据。
我从 StackOverflow 阅读了这段代码,我认为这很好,但我不明白如何在这里设置 (valueRange) 对象
this.mService.spreadsheets().values().update(spreadsheetId, range, valueRange)
.setValueInputOption("RAW")
.execute();
如果您仍然需要答案,或者其他人需要答案:
我也遇到了同样的问题,从文档中,我能够解决这个问题。
这是我将数据写入工作表的方法
private void writeDataToApi() throws IOException {
String spreadsheetId = "the_spreadsheet_id_like_the_google_example";
String range = "YourSheetName!A1:B1"; //Read the docs on how these ranges work.
//Currently, this is the range of a single row which would return
//as [[objA, objB]] if major dimension is set as ROW.(default).
// would be [[objA],[objB]] if its set to COLUMN. Read the doc for more info.
//for the values that you want to input, create a list of object lists
List<List<Object>> values = new ArrayList<>();
//Where each value represents the list of objects that is to be written to a range
//I simply want to edit a single row, so I use a single list of objects
List<Object> data1 = new ArrayList<>();
data1.add("objA");
data1.add("objB");
//There are obviously more dynamic ways to do these, but you get the picture
values.add(data1);
//Create the valuerange object and set its fields
ValueRange valueRange = new ValueRange();
valueRange.setMajorDimension("ROWS");
valueRange.setRange(range);
valueRange.setValues(values);
//then gloriously execute this copy-pasted code ;)
this.mService.spreadsheets().values()
.update(spreadsheetId, range, valueRange)
.setValueInputOption("RAW")
.execute();
//Try calling this method before executing the readDataFromApi method,
//and you'll see the immediate change
}
我希望这有帮助。
欲了解更多信息,请参阅docs https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update
编辑:还记得更改范围SheetsScopes.SPREADSHEETS_READONLY
to SheetsScopes.SPREADSHEETS
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)