diff --git a/CHANGELOG.md b/CHANGELOG.md index 861521fad..e7d8f9556 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.48.1 - Unreleased +- Sheets: return an empty `notes` or `links` array from `sheets notes --json` and `sheets links --json` when the range has none, instead of `null`. With `--results-only`, empty notes now print `[]` instead of `null`, and empty links print `[]` instead of the whole envelope. (#1314) — thanks @saariuslystoned. + ## 0.48.0 - 2026-11-05 **Highlights:** New backups stay local until you configure a remote; existing configurations keep working. Store service-account keys in the keyring, search Gmail IDs efficiently, and use environment-backed watch tokens, with Calendar fixes and security dependency updates. diff --git a/internal/cmd/sheets_links.go b/internal/cmd/sheets_links.go index 6186ac77b..2f3b919b9 100644 --- a/internal/cmd/sheets_links.go +++ b/internal/cmd/sheets_links.go @@ -50,7 +50,7 @@ func (c *SheetsLinksGetCmd) Run(ctx context.Context, flags *RootFlags) error { return err } - var links []sheetsCellLink + links := []sheetsCellLink{} for _, sheet := range resp.Sheets { if sheet == nil { diff --git a/internal/cmd/sheets_notes.go b/internal/cmd/sheets_notes.go index c3ea31f20..281346d8b 100644 --- a/internal/cmd/sheets_notes.go +++ b/internal/cmd/sheets_notes.go @@ -40,7 +40,7 @@ func (c *SheetsNotesCmd) Run(ctx context.Context, flags *RootFlags) error { return err } - var notes []sheetsCellNote + notes := []sheetsCellNote{} for _, sheet := range resp.Sheets { if sheet == nil { diff --git a/internal/cmd/sheets_notes_links_empty_test.go b/internal/cmd/sheets_notes_links_empty_test.go new file mode 100644 index 000000000..a8d6b01dd --- /dev/null +++ b/internal/cmd/sheets_notes_links_empty_test.go @@ -0,0 +1,55 @@ +package cmd + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +// A range with no notes or links must still be a JSON array, so scripts can +// use `jq '.[]'` or `length` on it, with and without --results-only. +func TestSheetsNotesAndLinks_EmptyRangeJSONIsEmptyArray(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{ + "sheets": []map[string]any{{ + "properties": map[string]any{"title": "Sheet1"}, + "data": []map[string]any{{ + "rowData": []map[string]any{{"values": []map[string]any{{"formattedValue": "plain"}}}}, + }}, + }}, + }) + })) + defer srv.Close() + svc := newSheetsServiceFromServer(t, srv) + + for _, tc := range []struct { + command string + key string + }{ + {command: "notes", key: "notes"}, + {command: "links", key: "links"}, + } { + envelope := executeWithSheetsTestService(t, []string{"--json", "--account", "a@b.com", "sheets", tc.command, "s1", "Sheet1!A1:A1"}, svc) + if envelope.err != nil { + t.Fatalf("%s: %v", tc.command, envelope.err) + } + var got map[string]json.RawMessage + if err := json.Unmarshal([]byte(envelope.stdout), &got); err != nil { + t.Fatalf("%s json: %v (%q)", tc.command, err, envelope.stdout) + } + if string(got[tc.key]) != "[]" { + t.Fatalf("%s: %q = %s, want []", tc.command, tc.key, got[tc.key]) + } + + unwrapped := executeWithSheetsTestService(t, []string{"--json", "--results-only", "--account", "a@b.com", "sheets", tc.command, "s1", "Sheet1!A1:A1"}, svc) + if unwrapped.err != nil { + t.Fatalf("%s --results-only: %v", tc.command, unwrapped.err) + } + if strings.TrimSpace(unwrapped.stdout) != "[]" { + t.Fatalf("%s --results-only = %s, want []", tc.command, strings.TrimSpace(unwrapped.stdout)) + } + } +}