mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-10-26 05:29:45 +00:00
63d973f40c
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
34 lines
1.1 KiB
Go
34 lines
1.1 KiB
Go
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
|
||
}
|