\
您当前的位置 : 首页 >> 帮助中心

[Go][Excelize] 确定单元格值是否有删除线

来源:恩氏滤油机
时间:2024-09-23
浏览量:1001

简介

我想确定单元格的值是否有删除线。

确定单元格的值是否有删除线

要确定单元格的值是否有删除线,我必须通过两种方式获取单元格样式。

如果只有单元格的某些值被删除,如“a1”,我应该从“excelize.richtextrun”获取单元格样式。

如果单元格中的所有值都像“a2”一样被删除,我应该从“excelize.xlsxxf”获得单元格样式。

xlsxreader.go

package main

import (
    "fmt"
    "log"

    "github.com/xuri/excelize/v2"
)

func getcellstyles(filepath string) {
    xlfile, err := excelize.openfile(filepath)
    if err != nil {
        fmt.println(err)
    }
    defer func() {
        // close the spreadsheet.
        if err := xlfile.close(); err != nil {
            fmt.println(err)
        }
    }()
    sheetname := xlfile.getsheetname(0)
    log.println(sheetname)
    printcellstyles(xlfile, sheetname, "a1")
    printcellstyles(xlfile, sheetname, "a2")
    printcellstyles(xlfile, sheetname, "a3")
}
func printcellstyles(xlfile *excelize.file, sheetname string, celladdress string) {
    log.printf("--------%s--------", celladdress)
    value, _ := xlfile.getcellvalue(sheetname, celladdress)
    log.println(value)
    // if the cell value has multiple formats, "getcellrichtext" will return multiple values.
    runs, _ := xlfile.getcellrichtext(sheetname, celladdress)
    if len(runs) > 0 {
        for _, r := range runs {
            if r.font == nil {
                log.printf("value: %s no fonts", r.text)
            } else {
                log.printf("value: %s strike through?: %t", r.text, r.font.strike)
            }
        }
    }
    styleid, _ := xlfile.getcellstyle(sheetname, celladdress)
    // get cell style info by styleid
    style := xlfile.styles.cellxfs.xf[styleid]
    // get font info by style.fontid
    font := xlfile.styles.fonts.font[*style.fontid]
    // check
    if font.strike != nil && *font.strike.val {
        log.println("the cell value has a strike through")
    } else {
        log.println("the cell value doesn't have a strike through")
    }
}

结果

2024/08/27 03:02:32 Sheet1
2024/08/27 03:02:32 --------A1--------
2024/08/27 03:02:32 123abc
2024/08/27 03:02:32 Value: 12 No fonts
2024/08/27 03:02:32 Value: 3ab Strike through?: true
2024/08/27 03:02:32 Value: c Strike through?: false
2024/08/27 03:02:32 The cell value doesn't have a strike through
2024/08/27 03:02:32 --------A2--------
2024/08/27 03:02:32 aiu
2024/08/27 03:02:32 The cell value has a strike through
2024/08/27 03:02:32 --------A3--------
2024/08/27 03:02:32 abc
def
ghi
2024/08/27 03:02:32 Value: abc
 No fonts
2024/08/27 03:02:32 Value: def Strike through?: false
2024/08/27 03:02:32 Value:
 Strike through?: false
2024/08/27 03:02:32 Value: ghi Strike through?: false
2024/08/27 03:02:32 The cell value doesn't have a strike through

资源

单元格 - excelize 文档

xlsx/tutorial/tutorial.adoc - tealeg/xlsx - github

以上就是[Go][Excelize] 确定单元格值是否有删除线的详细内容,更多请关注本网内其它相关文章!

免责申明

以上展示内容来源于合作媒体、企业机构、网友提供或网络收集整理,版权争议与本站无关,文章涉及见解与观点不代表恩氏滤油机网官方立场,请读者仅做参考。本文欢迎转载,转载请说明出处。若您认为本文侵犯了您的版权信息,或您发现该内容有任何涉及有违公德、触犯法律等违法信息,请您立即联系我们及时修正或删除。
Copyright © 2004-2025 BaiJiaMai.Com 2026年世界杯48队比赛规则 版权所有  网站备案号:渝ICP备2024041059号
Baidu
map