如何编写和更新数据到谷歌电子表格android(api v4) [英] How to write and update data to google spreadsheet android (api v4)

查看:96
本文介绍了如何编写和更新数据到谷歌电子表格android(api v4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循谷歌Google Sheets API Android Quickstart提供的Android快速入门,能够从Google电子表格中检索数据,但我无法理解如何编写和更新单个或多个数据。

我从StackOverflow中读取这段代码,我认为这很好,但我无法理解如何在这里设置(valueRange)对象

  this.mService.spreadsheets()。values()。update(spreadsheetId,range,valueRange)
.setValueInputOption(RAW)
。执行();


解决方案

如果您仍然需要这个答案,否则谁做:

我也面临同样的问题,从文档中,我能够解决这个问题。
这里有一个方法,我把数据写入表单中。

  private void writeDataToApi()throws IOException {
String spreadsheetId =the_spreadsheet_id_like_the_google_example;
String range =YourSheetName!A1:B1; //阅读有关这些范围如何工作的文档。
//目前,如果主要维度设置为ROW(默认值),则这是单行的范围,它将返回
//作为[[objA,objB]]。
//如果将其设置为COLUMN,则会为[[objA],[objB]]。阅读文档以获取更多信息。

//对于要输入的值,创建一个对象列表
List< List< Object>> values = new ArrayList<>();

//其中每个值表示要写入范围的对象列表
//我只是想编辑一行,因此我使用单个对象列表
List< Object> data1 = new ArrayList<>();
data1.add(objA);
data1.add(objB);

//显然有更多动态的方式来做这些,但是你得到的图片是
values.add(data1);

//创建valuerange对象并设置其字段
ValueRange valueRange = new ValueRange();
valueRange.setMajorDimension(ROWS);
valueRange.setRange(range);
valueRange.setValues(values);

//然后光荣地执行这个复制粘贴的代码;)
this.mService.spreadsheets()。values()
.update(spreadsheetId,range,valueRange)
.setValueInputOption(RAW)
.execute();

//在执行readDataFromApi方法之前尝试调用此方法,
//并且您会看到立即更改

}

我希望这会有所帮助。
欲了解更多信息,请参阅 docs



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


I have followed the Android Quickstart provided by google Google Sheets API Android Quickstart and was able to retrieve data from the google spreadsheet but I am not able to understand how to write and update single or multiple data.

I read this code from StackOverflow, I think It's good but I can't understand how to set (valueRange) object here

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

解决方案

In case you still need the answer, or for anyone else who does:

I faced the same issue too, and from the docs, I was able to work this out. Here's a method in which i write the data to the sheet

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

        }

I hope this helped. And for more info, pls see the docs

EDIT: Also remember to change scope from SheetsScopes.SPREADSHEETS_READONLY to SheetsScopes.SPREADSHEETS

这篇关于如何编写和更新数据到谷歌电子表格android(api v4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆