| 377 | } |
| 378 | |
| 379 | func resolveSheetsTable(input string, tables []sheetsTableItem) (sheetsTableItem, bool, error) { |
| 380 | in := strings.TrimSpace(input) |
| 381 | if in == "" { |
| 382 | return sheetsTableItem{}, false, nil |
| 383 | } |
| 384 | |
| 385 | for _, table := range tables { |
| 386 | if table.TableID == in { |
| 387 | return table, true, nil |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | var matches []sheetsTableItem |
| 392 | for _, table := range tables { |
| 393 | if strings.EqualFold(table.Name, in) { |
| 394 | matches = append(matches, table) |
| 395 | } |
| 396 | } |
| 397 | switch len(matches) { |
| 398 | case 0: |
| 399 | return sheetsTableItem{}, false, nil |
| 400 | case 1: |
| 401 | return matches[0], true, nil |
| 402 | default: |
| 403 | parts := make([]string, 0, len(matches)) |
| 404 | for _, match := range matches { |
| 405 | parts = append(parts, fmt.Sprintf("%s (%s)", match.Name, match.TableID)) |
| 406 | } |
| 407 | return sheetsTableItem{}, false, usagef("ambiguous table %q; matches: %s", in, strings.Join(parts, ", ")) |
| 408 | } |
| 409 | } |