当前位置:网站首页>Go - reading (7), CopySheet Excelize API source code (the from and to the int)
Go - reading (7), CopySheet Excelize API source code (the from and to the int)
2022-07-29 13:02:00 【ReganYue】
Go-Excelize API源码阅读(七)—— CopySheet(from, to int)
开源摘星计划(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 或更高版本.
二、CopySheet(from, to int)、
func (f *File) CopySheet(from, to int) error
该APIThe role of the function is to provide a function to copy the worksheet by giving the source worksheet and target worksheet index,This index needs to be confirmed by developers themselves.注意,Copying include tables is currently not supported、A workbook of diagrams or pictures,Only worksheet copying with cell values and formulas is supported.
使用案例:
sheet_index := f.NewSheet("Sheet2")
err := f.CopySheet(1, sheet_index)
Start reading the source code directly:
func (f *File) CopySheet(from, to int) error {
if from < 0 || to < 0 || from == to || f.GetSheetName(from) == "" || f.GetSheetName(to) == "" {
return ErrSheetIdx
}
return f.copySheet(from, to)
}
This function should be givencopySheetFilter out some index errors.比如:
- The source worksheet index is less than0Or the target worksheet index is less than0.
- The source worksheet index is equal to the target worksheet index.
- The source sheet does not exist or the target sheet does not exist
然后调用copySheet.
Read directly nextcopySheet的源码:
unc (f *File) copySheet(from, to int) error {
fromSheet := f.GetSheetName(from)
sheet, err := f.workSheetReader(fromSheet)
if err != nil {
return err
}
worksheet := deepcopy.Copy(sheet).(*xlsxWorksheet)
toSheetID := strconv.Itoa(f.getSheetID(f.GetSheetName(to)))
sheetXMLPath := "xl/worksheets/sheet" + toSheetID + ".xml"
if len(worksheet.SheetViews.SheetView) > 0 {
worksheet.SheetViews.SheetView[0].TabSelected = false
}
worksheet.Drawing = nil
worksheet.TableParts = nil
worksheet.PageSetUp = nil
f.Sheet.Store(sheetXMLPath, worksheet)
toRels := "xl/worksheets/_rels/sheet" + toSheetID + ".xml.rels"
fromRels := "xl/worksheets/_rels/sheet" + strconv.Itoa(f.getSheetID(fromSheet)) + ".xml.rels"
if rels, ok := f.Pkg.Load(fromRels); ok && rels != nil {
f.Pkg.Store(toRels, rels.([]byte))
}
fromSheetXMLPath := f.sheetMap[trimSheetName(fromSheet)]
fromSheetAttr := f.xmlAttr[fromSheetXMLPath]
f.xmlAttr[sheetXMLPath] = fromSheetAttr
return err
}
看第一部分:
This part is to get the source worksheet name,Then read the source worksheet.
嗯,我们继续.
worksheet := deepcopy.Copy(sheet).(*xlsxWorksheet)
这里调用了github.com/mohae/deepcopy包.
func Copy(src interface{
}) interface{
} {
if src == nil {
return nil
}
// Make the interface a reflect.Value
original := reflect.ValueOf(src)
// Make a copy of the same type as the original.
cpy := reflect.New(original.Type()).Elem()
// Recursively copy the original.
copyRecursive(original, cpy)
// Return the copy as an interface.
return cpy.Interface()
}
CopyCreates a deep copy of the object passed to it,并在一个interface {}returns the copy in . The returned value will need to be asserted to the correct type.

Next is to get the target sheetID,And piece together the target worksheetXML路径.
如果xml文件中SheetViewcorresponding to the parameterssheetView长度大于0,这个参数是[]xlsxSheetView类型,Should be a sheet view collection.Here, when the number of views is greater than 0,就将第1view'sTabSelected 参数置为false.TabSelectedLooking for Microsoft's documentation,There is no indication of what it is used for.Here the author guesses that it is the parameter corresponding to the box highlighted when the cell is clicked.
The next step is to initialize the three parameters of the deep copied worksheet.
然后以sheetXMLPath为键,worksheet为值,into the target worksheetMap:Sheet.

This part is processingrels文件的拷贝.

This should be the properties of the copied worksheet.
三、结语
这里是老岳,这是GoThe seventh chapter of the interpretation of language-related source code,我会不断努力,给大家带来更多类似的文章,恳请大家不吝赐教.
边栏推荐
- SIP系统组成格式
- 【c ++ primer 笔记】第6章 函数
- npm出现报错 npm WARN config global `--global`, `--local` are deprecated. Use `--location=global
- MLX90640 infrared thermal imaging temperature measuring sensor module development notes (9)
- Go-Excelize API源码阅读(七)—— CopySheet(from, to int)
- MySQL如何对SQL做prepare预处理(解决IN查询SQL预处理仅能查询出一条记录的问题)
- 3D激光SLAM:LeGO-LOAM论文解读---硬件系统部分
- 金仓数据库KingbaseES客户端编程接口指南-JDBC(3. JDBC 建立/关闭连接)
- WPF 截图控件之绘制方框与椭圆(四) 「仿微信」
- 投资127亿!深圳,再添一所985
猜你喜欢

记账APP:小哈记账3——登录页面的制作

来自 Qt 官网的呐喊

IDEA 数据库插件Database Navigator 插件

3D Laser SLAM: Interpretation of LeGO-LOAM Papers---Hardware System Part

TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2

2022年编程语言排名,官方数据来了,让人大开眼界

CentOS7安装Oracle数据库的全流程

Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法

MySql 5.7.38下载安装教程 ,并实现在Navicat操作MySql

SIP system composition format
随机推荐
mysql数据库安装(详细)
什么是BOM
Container is changed | deploy MySQL cluster in the Rancher
JS_删除数组里的无效数据 0 undefined ‘‘ null false NaN
bean的生命周期
传奇服务端GOM引擎和GEE引擎区别在哪里?
容器化 | 在 Rancher 中部署 MySQL 集群
WordPress 主题和插件
【c ++ primer 笔记】第6章 函数
Bookkeeping APP: Xiaoha Bookkeeping 3 - Production of Login Page
es6 arrow function explanation
峰米V10、极米Rs Pro2及当贝X3 Pro究竟孰优孰劣?
IDEA2021.2安装与配置(持续更新)
TiCDC同步延迟问题处理
为什么用了大牌工具后报表开发依然头痛
MySQL基础(DDL、DML、DQL)
[Mysql] LENGTH函数
【多线程】——synchronized关键字
金仓数据库KingbaseES安全指南--6.8. SSPI身份验证
[Cloud native] Introduction and use of Feign of microservices