【问题标题】:How to write and update data to google spreadsheet android (api v4)如何将数据写入和更新到 google 电子表格 android (api v4)
【发布时间】:2016-11-24 08:48:56
【问题描述】:

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

我从 StackOverflow 中阅读了这段代码,我认为这很好但我不明白如何在这里设置 (valueRange) 对象

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

【问题讨论】:

  • 我也有同样的问题...请帮忙....

标签: android google-sheets


【解决方案1】:

如果您仍然需要答案,或者其他需要答案的人:

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

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

【讨论】:

  • @Ramkumar P 希望这会有所帮助
  • 嗨,@Mofe-hendy:当我尝试向工作表添加值时,它给了我以下错误,你能帮忙吗? {“代码”:400,“错误”:[{“域”:“全局”,“消息”:“无效值[5] [0]:struct_value {\n}\n”,“原因”:“badRequest " } ], "message" : "无效值[5][0]: struct_value {\n}\n", "status" : "INVALID_ARGUMENT" }
  • 我正在发送这样的数据 List> value = new ArrayList();列表 valueRow = new ArrayList(); for (int i = 0; i
  • 看起来你遇到了某种参数冲突,你记得指定range吗?
  • 是的,我已经指定了范围 Data!A1:H,
猜你喜欢
  • 1970-01-01
  • 2020-01-30
  • 1970-01-01
  • 2017-06-05
  • 1970-01-01
  • 2016-12-28
  • 1970-01-01
  • 1970-01-01
  • 2017-06-20
相关资源
最近更新 更多