"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > [Go][Excelize] Determining if a cell&#s value has a strike through

[Go][Excelize] Determining if a cell&#s value has a strike through

Published on 2024-11-07
Browse:915

Intro

I want to determine if a cell's value has a strike through.

[Go][Excelize] Determining if a cell

Determining if a cell's value has a strike through

To determine if a cell's value has a strike through, I have to get cell styles in two ways.

If only some values of a cell ​​are struck through like "A1", I should get cell styles from "excelize.RichTextRun".
If all values in a cell are struck through like "A2", I should get a cell style from "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")
    }
}

Results

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

Resources

  • Cell - Excelize Document
  • xlsx/tutorial/tutorial.adoc - tealeg/xlsx - GitHub
Release Statement This article is reproduced at: https://dev.to/masanori_msl/goexcelize-determining-if-a-cells-value-has-a-strike-through-2ai0?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3