In the Google sheets example there is this piece of code down the bottom that loops over the rows in a spreadsheet:
for _, row := range resp.Values {
// Print columns A and E, which correspond to indices 0 and 4.
fmt.Printf("%s, %s
", row[0], row[4])
}
However this code gives an error if there is an empty row in the spreadsheet due to referencing row[0]
when row
is an empty interface of size two <[]interface {}> (length: 0, cap: 0)
A simple if statement to check if row
is empty doesn't work as row == nil
shows false
.
How can I check if row
is emmpty?
How can I check if row is emmpty?
if len(row) == 0 { // row is empty
But since you need to access index 4 you might as well check for what you actually need:
if len(row) < 5 { // row doesn't have an index 4