当前位置:网站首页>Go-Excelize API源码阅读(十一)—— GetActiveSheetIndex()
Go-Excelize API源码阅读(十一)—— GetActiveSheetIndex()
2022-08-10 07:32:00 【ReganYue】
Go-Excelize API源码阅读(十一)—— GetActiveSheetIndex()
开源摘星计划(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 或更高版本。
二、GetActiveSheetIndex()
func (f *File) GetActiveSheetIndex() int
该API的作用是获取默认工作表的索引,如果没有找到默认工作表将返回 0。
废话少说,直接上源码:
// GetActiveSheetIndex provides a function to get active sheet index of the
// spreadsheet. If not found the active sheet will be return integer 0.
func (f *File) GetActiveSheetIndex() (index int) {
sheetID := f.getActiveSheetID()
wb := f.workbookReader()
if wb != nil {
for idx, sheet := range wb.Sheets.Sheet {
if sheet.SheetID == sheetID {
index = idx
return
}
}
}
return
}
看到这个sheetID := f.getActiveSheetID()
getActiveSheetID 提供了一个函数来获取电子表格的活动工作表 ID。
如果未找到活动工作表,将返回整数 0。
func (f *File) getActiveSheetID() int {
wb := f.workbookReader()
if wb != nil {
if wb.BookViews != nil && len(wb.BookViews.WorkBookView) > 0 {
activeTab := wb.BookViews.WorkBookView[0].ActiveTab
if len(wb.Sheets.Sheet) > activeTab && wb.Sheets.Sheet[activeTab].SheetID != 0 {
return wb.Sheets.Sheet[activeTab].SheetID
}
}
if len(wb.Sheets.Sheet) >= 1 {
return wb.Sheets.Sheet[0].SheetID
}
}
return 0
}
其实就是读取表格文件,然后获取工作簿视图队列第一个ActiveTab。
ActiveTab我在微软文档没有找到,我猜测是活动视图队列的工作表序号。
type xlsxWorkBookView struct {
Visibility string `xml:"visibility,attr,omitempty"`
Minimized bool `xml:"minimized,attr,omitempty"`
ShowHorizontalScroll *bool `xml:"showHorizontalScroll,attr"`
ShowVerticalScroll *bool `xml:"showVerticalScroll,attr"`
ShowSheetTabs *bool `xml:"showSheetTabs,attr"`
XWindow string `xml:"xWindow,attr,omitempty"`
YWindow string `xml:"yWindow,attr,omitempty"`
WindowWidth int `xml:"windowWidth,attr,omitempty"`
WindowHeight int `xml:"windowHeight,attr,omitempty"`
TabRatio int `xml:"tabRatio,attr,omitempty"`
FirstSheet int `xml:"firstSheet,attr,omitempty"`
ActiveTab int `xml:"activeTab,attr,omitempty"`
AutoFilterDateGrouping *bool `xml:"autoFilterDateGrouping,attr"`
}
if len(wb.Sheets.Sheet) > activeTab && wb.Sheets.Sheet[activeTab].SheetID != 0 {
return wb.Sheets.Sheet[activeTab].SheetID
}
活动视图队列第一个工作表序号不能大于工作表的数量,并且其工作表ID不等于0。
如此,便可以返回活动视图队列第一个工作表的工作表ID。
if len(wb.Sheets.Sheet) >= 1 {
return wb.Sheets.Sheet[0].SheetID
}
如果工作簿视图不存在,或者工作簿视图数量等于0,并且工作表的数量大于等于1,那么就返回索引为0的工作表的ID。
wb := f.workbookReader()
if wb != nil {
for idx, sheet := range wb.Sheets.Sheet {
if sheet.SheetID == sheetID {
index = idx
return
}
}
}
读取工作簿,然后如果工作簿为空,就会导致sheetID := f.getActiveSheetID()获取的工作表ID为0,所以会返回0。
如果不为空,就遍历工作表,当工作表ID为getActiveSheetID()获取到的活跃工作表ID时,取其工作表索引,返回。
三、结语
这里是老岳,这是Go语言相关源码的解读第十篇,我会不断努力,给大家带来更多类似的文章,恳请大家不吝赐教。
边栏推荐
- 2022 Henan Mengxin League Game (5): University of Information Engineering C - Throwing a Handkerchief
- 搭建 risc-v 编译环境
- navicat for mysql 连接时报错:1251-Client does not support authentication protocol requested by server
- 自动化测试框架搭建 ---- 标记性能较差用例
- 1413. Stepwise Summation to Get Minimum Positive Numbers
- 探索神经网络架构教程视频,设计神经网络的步骤
- Data types for database learning
- .NET-8. My Thought Notes
- Sort binary tree code
- 3-6月面经总结,200多页真题笔记和详解(含核心考点及6家大厂)
猜你喜欢
随机推荐
2022 Henan Mengxin League No. 5: University of Information Engineering B - Transportation Renovation
90.(cesium之家)cesium高度监听事件
Based on STC8G2K64S4 single-chip microcomputer to display analog photosensitive analog value through OLED screen
IDLE development wordCount program (5)
Confluence可以连接数据库但是在下一步就报错了
3. Transactions [mysql advanced]
关于数据库中的中文模糊检索探讨
mysql数据库定时备份(保留近7天的备份)
高级测试:如何使用Flink对Strom任务的逻辑功能进行复现测试?
The probability distribution and its application
初使jest 单元测试
Regular backup of mysql database (retain backups for nearly 7 days)
Relaxation class: the boss will martial arts, who also can not hold up against!The charm of six sigma training
进程管理(动态的)
Data types for database learning
卷积神经网络卷积层公式,卷积神经网络运算公式
winget package manager
Quickly enter the current date and time
Discussion on Chinese Fuzzy Retrieval in Databases
941 · 滑动拼图









