简体   繁体   English

如何迭代Google表格中的所有行

[英]How to iterate all rows from Google Sheet

I'm working with Google Spreadsheets Api and Android Studio. 我正在使用Google Spreadsheets Api和Android Studio。 I'm reading info from a sheet, and showing the results into a List View. 我正在从工作表中读取信息,并将结果显示在列表视图中。 My problem is that I can only retrieve the info from the first row, but not from the next ones. 我的问题是我只能从第一行中检索信息,而不能从下一行中检索信息。 How can I do that? 我怎样才能做到这一点?

I have the next code: 我有下一个代码:

private class MakeRequestTask extends AsyncTask<Void, Void, List<String>> {
        private Exception mLastError = null;

        MakeRequestTask(GoogleAccountCredential credential) {

        }

        @Override
        protected List<String> doInBackground(Void... params) {
            try {
                return getDataFromApi();
            } catch (Exception e) {
                mLastError = e;
                cancel(true);
                return null;
            }
        }

        private List<String> getDataFromApi() throws IOException {

            String range = "Sheet1!A2:H";
            List<String> results = new ArrayList<String>();
            ValueRange response = mService.spreadsheets().values()
                    .get(spreadsheet_id, range)
                    .execute();
            List<List<Object>> values = response.getValues();
            if (values == null) {
                //No me deja mostrar toast aqui
            } else {
                for (List row : values) {
                        row.get(0);
                        row.get(1);
                        row.get(2);
                        row.get(5);
                        System.out.println("resultados:" + row.get(0) + ", " + row.get(1));
                    }
                }


            return results;
        }

Try to check the sample code on Reading multiples ranges 尝试检查有关读取倍数范围的示例代码

To read multiple discontinuous ranges, use a spreadsheets.values.batchGet , which lets you specify any number of ranges to retrieve: 要读取多个不连续的范围,请使用sheetssheets.values.batchGet ,它可以指定要检索的任意数量的范围:

Here is a sample Java code: 这是一个示例Java代码:

List<String> ranges = Arrays.asList(
        //Range names ...
        );
BatchGetValuesResponse result = service.spreadsheets().values().batchGet(spreadsheetId)
        .setRanges(ranges).execute();

Another thing is try to replace the for loop to: 另一件事是尝试将for循环替换为:

for (List row : values) {
    // Print columns A and E, which correspond to indices 0 and 4.
    System.out.printf("%s, %s\n", row.get(0), row.get(4));
}

You can specify the indices you needed. 您可以指定所需的索引。 In the example index 0 to 4. 在示例中,索引0到4。

Hope this is what you are looking for. 希望这是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM