|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "google.golang.org/api/sheets/v4" |
| 11 | + |
| 12 | + "github.com/steipete/gogcli/internal/outfmt" |
| 13 | + "github.com/steipete/gogcli/internal/ui" |
| 14 | +) |
| 15 | + |
| 16 | +type SheetsTableAppendCmd struct { |
| 17 | + SpreadsheetID string `arg:"" name:"spreadsheetId" help:"Spreadsheet ID"` |
| 18 | + TableID string `arg:"" name:"tableId" help:"Table ID or table name"` |
| 19 | + Values []string `arg:"" optional:"" name:"values" help:"Values (comma-separated rows, pipe-separated cells)"` |
| 20 | + ValueInput string `name:"input" help:"Value input option: RAW or USER_ENTERED" default:"USER_ENTERED"` |
| 21 | + ValuesJSON string `name:"values-json" help:"Values as JSON 2D array"` |
| 22 | +} |
| 23 | + |
| 24 | +func (c *SheetsTableAppendCmd) Run(ctx context.Context, flags *RootFlags) error { |
| 25 | + u := ui.FromContext(ctx) |
| 26 | + spreadsheetID := normalizeGoogleID(strings.TrimSpace(c.SpreadsheetID)) |
| 27 | + in := strings.TrimSpace(c.TableID) |
| 28 | + if spreadsheetID == "" { |
| 29 | + return usage("empty spreadsheetId") |
| 30 | + } |
| 31 | + if in == "" { |
| 32 | + return usage("empty tableId") |
| 33 | + } |
| 34 | + |
| 35 | + values, err := parseSheetsAppendValues(c.ValuesJSON, c.Values) |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + valueInputOption := strings.TrimSpace(c.ValueInput) |
| 40 | + if valueInputOption == "" { |
| 41 | + valueInputOption = sheetsDefaultValueInputOption |
| 42 | + } |
| 43 | + |
| 44 | + account, err := requireAccount(flags) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + svc, err := newSheetsService(ctx, account) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + tables, err := fetchSpreadsheetTables(ctx, svc, spreadsheetID) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + table, found, err := resolveSheetsTable(in, tables) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + if !found { |
| 63 | + return usagef("unknown table %q", in) |
| 64 | + } |
| 65 | + if strings.TrimSpace(table.A1) == "" { |
| 66 | + return fmt.Errorf("table %q has no bounded A1 range", table.TableID) |
| 67 | + } |
| 68 | + if widthErr := validateSheetsTableAppendWidth(table, values); widthErr != nil { |
| 69 | + return widthErr |
| 70 | + } |
| 71 | + |
| 72 | + if dryRunErr := dryRunExit(ctx, flags, "sheets.table.append", map[string]any{ |
| 73 | + "spreadsheet_id": spreadsheetID, |
| 74 | + "table_id": table.TableID, |
| 75 | + "name": table.Name, |
| 76 | + "range": table.A1, |
| 77 | + "values": values, |
| 78 | + "value_input_option": valueInputOption, |
| 79 | + "insert_data_option": "INSERT_ROWS", |
| 80 | + }); dryRunErr != nil { |
| 81 | + return dryRunErr |
| 82 | + } |
| 83 | + |
| 84 | + resp, err := svc.Spreadsheets.Values.Append(spreadsheetID, table.A1, &sheets.ValueRange{Values: values}). |
| 85 | + ValueInputOption(valueInputOption). |
| 86 | + InsertDataOption("INSERT_ROWS"). |
| 87 | + Do() |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + if resp == nil || resp.Updates == nil { |
| 92 | + return fmt.Errorf("append response missing update metadata") |
| 93 | + } |
| 94 | + |
| 95 | + if outfmt.IsJSON(ctx) { |
| 96 | + return outfmt.WriteJSON(ctx, os.Stdout, map[string]any{ |
| 97 | + "tableId": table.TableID, |
| 98 | + "name": table.Name, |
| 99 | + "tableRange": table.A1, |
| 100 | + "updatedRange": resp.Updates.UpdatedRange, |
| 101 | + "updatedRows": resp.Updates.UpdatedRows, |
| 102 | + "updatedColumns": resp.Updates.UpdatedColumns, |
| 103 | + "updatedCells": resp.Updates.UpdatedCells, |
| 104 | + }) |
| 105 | + } |
| 106 | + |
| 107 | + u.Out().Printf("Appended %d cells to %s", resp.Updates.UpdatedCells, resp.Updates.UpdatedRange) |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +func parseSheetsAppendValues(valuesJSON string, values []string) ([][]interface{}, error) { |
| 112 | + switch { |
| 113 | + case strings.TrimSpace(valuesJSON) != "": |
| 114 | + b, err := resolveInlineOrFileBytes(valuesJSON) |
| 115 | + if err != nil { |
| 116 | + return nil, fmt.Errorf("read --values-json: %w", err) |
| 117 | + } |
| 118 | + var parsed [][]interface{} |
| 119 | + if err := json.Unmarshal(b, &parsed); err != nil { |
| 120 | + return nil, fmt.Errorf("invalid JSON values: %w", err) |
| 121 | + } |
| 122 | + if len(parsed) == 0 { |
| 123 | + return nil, fmt.Errorf("provide at least one row") |
| 124 | + } |
| 125 | + return parsed, nil |
| 126 | + case len(values) > 0: |
| 127 | + rawValues := strings.Join(values, " ") |
| 128 | + rows := strings.Split(rawValues, ",") |
| 129 | + parsed := make([][]interface{}, 0, len(rows)) |
| 130 | + for _, row := range rows { |
| 131 | + cells := strings.Split(strings.TrimSpace(row), "|") |
| 132 | + rowData := make([]interface{}, len(cells)) |
| 133 | + for i, cell := range cells { |
| 134 | + rowData[i] = strings.TrimSpace(cell) |
| 135 | + } |
| 136 | + parsed = append(parsed, rowData) |
| 137 | + } |
| 138 | + return parsed, nil |
| 139 | + default: |
| 140 | + return nil, fmt.Errorf("provide values as args or via --values-json") |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func validateSheetsTableAppendWidth(table sheetsTableItem, values [][]interface{}) error { |
| 145 | + if len(table.Columns) == 0 { |
| 146 | + return nil |
| 147 | + } |
| 148 | + width := len(table.Columns) |
| 149 | + for i, row := range values { |
| 150 | + if len(row) > width { |
| 151 | + return usagef("row %d has %d cells, but table %q has %d columns", i+1, len(row), table.Name, width) |
| 152 | + } |
| 153 | + } |
| 154 | + return nil |
| 155 | +} |
0 commit comments