当前位置:网站首页>Go-Excelize API源码阅读(八)——GroupSheets(sheets []string)、UngroupSheets()
Go-Excelize API源码阅读(八)——GroupSheets(sheets []string)、UngroupSheets()
2022-08-01 09:11: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
该API的功能是根据给定的工作表名称对工作表进行分组,给定的工作表中需包含默认工作表。
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
}
整个API实现分为两大步骤:
1.在组工作表中找到一个活跃的工作表
activeSheet是活跃的工作表索引。sheetMap := f.GetSheetList()
GetSheetList 提供了一个函数,用于获取工作簿的工作表、图表工作表和对话框工作表名称列表。
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
}
}
}
这段代码是寻找sheets里面是不是有s与idx等于activeSheet的sheetName相等。
2.检查工作表是否存在
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 直接映射命名空间中的工作表元素 http://schemas.openxmlformats.org/spreadsheetml/2006/main。
新建这样一个结构体,然后遍历sheets,读取每一个sheet,加入wss。
然后遍历这些wss,获取wss中每一个sheet的视图。
遍历视图,将每一个视图的TabSelected置为true。
三、UngroupSheets()
该API的功能是取消工作表分组。
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
}
获取活跃工作表的索引activeSheet。
然后遍历文件的工作表,只处理活跃工作表。
遍历到活跃工作表的索引时,读取该工作表,将其的每一个视图的TabSelected置为false.
三、结语
这里是老岳,这是Go语言相关源码的解读第八篇,我会不断努力,给大家带来更多类似的文章,恳请大家不吝赐教。
边栏推荐
- In the background of the GBase 8c database, what command is used to perform the master-slave switchover operation for the gtm and dn nodes
- 毕业论文写作技巧
- GBase 8s 锁分类
- 在GBase 8c数据库后台,使用什么样的命令来对gtm、dn节点进行主备切换的操作
- SaaS安全认证综合指南
- Pod environment variables and initContainer
- 静态Pod、Pod创建流程、容器资源限制
- Redis middleware (from building to refuse pit)
- Chapters 6 and 7 of Huawei Deep Learning Course
- [Interview: Concurrency 39: Multithreading: Thread Pool] ThreadPoolExecutor Class - Submit, Stop
猜你喜欢

Prime Ring Problem(素数环问题)

The soul asks: How does MySQL solve phantom reads?

数据分析6

What do the values 1, 2, and 3 in nodetype mean?

VoLTE Basic Learning Series | Enterprise Voice Network Brief

ACmix 论文精读,并解析其模型结构

【数据集】各类绝缘子、鸟巢及防震锤数据集汇总

基于MySql,Redis,Mq,ES的高可用方案解析

HoloView 在 jyputer lab/notebook 不显示总结

22牛客多校1 C.Grab the Seat (几何 + 暴力)
随机推荐
GBase 8s 锁分类
pytest接口自动化测试框架 | 使用函数返回值的形式传入参数值
pytest接口自动化测试框架 | parametrize源码解析
[Beyond programming] When the fig leaf is lifted, when people begin to accept everything
静态Pod、Pod创建流程、容器资源限制
Pytest | skip module interface test automation framework
How to query database configuration parameters in GBase 8c, such as datestyle
Idea 常用插件
Flink SQL - client, how to deal with the source side and to increase the target, the SQL - client including mapping table and the JOB such as
HoloView--live data
三子棋(C语言实现)
ASP.NET Core 6框架揭秘实例演示[30]:利用路由开发REST API
优炫数据库支持Oracle哪几种时间及日期类型
【编程之外】当遮羞布被掀开,当人们开始接受一切
Redis 3.2.3 crashed by signal: 11 服务宕机问题排查
解析MySQL数据库:“SQL优化”与“索引优化”
leetcode 42. 接雨水
22 Niu Ke Duo School 1 I. Chiitoitsu (Probability dp)
HoloView -- Tabular Datasets
net stop/start mysql80 access denied