当前位置:网站首页>Go exceed API source code reading (V) -- close (), newsheet ()
Go exceed API source code reading (V) -- close (), newsheet ()
2022-07-23 09:17:00 【ReganYue】
Go-Excelize API Source code reading ( 5、 ... and )—— Close()
Open source star picking program (WeOpen Star) By Tengyuan society 2022 A new project launched in , Designed to provide growth incentives for open source people , Provide growth support for open source projects , Help developers better understand open source , Cross the gap faster , Participate in the specific contribution and practice of open source .
Whether you are open source Mengxin , Or veterans who want to participate more deeply in open source contributions , Follow “ Open source star picking program ” Start your open source journey , From a learning note 、 To the submission of a piece of code , Keep tapping your potential , Eventually grow into an open source community “ Blazing Star ”.
We will be with you , Explore more possibilities !
Project address : WeOpen-Star:https://github.com/weopenprojects/WeOpen-Star
One 、Go-Excelize brief introduction
Excelize yes Go Language written for operation Office Excel Document base library , be based on ECMA-376,ISO/IEC 29500 international standard . You can use it to read 、 Written by Microsoft Excel 2007 Spreadsheet documents created by and above . Support XLAM / XLSM / XLSX / XLTM / XLTX And so on , Highly compatible with styles 、 picture ( surface )、 PivotTable 、 Slicer and other complex components of the document , And provide streaming reading and writing API, For workbooks that contain large amounts of data . It can be applied to various report platforms 、 Cloud computing 、 Edge computing and other systems . Required to use this class library Go The language is 1.15 Or later .
Two 、Close()
func (f *File) Close() error
this API The function of is to close the workbook and clean up the system disk cache that may be generated when opening the document .
Let's take a look at the specific code :
func (f *File) Close() error {
var err error
if f.sharedStringTemp != nil {
if err := f.sharedStringTemp.Close(); err != nil {
return err
}
}
f.tempFiles.Range(func(k, v interface{
}) bool {
if err = os.Remove(v.(string)); err != nil {
return false
}
return true
})
return err
}
First judge sharedStringTemp Is it empty , This is a *os.File Variable of type . It should be to close an important cache file .
The next step is to traverse the temporary file ,os.Remove(v.(string)) These temporary files and folders will be deleted .
The above steps have cleared the system disk cache that may be generated when opening documents .
3、 ... and 、NewSheet()
func (f *File) NewSheet(sheet string) int
The API The function of is to create a new worksheet according to the given worksheet name , And return the index of the worksheet in the workbook . Please note that , When creating a new workbook , Will contain a file named Sheet1 Default worksheet for .
func (f *File) NewSheet(name string) int {
// Check if the worksheet already exists
index := f.GetSheetIndex(name)
if index != -1 {
return index
}
f.DeleteSheet(name)
f.SheetCount++
wb := f.workbookReader()
sheetID := 0
for _, v := range wb.Sheets.Sheet {
if v.SheetID > sheetID {
sheetID = v.SheetID
}
}
sheetID++
// Update docProps/app.xml
f.setAppXML()
// Update [Content_Types].xml
f.setContentTypes("/xl/worksheets/sheet"+strconv.Itoa(sheetID)+".xml", ContentTypeSpreadSheetMLWorksheet)
// Create new sheet /xl/worksheets/sheet%d.xml
f.setSheet(sheetID, name)
// Update workbook.xml.rels
rID := f.addRels(f.getWorkbookRelsPath(), SourceRelationshipWorkSheet, fmt.Sprintf("/xl/worksheets/sheet%d.xml", sheetID), "")
// Update workbook.xml
f.setWorkbook(name, sheetID, rID)
return f.GetSheetIndex(name)
}
Please note that , Worksheet names are case insensitive .
First check whether the payroll with the given name exists , If it doesn't exist GetSheetIndex It will return -1, Otherwise, return to the worksheet index .
func (f *File) GetSheetIndex(name string) int {
for index, sheet := range f.GetSheetList() {
if strings.EqualFold(sheet, trimSheetName(name)) {
return index
}
}
return -1
}
If GetSheetIndex It will return -1, That is, the worksheet exists , Just call DeleteSheet Delete this worksheet .
And then build a wb := f.workbookReader() Workbook reader .
for _, v := range wb.Sheets.Sheet {
if v.SheetID > sheetID {
sheetID = v.SheetID
}
}
The meaning of this code should be to find the maximum of the worksheet SheetID , Assign a value to sheetID.
Then create or update docProps/app.xml、[Content_Types].xml、/xl/worksheets/sheet%d.xml、 workbook.xml.rels、workbook.xml These documents .
// Update docProps/app.xml
f.setAppXML()
// Update [Content_Types].xml
f.setContentTypes("/xl/worksheets/sheet"+strconv.Itoa(sheetID)+".xml", ContentTypeSpreadSheetMLWorksheet)
// Create new sheet /xl/worksheets/sheet%d.xml
f.setSheet(sheetID, name)
// Update workbook.xml.rels
rID := f.addRels(f.getWorkbookRelsPath(), SourceRelationshipWorkSheet, fmt.Sprintf("/xl/worksheets/sheet%d.xml", sheetID), "")
// Update workbook.xml
f.setWorkbook(name, sheetID, rID)
Finally return to this Sheet.
3、 ... and 、 Conclusion
This is Lao Yue , This is a Go Interpretation of language related source code Chapter 5 , I'll keep trying , Bring you more similar articles , Please give me your advice .
边栏推荐
- 妙啊!美团 OCTO 分布式服务治理系统,这描述也太清晰了
- 认识盒子模型,盒子模型的边框、内外边距、水平布局、垂直布局、设置浮动、处理高度塌陷的基本方法
- 【管理篇 / 升级】* 02. 查看升级路径 * FortiGate 防火墙
- PMP备考心得 | 好的习惯、好的过程、好的结果
- input 输入完成时的触发事件
- 【面试:并发篇21:多线程:活跃性】死锁、活锁、饥饿
- 2022.7.22-----leetcode.757
- Talk about HART Protocol
- Template school jumpserver security operation and maintenance audit screen
- [concurrent programming] Chapter 2: go deep into the reentrantlock lock lock from the core source code
猜你喜欢
随机推荐
-Bash: wget: command not found
Amplitude limiting and deblocking filtering of botu PLC signal processing series
SQL用户表的通用设计
openresty lua-resty-balancer动态负载均衡
涅槃重生!字节大牛力荐大型分布式手册,凤凰架构让你浴火成神
BGP機房的優點
数论 —— 整除分块,常见经典例题。
基于时频图的音频处理-matlab
Under Arduino frame, esp32c3 +1.8 "TFT LCD is driven and displayed through tft_espi library
基于共轭梯度法的对抗攻击
Talk about HART Protocol
AirServer第三方投屏软件v7.3.0中文版 (Airplay终端实用工具)
How many points can you get on the latest UnionPay written test for test engineers?
Canal realizes MySQL data synchronization
[C language] file operation
2302. Count the number of subarrays with a score less than k - sliding array - double hundred code
驱动单片机硬件调试器的一些开源库总结(包含stlink调试器)
Airserver third party projection software v7.3.0 Chinese Version (airplay terminal utility)
TCP数据传输与性能
Advantages of implementing automatic network performance monitoring









