Hello,
When reading a sheet using Google Sheets API, I’m getting a panic “index out of range” if any cells are empty.
I’m using this package: google.golang.org/api/sheets/v4 which responds with a Values interface (resp.Values).
What is the easiest way to just leave it blank without panicking if it’s blank?
if len(resp.Values) == 0 {
fmt.Println("No data found.")
} else {
var csvOutput string = "ShipDate, BOL, SCAC, PRO, PO, SO, Dest"
var shipDate, BOL, SCAC, PRO, PO, SO, dest string
for _, row := range resp.Values {
// Read values and comma separate into a string.
shipDate = fmt.Sprintf("%v", row[0])[0:10]
SCAC = fmt.Sprintf("%v", row[2])
BOL = fmt.Sprintf("%v", row[3])
PRO = fmt.Sprintf("%v", row[4])
PO = fmt.Sprintf("%v", row[5])
SO = fmt.Sprintf("%v", row[6])
dest = fmt.Sprintf("%v", row[7])
}