Go?Excelize?API源碼解析GetSheetFormatPr使用示例
一、Go-Excelize簡介
Excelize 是 Go 語言編寫的用于操作 Office Excel 文檔基礎(chǔ)庫,基于 ECMA-376,ISO/IEC 29500 國際標(biāo)準(zhǔn)。
可以使用它來讀取、寫入由 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)。
使用本類庫要求使用的 Go 語言為 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
廢話少說,上代碼:
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
}
代碼很簡單,先讀取工作表,然后獲取工作表的格式屬性,然后遍歷不定長參數(shù)opts,對fp的每個(gè)opt進(jìn)行讀取。
SheetFormatPrOptionsPtr是一個(gè)interface。
type SheetFormatPrOptionsPtr interface {
SheetFormatPrOptions
getSheetFormatPr(formatPr *xlsxSheetFormatPr)
}
該interface 內(nèi)有兩個(gè)函數(shù)。





我們可以發(fā)現(xiàn),他們都大同小異,第一步的if語句是判斷格式屬性是否存在,如果不存在就賦一個(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)情況下行是否具有粗下邊框。
以上就是Go Excelize API源碼解析GetSheetFormatPr使用示例的詳細(xì)內(nèi)容,更多關(guān)于Go源碼解析GetSheetFormatPr的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
GoFrame框架garray對比PHP的array優(yōu)勢
這篇文章主要為大家介紹了GoFrame框架garray對比PHP的array優(yōu)勢詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Go語言循環(huán)遍歷含有中文的字符串的方法小結(jié)
這篇文章主要介紹了Go語言循環(huán)遍歷含有中文的字符串的幾種方法,文章通過代碼示例講解的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴跟著小編一起來看看吧2023-07-07
淺談Golang中創(chuàng)建一個(gè)簡單的服務(wù)器的方法
這篇文章主要介紹了淺談Golang中創(chuàng)建一個(gè)簡單的服務(wù)器的方法,golang中的net/http包對網(wǎng)絡(luò)的支持非常好,這樣會(huì)讓我們比較容易的建立起一個(gè)相對簡單的服務(wù)器,有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
golang時(shí)間及時(shí)間戳的獲取轉(zhuǎn)換
本文主要介紹了golang時(shí)間及時(shí)間戳的獲取轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Golang設(shè)計(jì)模式之單例模式詳細(xì)講解
單例模式很容易記住。就像名稱一樣,它只能提供對象的單一實(shí)例,保證一個(gè)類只有一個(gè)實(shí)例,并提供一個(gè)全局訪問該實(shí)例的方法。本文就來聊聊Go語言中的單例模式,感興趣的小伙伴可以了解一下2023-01-01

