当前位置:网站首页>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
activeSheet
Is 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,我会不断努力,给大家带来更多类似的文章,恳请大家不吝赐教.
边栏推荐
- Microsoft Azure & NVIDIA IoT developers season I | Azure IoT & NVIDIA Jetson development foundation
- leetcode-6132: Make all elements in array equal to zero
- Prime Ring Problem(素数环问题)
- 热修复技术可谓是百花齐放
- HoloView--live data
- 安装GBase 8c数据库的时候,报错显示“Resource,如何解决?
- Static Pod, Pod Creation Process, Container Resource Limits
- Opencv creates a window - cv.namedWindow()
- Leicester Weekly 304 6135. The longest ring in the picture Inward base ring tree
- JVM内存模型之深究模型特征
猜你喜欢
Meeting OA (Upcoming Meetings & All Meetings)
leetcode-6133: maximum number of groupings
【STM32】入门(一):环境搭建、编译、下载、运行
leetcode-6133:分组的最大数量
mysql在cmd的登录及数据库与表的基本操作
Redis middleware (from building to refuse pit)
How to ensure the consistency of database and cache data?
灵魂发问:MySQL是如何解决幻读的?
Redis 3.2.3 crashed by signal: 11 服务宕机问题排查
various network protocols
随机推荐
sqlserver 对比两张表的差异
HoloView——实时数据
VS“无法查找或打开PDB文件”是怎么回事?如何解决
正则表达式符号
Pod环境变量和initContainer
Pod environment variables and initContainer
灵魂发问:MySQL是如何解决幻读的?
A problem with writing to the database after PHP gets the timestamp
HoloView--live data
对于小应用来讲,使用哪款数据库比较好?
SQL Server database schema and objects related knowledge notes
Mysql database deployment and initialization steps
Comprehensive experiment BGP
leetcode-6132:使数组中所有元素都等于零
Microsoft Azure & NVIDIA IoT developers season I | Azure IoT & NVIDIA Jetson development foundation
【应用推荐】常见资源管理器整理,含个人使用体验和产品选型推荐
CTO强烈禁止使用Calendar,那用啥?
ASP.NET Core 6框架揭秘实例演示[30]:利用路由开发REST API
Opencv creates a window - cv.namedWindow()
GBase 8c中怎么查询数据库配置参数,例如datestyle