日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Go?Excelize?API源碼解析GetSheetFormatPr使用示例_Golang

作者:丘山子 ? 更新時(shí)間: 2022-10-12 編程語(yǔ)言

一、Go-Excelize簡(jiǎn)介

Excelize 是 Go 語(yǔ)言編寫的用于操作 Office Excel 文檔基礎(chǔ)庫(kù),基于 ECMA-376,ISO/IEC 29500 國(guó)際標(biāo)準(zhǔn)。

可以使用它來(lái)讀取、寫入由 Microsoft Excel? 2007 及以上版本創(chuàng)建的電子表格文檔。

支持 XLAM / XLSM / XLSX / XLTM / XLTX 等多種文檔格式,高度兼容帶有樣式、圖片(表)、透視表、切片器等復(fù)雜組件的文檔,并提供流式讀寫 API,用于處理包含大規(guī)模數(shù)據(jù)的工作簿。

可應(yīng)用于各類報(bào)表平臺(tái)、云計(jì)算、邊緣計(jì)算等系統(tǒng)。

使用本類庫(kù)要求使用的 Go 語(yǔ)言為 1.15 或更高版本。

二、GetSheetFormatPr

func (f *File) GetSheetFormatPr(sheet string, opts ...SheetFormatPrOptionsPtr) error

根據(jù)給定的工作表名稱獲取格式屬性。

可選格式參數(shù) 數(shù)據(jù)類型
BaseColWidth uint8
DefaultColWidth float64
DefaultRowHeight float64
CustomHeight bool
ZeroHeight bool
ThickTop bool
ThickBottom bool

使用示例:

f := excelize.NewFile()
const sheet = "Sheet1"
var (
    baseColWidth     excelize.BaseColWidth
    defaultColWidth  excelize.DefaultColWidth
    defaultRowHeight excelize.DefaultRowHeight
    customHeight     excelize.CustomHeight
    zeroHeight       excelize.ZeroHeight
    thickTop         excelize.ThickTop
    thickBottom      excelize.ThickBottom
)
if err := f.GetSheetFormatPr(sheet,
    &baseColWidth,
    &defaultColWidth,
    &defaultRowHeight,
    &customHeight,
    &zeroHeight,
    &thickTop,
    &thickBottom,
); err != nil {
    fmt.Println(err)
}
fmt.Println("Defaults:")
fmt.Println("- baseColWidth:", baseColWidth)
fmt.Println("- defaultColWidth:", defaultColWidth)
fmt.Println("- defaultRowHeight:", defaultRowHeight)
fmt.Println("- customHeight:", customHeight)
fmt.Println("- zeroHeight:", zeroHeight)
fmt.Println("- thickTop:", thickTop)
fmt.Println("- thickBottom:", thickBottom)

輸出結(jié)果:

Defaults:
- baseColWidth: 0
- defaultColWidth: 0
- defaultRowHeight: 15
- customHeight: false
- zeroHeight: false
- thickTop: false
- thickBottom: false

廢話少說(shuō),上代碼:

func (f *File) GetSheetFormatPr(sheet string, opts ...SheetFormatPrOptionsPtr) error {
	s, err := f.workSheetReader(sheet)
	if err != nil {
		return err
	}
	fp := s.SheetFormatPr
	for _, opt := range opts {
		opt.getSheetFormatPr(fp)
	}
	return err
}

代碼很簡(jiǎn)單,先讀取工作表,然后獲取工作表的格式屬性,然后遍歷不定長(zhǎng)參數(shù)opts,對(duì)fp的每個(gè)opt進(jìn)行讀取。

SheetFormatPrOptionsPtr是一個(gè)interface。

type SheetFormatPrOptionsPtr interface {
	SheetFormatPrOptions
	getSheetFormatPr(formatPr *xlsxSheetFormatPr)
}

該interface 內(nèi)有兩個(gè)函數(shù)。

我們可以發(fā)現(xiàn),他們都大同小異,第一步的if語(yǔ)句是判斷格式屬性是否存在,如果不存在就賦一個(gè)默認(rèn)值。 然后取格式屬性指針fp的格式屬性,前面是類型轉(zhuǎn)換:

type xlsxSheetFormatPr struct {
	XMLName          xml.Name `xml:"sheetFormatPr"`
	BaseColWidth     uint8    `xml:"baseColWidth,attr,omitempty"`
	DefaultColWidth  float64  `xml:"defaultColWidth,attr,omitempty"`
	DefaultRowHeight float64  `xml:"defaultRowHeight,attr"`
	CustomHeight     bool     `xml:"customHeight,attr,omitempty"`
	ZeroHeight       bool     `xml:"zeroHeight,attr,omitempty"`
	ThickTop         bool     `xml:"thickTop,attr,omitempty"`
	ThickBottom      bool     `xml:"thickBottom,attr,omitempty"`
	OutlineLevelRow  uint8    `xml:"outlineLevelRow,attr,omitempty"`
	OutlineLevelCol  uint8    `xml:"outlineLevelCol,attr,omitempty"`
}

下面介紹一下各個(gè)參數(shù)的作用:

  • BaseColWidth:指定普通樣式字體的最大數(shù)字寬度的字符數(shù)。此值不包括邊距填充或網(wǎng)格線的額外填充。它只是字符數(shù)。
  • DefaultColWidth 指定默認(rèn)列寬,其度量值為普通樣式字體的最大數(shù)字寬度的字符數(shù)。
  • DefaultRowHeight 指定以磅值度量的默認(rèn)行高,我們不必在所有行上寫入高度。如果大多數(shù)行具有自定義高度,則可以將其寫出,以實(shí)現(xiàn)優(yōu)化。
  • CustomHeight 指定自定義高度。
  • ZeroHeight 指定是否隱藏行。
  • ThickTop 指定默認(rèn)情況下行是否具有粗上邊框。
  • ThickBottom 指定默認(rèn)情況下行是否具有粗下邊框。

原文鏈接:https://juejin.cn/post/7131983142763626503

欄目分類
最近更新