如何编写和更新数据到谷歌电子表格android(api v4)

时间:2016-11-24 08:48:57

标签: android google-sheets

我已经关注了谷歌Google表格API Android快速入门提供的Android快速入门,并且能够从谷歌电子表格中检索数据,但我无法理解如何编写和更新单个或多个数据。

我从StackOverflow读取此代码,我觉得它很好但我不能理解如何在这里设置(valueRange)对象

this.mService.spreadsheets().values().update(spreadsheetId, range, valueRange)
                .setValueInputOption("RAW")
                .execute();

1 个答案:

答案 0 :(得分:3)

如果你仍然需要答案,或者是其他任何人:

我也遇到了同样的问题,从文档中我能够解决这个问题。 这是我将数据写入工作表的方法

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