R.Donohoe
R.Donohoe

Reputation: 59

Reading Google Spreadsheet (Java): IndexOutOfBounds exception when row's last cell(s) empty

My method returns a range from a google spreadsheet, and cycles through its rows, and then each cell in a row. When the final cell/ cells of a given row in the spreadsheet range are empty, the method fails with an IndexOutOfBoundsException (Index 36 out of bounds for length 36). I think this is telling me that the length of a row returned is only the length up to the final non-blank cell.

It is necessary in my application that blank cells at the end of rows can be handled; I need each row to be considered its full length with my variable currentCellValue being assigned an empty string for a blank cell. Can someone help please?

My method:

        List<List<Object>> tableContents = SheetsQuickstart.getValues("FC_Input!A2:AN900");

        int[] columnNumbers = ColumnOps.fillColumnNumbersArray();
        
        //Cycle through rows
        for (List row : tableContents) {
            String currentCellValue = "";
            int currentColumnNumber = -1;
            
            //Cycle through cells
            for (int column : columnNumbers) {
                currentColumnNumber++;
                currentCellValue = row.get(currentColumnNumber).toString();
            }
        }
    }

Upvotes: 0

Views: 209

Answers (1)

Kessy
Kessy

Reputation: 1994

This is not possible to do, on the documentation is stated that if the last empty rows are empty the method omits them.

You can find it on the Read a single range documentation:

The following spreadsheets.values.get request reads the values stored in the range Sheet1!A1:D5 and returns them in the response. Empty trailing rows and columns are omitted.

Upvotes: 1

Related Questions