| 15 | } |
| 16 | |
| 17 | func (c *DocsTablePinHeaderCmd) Run(ctx context.Context, flags *RootFlags) error { |
| 18 | docID, err := validateDocsTableMutationArgs(c.DocID, c.Table) |
| 19 | if err != nil { |
| 20 | return err |
| 21 | } |
| 22 | if c.Rows < 0 { |
| 23 | return usage("--rows must be >= 0") |
| 24 | } |
| 25 | if dryRunErr := dryRunExit(ctx, flags, "docs.table-row.pin-header", map[string]any{ |
| 26 | "documentId": docID, |
| 27 | "table": c.Table, |
| 28 | "rows": c.Rows, |
| 29 | "tab": c.Tab, |
| 30 | }); dryRunErr != nil { |
| 31 | return dryRunErr |
| 32 | } |
| 33 | |
| 34 | svc, loaded, selected, err := loadDocsSelectedTables(ctx, flags, docID, c.Tab, c.Table) |
| 35 | if err != nil { |
| 36 | return err |
| 37 | } |
| 38 | requests := make([]*docs.Request, 0, len(selected)) |
| 39 | results := make([]docsTableRowActionResult, 0, len(selected)) |
| 40 | for _, target := range selected { |
| 41 | rowCount := int64(len(target.table.TableRows)) |
| 42 | if c.Rows > rowCount { |
| 43 | return usagef("cannot pin %d header rows in table %d (table has %d rows)", c.Rows, target.index, rowCount) |
| 44 | } |
| 45 | requests = append(requests, &docs.Request{PinTableHeaderRows: &docs.PinTableHeaderRowsRequest{ |
| 46 | TableStartLocation: &docs.Location{Index: target.startIdx, TabId: loaded.tabID}, |
| 47 | PinnedHeaderRowsCount: c.Rows, |
| 48 | ForceSendFields: []string{"PinnedHeaderRowsCount"}, |
| 49 | }}) |
| 50 | results = append(results, docsTableRowActionResult{TableIndex: target.index, Value: c.Rows}) |
| 51 | } |
| 52 | if _, err := executeDocsTableRequests(ctx, svc, docID, loaded.full.RevisionId, requests); err != nil { |
| 53 | return fmt.Errorf("pin table header rows: %w", err) |
| 54 | } |
| 55 | return writeDocsTableRowActionResult(ctx, docID, loaded.tabID, "pin-header", "rows", results) |
| 56 | } |