Google Sheets API v4 - How to get the last row with value?

微笑、不失礼 提交于 2019-12-17 19:39:22

问题


How to get the last row with value in the new Google Sheets API v4 ?

i use this to get a range from a sheet:

mService.spreadsheets().values().get("ID_SHEET", "Sheet1!A2:B50").execute();

how to detect the last row with value in the sheet ?


回答1:


You can set the range to "A2:D" and this would fetch as far as the last data row in your sheet.




回答2:


I managed to get it by counting the total rows from current Sheets. Then append the data to the next row.

rowcount = this.mService.spreadsheets().values().get(spreadsheetId, range).execute().getValues().size()



回答3:


You don't need to. Set a huge range (for example A2:D5000) to guarantee that all your rows will be located in it. I don't know if it has some further impact, may be increased memory consumption or something, but for now it's OK.

private List<String> getDataFromApi() throws IOException {
        String spreadsheetId = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms"; 
        String range = "A2:D5000";
        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(0) + ", " + row.get(3));
            }
        }
        return results;
    }

Look at the loop for (List row : values). If you have two rows in your table you will get two elements in values list.



来源:https://stackoverflow.com/questions/37331756/google-sheets-api-v4-how-to-get-the-last-row-with-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!