• Go-Excelize API源码阅读(三十三)—— RemoveCol


    Go-Excelize API源码阅读(三十三)——RemoveCol

    开源摘星计划(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 或更高版本。

    二、RemoveCol

    func (f *File) RemoveCol(sheet, col string) error
    根据给定的工作表名称和列名称删除指定列。例如,删除名为 Sheet1 的 C 列:

    err := f.RemoveCol("Sheet1", "C")
    请谨慎使用此方法,这将影响所有对该工作表中原有公式、图表等资源引用的更改。如果该工作表包含任何引用值,在使用此方法后使用 Excel 应用程序打开它时将可能导致文件错误。excelize 目前仅支持对工作表上部分引用的更新。

    废话少说,直接看源码:

    // RemoveCol provides a function to remove single column by given worksheet
    // name and column index. For example, remove column C in Sheet1:
    //
    //    err := f.RemoveCol("Sheet1", "C")
    //
    // Use this method with caution, which will affect changes in references such
    // as formulas, charts, and so on. If there is any referenced value of the
    // worksheet, it will cause a file error when you open it. The excelize only
    // partially updates these references currently.
    func (f *File) RemoveCol(sheet, col string) error {
    	num, err := ColumnNameToNumber(col)
    	if err != nil {
    		return err
    	}
    
    	ws, err := f.workSheetReader(sheet)
    	if err != nil {
    		return err
    	}
    	for rowIdx := range ws.SheetData.Row {
    		rowData := &ws.SheetData.Row[rowIdx]
    		for colIdx := range rowData.C {
    			colName, _, _ := SplitCellName(rowData.C[colIdx].R)
    			if colName == col {
    				rowData.C = append(rowData.C[:colIdx], rowData.C[colIdx+1:]...)[:len(rowData.C)-1]
    				break
    			}
    		}
    	}
    	return f.adjustHelper(sheet, columns, num, -1)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    先来看第一部分:

    	num, err := ColumnNameToNumber(col)
    	if err != nil {
    		return err
    	}
    
    • 1
    • 2
    • 3
    • 4
    // ColumnNameToNumber provides a function to convert Excel sheet column name
    // to int. Column name case-insensitive. The function returns an error if
    // column name incorrect.
    //
    // Example:
    //
    //     excelize.ColumnNameToNumber("AK") // returns 37, nil
    //
    func ColumnNameToNumber(name string) (int, error) {
    	if len(name) == 0 {
    		return -1, newInvalidColumnNameError(name)
    	}
    	col := 0
    	multi := 1
    	for i := len(name) - 1; i >= 0; i-- {
    		r := name[i]
    		if r >= 'A' && r <= 'Z' {
    			col += int(r-'A'+1) * multi
    		} else if r >= 'a' && r <= 'z' {
    			col += int(r-'a'+1) * multi
    		} else {
    			return -1, newInvalidColumnNameError(name)
    		}
    		multi *= 26
    	}
    	if col > MaxColumns {
    		return -1, ErrColumnNumber
    	}
    	return col, nil
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    该API的作用是将英文字母的列号转化为数字的列数,当列数为多个字母时,每转化一个字母,就执行multi *= 26,然后根据字母的ASCII码与’a’/'A’的关系计算序号。

    	ws, err := f.workSheetReader(sheet)
    	if err != nil {
    		return err
    	}
    
    • 1
    • 2
    • 3
    • 4

    打开工作表,然后如果err不为空,即返回err。

    	for rowIdx := range ws.SheetData.Row {
    		rowData := &ws.SheetData.Row[rowIdx]
    		for colIdx := range rowData.C {
    			colName, _, _ := SplitCellName(rowData.C[colIdx].R)
    			if colName == col {
    				rowData.C = append(rowData.C[:colIdx], rowData.C[colIdx+1:]...)[:len(rowData.C)-1]
    				break
    			}
    		}
    	}
    	return f.adjustHelper(sheet, columns, num, -1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    遍历工作表的Row对象,然后取每一行的数据,然后遍历此行的所有列,获取列号,然后调用SplitCellName获取列名

    excelize.SplitCellName("AK74") // return "AK", 74, nil
    
    • 1

    比较API参数的col是不是与列名一致,删除,如此然后将该列的所有行删除。

    结语

    这是Excelize API阅读的第三十三篇,鉴于本人技术水平有限,如有错误,烦请指出斧正!感谢!

  • 相关阅读:
    单元测试很难么?也没有吧
    三个练手的软件测试实战项目(附全套视频跟源码)偷偷卷死他们
    基于java找到并显示100以内的素数
    【面试高频题】二叉树“神级遍历“入门
    助力服务智能医疗检测,基于yolov5开发构建结直肠息肉检测系统,实践训练n/s/m不同量级模型,对比性能结果
    通过一道笔试题目,进行缓存与内存的性能比较及其分析测试
    安防监控EasyCVR视频汇聚平台使用海康SDK播放出现花屏是什么原因?
    C语言 驼峰命名法和下划线命名法
    【华为OD机试真题 python】数据分类【2022 Q4 | 200分】
    nodejs入门及常用模块(http、fs、path)
  • 原文地址:https://blog.csdn.net/qq_36045898/article/details/128022769