Files
pulse/pkg/reporting/csv_text.go
T
pulse-triage[bot] 63d973f40c Keep report CSV text from becoming spreadsheet formulas
Escape formula-like external text in single-resource, fleet and VM inventory CSV exports, including saved inventory reports. Preserve field boundaries, numeric measurements, ordinary text and the runtime inventory.

Contract-Neutral: Restores safe literal-text handling in existing report CSV cells; columns, numeric values, endpoints, authorization, archive formats and scheduling contracts are unchanged.
Change-source: pulse-maintainer
2026-10-25 03:45:08 +00:00

34 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package reporting
import (
"strings"
"unicode"
)
// spreadsheetText keeps externally supplied text from becoming a spreadsheet
// formula. CSV quoting alone only protects field boundaries, not cell types.
// Prefix the original value (without trimming or rewriting it) with the usual
// spreadsheet text marker. Only text cells use this helper: generated numbers,
// including negative measurements, must remain numbers.
func spreadsheetText(value string) string {
if value == "" {
return value
}
// Some importers treat these leading controls specially, even before text.
switch value[0] {
case '\t', '\r', '\n':
return "'" + value
}
// Importers may ignore whitespace or a byte-order mark before a formula.
leading := strings.TrimLeftFunc(value, func(r rune) bool {
return unicode.IsSpace(r) || r == '\uFEFF'
})
if strings.HasPrefix(leading, "=") || strings.HasPrefix(leading, "+") ||
strings.HasPrefix(leading, "-") || strings.HasPrefix(leading, "@") ||
strings.HasPrefix(leading, "=") || strings.HasPrefix(leading, "+") ||
strings.HasPrefix(leading, "-") || strings.HasPrefix(leading, "@") {
return "'" + value
}
return value
}