Skip to content

Commit

Permalink
truncate audit plan content when exporting for Excel view
Browse files Browse the repository at this point in the history
  • Loading branch information
BugsGuru committed Aug 8, 2024
1 parent f63f8eb commit df0d857
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sqle/api/controller/v1/instance_audit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,7 @@ func GetInstanceAuditPlanSQLExport(c echo.Context) error {
}
for _, rowMap := range rows {
for col, h := range head {
toWrite[col] = rowMap[h.Name]
toWrite[col] = utils.TruncateAndMarkForExcelCell(rowMap[h.Name])
}
if err = csvWriter.Write(toWrite); err != nil {
return controller.JSONBaseErrorReq(c, err)
Expand Down
35 changes: 35 additions & 0 deletions sqle/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,38 @@ func GenerateRandomString(halfLength int) string {
rand.Read(bytes)
return fmt.Sprintf("%x", bytes)
}

// TruncateStringByChars 按字符数截取字符串
func TruncateStringByChars(s string, maxChars uint) string {
// 字节数不大于 maxChars ,那字符数肯定不大于 maxChars
if uint(len(s)) <= maxChars {
return s
}

// 逐个rune遍历s,i为每个rune起始的字节索引
// 如:s="a一b二c";汉字为多字节字符,len(s)=9;i依次为:0  1  4  5  8
// ^a ^一 ^b ^二 ^c
var charsCount uint
for i := range s {
if charsCount == maxChars {
// 达到截取的字符数了,将字符截取至此时rune的字节索引
return s[:i]
}
// 未达到要截取的字符数,继续获取下一个rune
charsCount++
}
// 字符串字符数不足 maxChars
return s
}

const excelCellMaxChars = 32766

// TruncateAndMarkForExcelCell 对超长字符串进行截取,以符合Excel类工具对单元格字符数上限的限制
func TruncateAndMarkForExcelCell(s string) string {
truncated := TruncateStringByChars(s, excelCellMaxChars-4)
if truncated != s {
// 截取了的话,做标记
return truncated + " ..."
}
return s
}

0 comments on commit df0d857

Please sign in to comment.