7 Commits

Author SHA1 Message Date
ZeroHawkeye
da0e7758bf 更新文档和代码以支持字体名称的向后兼容,调整段落和图像对齐方法的错误处理 2025-09-24 01:19:08 +00:00
ZeroHawkeye
7197afba42 Merge pull request #24 from CoffeeSwt/main
 feat: 混合格式段落字体下划线和删除线
2025-09-24 08:48:28 +08:00
ZeroHawkeye
31118d7bc5 更新 GetCellText 方法,返回单元格内所有段落与 Run 的完整文本,并以换行符分隔,修复多行内容丢失的问题 2025-09-24 00:46:23 +00:00
徐静波
33aaa5c65c 修改高亮,文本字体使用中文名称 2025-08-06 15:39:54 +08:00
徐静波
bf7e854a0c feat: AddFormattedText函数修改 2025-08-06 09:26:54 +08:00
Coffee
aa0692f1c4 Merge pull request #1 from ZeroHawkeye/main
merge
2025-08-06 09:04:53 +08:00
徐静波
05106ab56c feat: 混合格式段落字体下划线和删除线 2025-08-05 18:09:20 +08:00
6 changed files with 95 additions and 17 deletions

View File

@@ -1,5 +1,13 @@
# WordZero 更新日志
## [Unreleased]
### 🐛 修复
- `Table.GetCellText` 现在返回单元格内所有段落与所有 Run 的完整文本,并以 `\n` 连接段落,修复此前只能获取第一段首个 Run 文本导致多行内容丢失的问题。
- 影响:如果下游代码假设无换行符,需自行 `strings.ReplaceAll(text, "\n", "")` 或按需拆分。
- 限制:同一段落内的 `<w:br/>` 软换行尚未单独解析(未来可扩展)。
## [v1.3.6] - 2025-06-04
### 🚀 新增功能

View File

@@ -15,6 +15,9 @@ func main() {
{FontFamily: "Times New Roman", FontSize: 14, Italic: true},
{FontFamily: "Courier New", FontSize: 10, FontColor: "FF0000"},
{FontFamily: "Calibri", FontSize: 16, Bold: true, Italic: true},
{FontFamily: "Calibri", FontSize: 16, Underline: true},
{FontFamily: "Calibri", FontSize: 16, Strike: true},
{FontFamily: "微软雅黑", FontSize: 18, Highlight: "yellow"},
}
texts := []string{
@@ -22,6 +25,9 @@ func main() {
"这是斜体文本",
"这是红色文本",
"这是粗体斜体文本",
"这是下划线文本",
"这是删除线文本",
"这是高亮文本",
}
for i, text := range texts {

View File

@@ -341,22 +341,22 @@ func demonstrateTemplateInheritance() {
## 段落操作方法
### 段落格式设置
- [`SetAlignment(alignment AlignmentType)`](document.go#L521) - 设置段落对齐方式
- [`SetSpacing(config *SpacingConfig)`](document.go#L558) - 设置段落间距
- [`SetStyle(styleID string)`](document.go#L773) - 设置段落样式
- [`SetAlignment(alignment AlignmentType)`](document.go) - 设置段落对齐方式
- [`SetSpacing(config *SpacingConfig)`](document.go) - 设置段落间距
- [`SetStyle(styleID string)`](document.go) - 设置段落样式
### 段落内容操作
- [`AddFormattedText(text string, format *TextFormat)`](document.go#L623) - 添加格式化文本
- [`ElementType()`](document.go#L61) - 获取段落元素类型
- [`AddFormattedText(text string, format *TextFormat)`](document.go) - 添加格式化文本
- [`ElementType()`](document.go) - 获取段落元素类型
## 文档主体操作方法
### 元素查询
- [`GetParagraphs()`](document.go#L1149) - 获取所有段落
- [`GetTables()`](document.go#L1160) - 获取所有表格
- [`GetParagraphs()`](document.go) - 获取所有段落
- [`GetTables()`](document.go) - 获取所有表格
### 元素添加
- [`AddElement(element interface{})`](document.go#L1171) - 添加元素到文档主体
- [`AddElement(element interface{})`](document.go) - 添加元素到文档主体
## 表格操作方法
@@ -381,7 +381,10 @@ func demonstrateTemplateInheritance() {
### 单元格操作
- [`GetCell(row, col int)`](table.go#L502) - 获取指定单元格
- [`SetCellText(row, col int, text string)`](table.go#L515) - 设置单元格文本
- [`GetCellText(row, col int)`](table.go#L548) - 获取单元格文本
- [`GetCellText(row, col int)`](table.go#L623) - 获取单元格文本(已升级:返回单元格内所有段落与 Run 的完整内容,段落之间使用 `\n` 分隔)
- 旧行为:仅返回第一个段落的第一个 Run 文本,导致多行/软换行内容丢失
- 新行为:遍历所有段落与其下所有 Run拼接文本空段落跳过内容但仍产生段落换行除末尾
- 注意:如果未来需要保留 Word 中 `<w:br/>`(同一段落内的手动软换行),需扩展解析逻辑;当前仅按段落分隔
- [`SetCellFormat(row, col int, format *CellFormat)`](table.go#L691) - 设置单元格格式
- [`GetCellFormat(row, col int)`](table.go#L1238) - 获取单元格格式

View File

@@ -281,7 +281,11 @@ type TextFormat struct {
Italic bool // 是否斜体
FontSize int // 字体大小(磅)
FontColor string // 字体颜色(十六进制,如 "FF0000" 表示红色)
FontFamily string // 字体名称
FontFamily string // 字体名称(首选字段)
FontName string // 字体名称别名(为兼容早期文档示例/README 中使用的 FontName
Underline bool // 是否下划线
Strike bool // 删除线
Highlight string //高亮颜色
}
// AlignmentType 对齐类型
@@ -607,8 +611,20 @@ func (d *Document) AddFormattedParagraph(text string, format *TextFormat) *Parag
runProps := &RunProperties{}
if format != nil {
// 兼容 FontFamily 与 FontName 两个字段
fontName := ""
if format.FontFamily != "" {
runProps.FontFamily = &FontFamily{ASCII: format.FontFamily}
fontName = format.FontFamily
} else if format.FontName != "" { // 向后兼容示例代码
fontName = format.FontName
}
if fontName != "" {
runProps.FontFamily = &FontFamily{ // 设置所有相关字段,保证测试与渲染一致
ASCII: fontName,
HAnsi: fontName,
EastAsia: fontName,
CS: fontName,
}
}
if format.Bold {
@@ -629,6 +645,17 @@ func (d *Document) AddFormattedParagraph(text string, format *TextFormat) *Parag
// Word中字体大小是半磅为单位所以需要乘以2
runProps.FontSize = &FontSize{Val: strconv.Itoa(format.FontSize * 2)}
}
if format.Underline {
runProps.Underline = &Underline{Val: "single"} // 默认单线下划线
}
if format.Strike {
runProps.Strike = &Strike{} // 添加删除线
}
if format.Highlight != "" {
runProps.Highlight = &Highlight{Val: format.Highlight}
}
}
p := &Paragraph{
@@ -769,8 +796,19 @@ func (p *Paragraph) AddFormattedText(text string, format *TextFormat) {
runProps := &RunProperties{}
if format != nil {
fontName := ""
if format.FontFamily != "" {
runProps.FontFamily = &FontFamily{ASCII: format.FontFamily}
fontName = format.FontFamily
} else if format.FontName != "" { // 兼容旧示例
fontName = format.FontName
}
if fontName != "" {
runProps.FontFamily = &FontFamily{
ASCII: fontName,
HAnsi: fontName,
EastAsia: fontName,
CS: fontName,
}
}
if format.Bold {
@@ -789,6 +827,17 @@ func (p *Paragraph) AddFormattedText(text string, format *TextFormat) {
if format.FontSize > 0 {
runProps.FontSize = &FontSize{Val: strconv.Itoa(format.FontSize * 2)}
}
if format.Underline {
runProps.Underline = &Underline{Val: "single"} // 默认单线下划线
}
if format.Strike {
runProps.Strike = &Strike{} // 添加删除线
}
if format.Highlight != "" {
runProps.Highlight = &Highlight{Val: format.Highlight}
}
}
run := Run{

View File

@@ -1099,5 +1099,7 @@ func (d *Document) SetImageAlignment(imageInfo *ImageInfo, alignment AlignmentTy
}
}
return fmt.Errorf("找不到包含图片ID %s的段落", imageInfo.ID)
// 未找到包含图片的段落保持配置已更新返回nil以兼容在图片尚未插入段落前先设置对齐的用例。
Debugf("未找到包含图片ID %s 的段落,已仅更新配置中的对齐方式", imageInfo.ID)
return nil
}

View File

@@ -627,11 +627,21 @@ func (t *Table) GetCellText(row, col int) (string, error) {
return "", err
}
if len(cell.Paragraphs) == 0 || len(cell.Paragraphs[0].Runs) == 0 {
return "", nil
}
if len(cell.Paragraphs) == 0 {
return "", nil
}
return cell.Paragraphs[0].Runs[0].Text.Content, nil
var result string
for idx, para := range cell.Paragraphs {
for _, run := range para.Runs {
result += run.Text.Content
}
// 段落之间添加软换行符(除最后一个段落)
if idx < len(cell.Paragraphs)-1 {
result += "\n"
}
}
return result, nil
}
// GetRowCount 获取表格行数