当前位置:网站首页>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 givencopySheet
Filter 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
.TabSelected
Looking 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,我会不断努力,给大家带来更多类似的文章,恳请大家不吝赐教.
边栏推荐
猜你喜欢
拦截器与过滤器(三)@interface自定义注解拦截
[纯理论] FPN (Feature Pyramid Network)
mysql根据多字段分组——group by带两个或多个参数
Windows系统Mysql8版本的安装教程
【微信小程序】一文解决button、input、image组件
MySQL八股文背诵版
mysql数据库安装(详细)
[based] GO language. Why do I have to learn Golang and introduction to the language universal
MySql 5.7.38下载安装教程 ,并实现在Navicat操作MySql
nacos集群搭建
随机推荐
关于栈迁移的那些事儿
Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2
String.split()最详细源码解读及注意事项
gee引擎修改UI界面图文教程
IDEA 数据库插件Database Navigator 插件
MySql字符串拆分实现split功能(字段分割转列、转行)
PHP uedtior报错 errorHandler is not defined
TiCDC同步延迟问题处理
MySQL 安装报错的解决方法
What should I do if the webpage is hijacked and redirected?Release net repair method
WordPress 版本更新
RedisTemplate使用详解
金仓数据库KingbaseES客户端编程接口指南-ODBC(6. KingbaseES ODBC 的扩展属性)
金仓数据库KingbaseES安全指南--6.6. SSL客户端证书认证
详述 TCP 的 TIME_WAIT 状态要维持 2MSL 的原因
MySQL常用的日期时间函数
DVWA全级别通关教程
金仓数据库 KingbaseES 客户端编程接口指南 - ODBC 驱动使用
传奇服务端GOM引擎和GEE引擎区别在哪里?