Go?Excelize?API源碼解析GetSheetFormatPr使用示例
一、Go-Excelize簡介
Excelize 是 Go 語言編寫的用于操作 Office Excel 文檔基礎(chǔ)庫,基于 ECMA-376,ISO/IEC 29500 國際標準。
可以使用它來讀取、寫入由 Microsoft Excel™ 2007 及以上版本創(chuàng)建的電子表格文檔。
支持 XLAM / XLSM / XLSX / XLTM / XLTX 等多種文檔格式,高度兼容帶有樣式、圖片(表)、透視表、切片器等復雜組件的文檔,并提供流式讀寫 API,用于處理包含大規(guī)模數(shù)據(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的每個opt進行讀取。
SheetFormatPrOptionsPtr是一個interface。
type SheetFormatPrOptionsPtr interface { SheetFormatPrOptions getSheetFormatPr(formatPr *xlsxSheetFormatPr) }
該interface 內(nèi)有兩個函數(shù)。
我們可以發(fā)現(xiàn),他們都大同小異,第一步的if語句是判斷格式屬性是否存在,如果不存在就賦一個默認值。 然后取格式屬性指針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"` }
下面介紹一下各個參數(shù)的作用:
- BaseColWidth:指定普通樣式字體的最大數(shù)字寬度的字符數(shù)。此值不包括邊距填充或網(wǎng)格線的額外填充。它只是字符數(shù)。
- DefaultColWidth 指定默認列寬,其度量值為普通樣式字體的最大數(shù)字寬度的字符數(shù)。
- DefaultRowHeight 指定以磅值度量的默認行高,我們不必在所有行上寫入高度。如果大多數(shù)行具有自定義高度,則可以將其寫出,以實現(xiàn)優(yōu)化。
- CustomHeight 指定自定義高度。
- ZeroHeight 指定是否隱藏行。
- ThickTop 指定默認情況下行是否具有粗上邊框。
- ThickBottom 指定默認情況下行是否具有粗下邊框。
以上就是Go Excelize API源碼解析GetSheetFormatPr使用示例的詳細內(nèi)容,更多關(guān)于Go源碼解析GetSheetFormatPr的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Golang使用Gin框架實現(xiàn)HTTP上傳文件過程介紹
由于需求中有文件上傳這一個需求,在這里我們就學習一下go語言如何上傳文件。本文主要通過表單的方式進行文件上傳操作,本文實例為大家分享了Go實現(xiàn)文件上傳操作的具體代碼,供大家參考,具體內(nèi)容如下2023-04-04golang實現(xiàn)簡單的udp協(xié)議服務端與客戶端示例
這篇文章主要介紹了golang實現(xiàn)簡單的udp協(xié)議服務端與客戶端,結(jié)合實例形式分析了基于UDP協(xié)議的數(shù)據(jù)傳輸相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2016-07-07Golang程序中使用Prometheus的client_golang庫
這篇文章主要介紹了Golang程序中使用Prometheus的client_golang庫,Prometheus 是一個開源的監(jiān)控和警報工具包,用于收集和處理應用程序和系統(tǒng)的指標數(shù)據(jù)。Prometheus 提供了多種客戶端庫,可以輕松地集成到各種編程語言中2023-04-04