当前位置:网站首页>Go-Excelize API source code reading (8) - GroupSheets(sheets []string), UngroupSheets()
Go-Excelize API source code reading (8) - GroupSheets(sheets []string), UngroupSheets()
2022-08-01 09:32:00 【ReganYue】
Go-Excelize API源码阅读(八)——UngroupSheets()
开源摘星计划(WeOpen Star) 是由腾源会 2022 年推出的全新项目,旨在为开源人提供成长激励,为开源项目提供成长支持,助力开发者更好地了解开源,更快地跨越鸿沟,参与到开源的具体贡献与实践中.
不管你是开源萌新,还是希望更深度参与开源贡献的老兵,跟随“开源摘星计划”开启你的开源之旅,从一篇学习笔记、到一段代码的提交,不断挖掘自己的潜能,最终成长为开源社区的“闪亮之星”.
我们将同你一起,探索更多的可能性!
项目地址: WeOpen-Star:https://github.com/weopenprojects/WeOpen-Star
一、Go-Excelize简介
Excelize 是 Go 语言编写的用于操作 Office Excel 文档基础库,基于 ECMA-376,ISO/IEC 29500 国际标准.可以使用它来读取、写入由 Microsoft Excel 2007 及以上版本创建的电子表格文档.支持 XLAM / XLSM / XLSX / XLTM / XLTX 等多种文档格式,高度兼容带有样式、图片(表)、透视表、切片器等复杂组件的文档,并提供流式读写 API,用于处理包含大规模数据的工作簿.可应用于各类报表平台、云计算、边缘计算等系统.使用本类库要求使用的 Go 语言为 1.15 或更高版本.
二、GroupSheets(sheets []string)
func (f *File) GroupSheets(sheets []string) error
该APIIs the function of work according to the given table name for grouping work table,A given job will consist of a default in the table table.
func (f *File) GroupSheets(sheets []string) error {
// check an active worksheet in group worksheets
var inActiveSheet bool
activeSheet := f.GetActiveSheetIndex()
sheetMap := f.GetSheetList()
for idx, sheetName := range sheetMap {
for _, s := range sheets {
if s == sheetName && idx == activeSheet {
inActiveSheet = true
}
}
}
if !inActiveSheet {
return ErrGroupSheets
}
// check worksheet exists
var wss []*xlsxWorksheet
for _, sheet := range sheets {
worksheet, err := f.workSheetReader(sheet)
if err != nil {
return err
}
wss = append(wss, worksheet)
}
for _, ws := range wss {
sheetViews := ws.SheetViews.SheetView
if len(sheetViews) > 0 {
for idx := range sheetViews {
ws.SheetViews.SheetView[idx].TabSelected = true
}
continue
}
}
return nil
}
整个APIImplementation is divided into two steps:
1.Find an active in group work table worksheet
activeSheetIs the job of the active table index.sheetMap := f.GetSheetList()
GetSheetList 提供了一个函数,Used to get workbook worksheet、Chart worksheet and dialog work table name list.
func (f *File) GetSheetList() (list []string) {
wb := f.workbookReader()
if wb != nil {
for _, sheet := range wb.Sheets.Sheet {
list = append(list, sheet.Name)
}
}
return
}
for idx, sheetName := range sheetMap {
for _, s := range sheets {
if s == sheetName && idx == activeSheet {
inActiveSheet = true
}
}
}
This code is to look forsheets里面是不是有s与idx等于activeSheet的sheetName相等.
2.Check the schedule whether there is a
var wss []*xlsxWorksheet
for _, sheet := range sheets {
worksheet, err := f.workSheetReader(sheet)
if err != nil {
return err
}
wss = append(wss, worksheet)
}
for _, ws := range wss {
sheetViews := ws.SheetViews.SheetView
if len(sheetViews) > 0 {
for idx := range sheetViews {
ws.SheetViews.SheetView[idx].TabSelected = true
}
continue
}
}
xlsxWorksheet Direct mapping work table elements in the namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main.
A new structure,然后遍历sheets,读取每一个sheet,加入wss.
然后遍历这些wss,获取wss中每一个sheet的视图.
遍历视图,Each view ofTabSelected置为true.
三、UngroupSheets()
该APIFunction is to cancel the worksheet packet.
func (f *File) UngroupSheets() error
func (f *File) UngroupSheets() error {
activeSheet := f.GetActiveSheetIndex()
for index, sheet := range f.GetSheetList() {
if activeSheet == index {
continue
}
ws, _ := f.workSheetReader(sheet)
sheetViews := ws.SheetViews.SheetView
if len(sheetViews) > 0 {
for idx := range sheetViews {
ws.SheetViews.SheetView[idx].TabSelected = false
}
}
}
return nil
}
Get active work table indexactiveSheet.
Then traverse the file work table,Deal only with the active worksheet.
Traverse to the active worksheet indexes,Read the worksheet,The each viewTabSelected置为false.
三、结语
这里是老岳,这是GoThe interpretation of language related to source the eighth article,我会不断努力,给大家带来更多类似的文章,恳请大家不吝赐教.
边栏推荐
猜你喜欢
随机推荐
The use of scrapy crawler framework
STM32个人笔记-看门狗
Shell:条件测试操作
改版去不图床 Token 的获取
静态Pod、Pod创建流程、容器资源限制
力扣周赛304 6135. 图中的最长环 内向基环树
leetcode-6135:图中的最长环
笔记。。。。
net stop/start mysql80 拒绝访问
Introduction to ADAS
Redis学习
SkiaSharp 之 WPF 自绘 五环弹动球(案例版)
Explain / Desc 执行计划分析
How programmers learn open source projects, this article tells you
最新的Cesium和Three的整合方法(附完整代码)
Mysql database deployment and initialization steps
走进音视频的世界——mp3封装格式
2022.7.31-----leetcode.1161
程序员如何学习开源项目,这篇文章告诉你
The soul asks: How does MySQL solve phantom reads?









