完成快速入门教程后使用 google sheet api [英] Using the google sheets api after completing quickstart tutorial

查看:130
本文介绍了完成快速入门教程后使用 google sheet api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Android 应用开发还很陌生,所以请原谅我的幼稚.

I'm pretty new to Android app development so please forgive my naivety.

我目前正在尝试开发一款可以从 Google 电子表格中提取数据并将数据写入其中的应用.

I'm currently trying to develop an app that can pull data from a google spreadsheet and write data to it.

我已经完成了快速入门教程,所以我的代码与现在相同.一切正常.

I've completed the quickstart tutorial so my code is the same as that right now. It all works correctly.

我的问题是我需要能够从我自己的电子表格中读取,但我并不真正理解所使用的代码,所以我很难知道从哪里开始.

My issue is I need to be able to read from my own spreadsheet and I don't really understand the code used so I'm struggling to know where to start.

我看过这个 尝试以更少的步骤实现授权,我认为这可能会使代码更容易理解 - 但同样我缺乏知识意味着我不知道这如何适合我目前拥有的所有方法.

I've looked at this to try and implement the authorisation in fewer steps which I thought might make the code easier to understand - but again my lack of knowledge means I don't know how this fits into all the methods I currently have.

我查看了开发人员文档并尝试替换快速入门中的代码,该代码从应用中检索数据:

I've looked at the developer documentation and tried to replace the code from the quickstart which retrieves data from the app with this:

ValueRange result = service.spreadsheets().values().get(spreadsheetId, range).execute();
int numRows = result.getValues() != null ? result.getValues().size() : 0;
System.out.printf("%d rows retrieved.", numRows);

但这又与我已有的代码不同,因此不适合,因为 getDataFromApi() 方法需要 return 语句.我已经尝试将电子表格 ID 更改为我自己的电子表格,并将范围值更改为我需要的单元格,

But again this is different to the code I already have so doesn't fit in as the getDataFromApi() method requires a return statement. I've tried just changing the spreadsheetId to that of my own spreadsheet and changing the range value to the cells I need,

/**
         * Fetch a list of names and majors of students in a sample spreadsheet:
         * https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
         * @return List of names and majors
         * @throws IOException
         */
        private List<String> getDataFromApi() throws IOException {
            String spreadsheetId = "1-hL78Jm9-HijPx9UHthFxcXatkIhA2FR-AQ1lrCUbEg";
            String range = "Go Mix 12 0506!B6:D";
            List<String> results = new ArrayList<String>();
            ValueRange response = this.mService.spreadsheets().values()
                    .get(spreadsheetId, range)
                    .execute();
            List<List<Object>> values = response.getValues();
            if (values != null) {
                results.add("Name, Major");
                for (List row : values) {
                    results.add(row.get(1) + ", " + row.get(3));
                }
            }
            return results;
        }

但我显然是错误的,因为我收到了这个错误:索引 3 无效,大小为 3

but I'm clearly going about it wrong because I get this error: Invalid index 3, size is 3

我还关注了本教程 但它绕过了任何授权的需要,所以没有帮助.

I've also followed up to video 15 on this tutorial but it bypasses the need for any authorisation so didn't help.

基本上我遇到了麻烦,需要有人用简单的术语向我解释我如何使用快速入门教程中的代码来使用我自己的电子表格并修复该错误!或者(比我自己更喜欢)解释如何从头开始自己做.注意电子表格不能公开,所以我需要授权.

Basically I've hit a brick wall and need someone to explain to me in simple terms how I can either work with the code from the quickstart tutorial to work with my own spreadsheet and fix that error! OR (more preferable to myself) explain how to do it myself from scratch. NB the spreadsheet cannot be public so I will need authorisation.

我希望一切都有意义!

推荐答案

您要求检索三列数据(B 到 D),但随后尝试读取第四列 (rows.get(3)).在 Java(以及大多数编程语言)中,列表和数组是从 0 开始的.因此,如果您想要第一项,则调用 list.get(0),如果您想要第三项,则调用 list.get(2).这是显而易见的,因为错误消息告诉您您的索引 3 无效,因为列表的大小为 3 - 您正在尝试读取列表末尾以外的内容.

You are asking to retrieve three columns of data (B through D), but then attempting to read a fourth column (rows.get(3)). In Java (and in most programming languages), lists and arrays are 0-based. So if you want the first item, you call list.get(0), and if you want the third you call list.get(2). This is evident based on the fact that the error message tells you your index of 3 is invalid since the list is size 3 -- you're attempting to read something beyond the end of the list.

这篇关于完成快速入门教程后使用 google sheet api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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