gpt4 book ai didi

android - 如何写入和更新数据到 google 电子表格 android (api v4)

转载 作者:行者123 更新时间:2023-12-04 02:08:51 25 4
gpt4 key购买 nike

我遵循了 google Google Sheets API Android Quickstart 提供的 Android Quickstart 并能够从 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

编辑:还记得将范围从 SheetsScopes.SPREADSHEETS_READONLY 更改为 SheetsScopes.SPREADSHEETS

关于android - 如何写入和更新数据到 google 电子表格 android (api v4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40781620/

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