Skip to content

Commit 1fe660d

Browse files
committedApr 22, 2020
- Resolve qax-os#485 use sheet index instead of ID
- added 3 internal function: getSheetID, getActiveSheetID, getSheetNameByID
1 parent 10115b5 commit 1fe660d

13 files changed

+136
-74
lines changed
 

‎README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
## Introduction
1515

16-
Excelize is a library written in pure Go providing a set of functions that allow you to write to and read from XLSX files. Supports reading and writing XLSX file generated by Microsoft Excel™ 2007 and later.
17-
Supports saving a file without losing original charts of XLSX. This library needs Go version 1.10 or later. The full API docs can be seen using go's built-in documentation tool, or online at [go.dev](https://pkg.go.dev/github.com/360EntSecGroup-Skylar/excelize/v2?tab=doc) and [docs reference](https://xuri.me/excelize/).
16+
Excelize is a library written in pure Go providing a set of functions that allow you to write to and read from XLSX / XLSM / XLTM files. Supports reading and writing spreadsheet documents generated by Microsoft Excel™ 2007 and later. Supports complex components by high compatibility, and provided streaming API for generating or reading data from a worksheet with huge amounts of data. This library needs Go version 1.10 or later. The full API docs can be seen using go's built-in documentation tool, or online at [go.dev](https://pkg.go.dev/github.com/360EntSecGroup-Skylar/excelize/v2?tab=doc) and [docs reference](https://xuri.me/excelize/).
1817

1918
## Basic Usage
2019

@@ -24,6 +23,12 @@ Supports saving a file without losing original charts of XLSX. This library need
2423
go get github.com/360EntSecGroup-Skylar/excelize
2524
```
2625

26+
- If your package management with [Go Modules](https://blog.golang.org/using-go-modules), please install with following command.
27+
28+
```bash
29+
go get github.com/360EntSecGroup-Skylar/excelize/v2
30+
```
31+
2732
### Create XLSX file
2833

2934
Here is a minimal example usage that will create XLSX file.

‎README_zh.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
## 简介
1515

16-
Excelize 是 Go 语言编写的用于操作 Office Excel 文档类库,基于 ECMA-376 Office OpenXML 标准。可以使用它来读取、写入由 Microsoft Excel™ 2007 及以上版本创建的 XLSX 文档。相比较其他的开源类库,Excelize 支持写入原本带有图片(表)、透视表和切片器等复杂样式的文档,还支持向 Excel 文档中插入图片与图表,并且在保存后不会丢失文档原有样式,可以应用于各类报表系统中。使用本类库要求使用的 Go 语言为 1.10 或更高版本,完整的 API 使用文档请访问 [go.dev](https://pkg.go.dev/github.com/360EntSecGroup-Skylar/excelize/v2?tab=doc) 或查看 [参考文档](https://xuri.me/excelize/)
16+
Excelize 是 Go 语言编写的用于操作 Office Excel 文档基础库,基于 ECMA-376,ISO/IEC 29500 国际标准。可以使用它来读取、写入由 Microsoft Excel™ 2007 及以上版本创建的电子表格文档。支持 XLSX / XLSM / XLTM 等多种文档格式,高度兼容带有样式、图片(表)、透视表、切片器等复杂组件的文档,并提供流式读写 API,用于处理包含大规模数据的工作簿。可应用于各类报表平台、云计算、边缘计算等系统。使用本类库要求使用的 Go 语言为 1.10 或更高版本,完整的 API 使用文档请访问 [go.dev](https://pkg.go.dev/github.com/360EntSecGroup-Skylar/excelize/v2?tab=doc) 或查看 [参考文档](https://xuri.me/excelize/)
1717

1818
## 快速上手
1919

@@ -23,6 +23,12 @@ Excelize 是 Go 语言编写的用于操作 Office Excel 文档类库,基于 E
2323
go get github.com/360EntSecGroup-Skylar/excelize
2424
```
2525

26+
- 如果您使用 [Go Modules](https://blog.golang.org/using-go-modules) 管理软件包,请使用下面的命令来安装最新版本。
27+
28+
```bash
29+
go get github.com/360EntSecGroup-Skylar/excelize/v2
30+
```
31+
2632
### 创建 Excel 文档
2733

2834
下面是一个创建 Excel 文档的简单例子:

‎cell.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ func (f *File) SetCellFormula(sheet, axis, formula string, opts ...FormulaOpts)
337337
}
338338
if formula == "" {
339339
cellData.F = nil
340-
f.deleteCalcChain(f.GetSheetIndex(sheet), axis)
340+
f.deleteCalcChain(f.getSheetID(sheet), axis)
341341
return err
342342
}
343343

‎chart.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ func (f *File) AddChart(sheet, cell, format string, combo ...string) error {
762762
// a chart.
763763
func (f *File) AddChartSheet(sheet, format string, combo ...string) error {
764764
// Check if the worksheet already exists
765-
if f.GetSheetIndex(sheet) != 0 {
765+
if f.GetSheetIndex(sheet) != -1 {
766766
return errors.New("the same name worksheet already exists")
767767
}
768768
formatSet, comboCharts, err := f.getFormatChart(format, combo)

‎chart_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
func TestChartSize(t *testing.T) {
1414
xlsx := NewFile()
15-
sheet1 := xlsx.GetSheetName(1)
15+
sheet1 := xlsx.GetSheetName(0)
1616

1717
categories := map[string]string{
1818
"A2": "Small",
@@ -220,14 +220,14 @@ func TestAddChartSheet(t *testing.T) {
220220
}
221221
assert.NoError(t, f.AddChartSheet("Chart1", `{"type":"col3DClustered","series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],"title":{"name":"Fruit 3D Clustered Column Chart"}}`))
222222
// Test set the chartsheet as active sheet
223-
var sheetID int
224-
for idx, sheetName := range f.GetSheetMap() {
223+
var sheetIdx int
224+
for idx, sheetName := range f.GetSheetList() {
225225
if sheetName != "Chart1" {
226226
continue
227227
}
228-
sheetID = idx
228+
sheetIdx = idx
229229
}
230-
f.SetActiveSheet(sheetID)
230+
f.SetActiveSheet(sheetIdx)
231231

232232
// Test cell value on chartsheet
233233
assert.EqualError(t, f.SetCellValue("Chart1", "A1", true), "sheet Chart1 is chart sheet")

‎col_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func TestColWidth(t *testing.T) {
170170

171171
func TestInsertCol(t *testing.T) {
172172
f := NewFile()
173-
sheet1 := f.GetSheetName(1)
173+
sheet1 := f.GetSheetName(0)
174174

175175
fillCells(f, sheet1, 10, 10)
176176

@@ -188,7 +188,7 @@ func TestInsertCol(t *testing.T) {
188188

189189
func TestRemoveCol(t *testing.T) {
190190
f := NewFile()
191-
sheet1 := f.GetSheetName(1)
191+
sheet1 := f.GetSheetName(0)
192192

193193
fillCells(f, sheet1, 10, 15)
194194

‎excelize.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (f *File) UpdateLinkedValue() error {
268268
wb := f.workbookReader()
269269
// recalculate formulas
270270
wb.CalcPr = nil
271-
for _, name := range f.GetSheetMap() {
271+
for _, name := range f.GetSheetList() {
272272
xlsx, err := f.workSheetReader(name)
273273
if err != nil {
274274
return err

‎excelize_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func TestBrokenFile(t *testing.T) {
239239
// Test set active sheet without BookViews and Sheets maps in xl/workbook.xml.
240240
f3, err := OpenFile(filepath.Join("test", "BadWorkbook.xlsx"))
241241
f3.GetActiveSheetIndex()
242-
f3.SetActiveSheet(2)
242+
f3.SetActiveSheet(1)
243243
assert.NoError(t, err)
244244
})
245245

@@ -908,7 +908,7 @@ func TestCopySheet(t *testing.T) {
908908
}
909909

910910
idx := f.NewSheet("CopySheet")
911-
assert.NoError(t, f.CopySheet(1, idx))
911+
assert.NoError(t, f.CopySheet(0, idx))
912912

913913
assert.NoError(t, f.SetCellValue("CopySheet", "F1", "Hello"))
914914
val, err := f.GetCellValue("Sheet1", "F1")
@@ -924,8 +924,8 @@ func TestCopySheetError(t *testing.T) {
924924
t.FailNow()
925925
}
926926

927-
assert.EqualError(t, f.copySheet(0, -1), "sheet is not exist")
928-
if !assert.EqualError(t, f.CopySheet(0, -1), "invalid worksheet index") {
927+
assert.EqualError(t, f.copySheet(-1, -2), "sheet is not exist")
928+
if !assert.EqualError(t, f.CopySheet(-1, -2), "invalid worksheet index") {
929929
t.FailNow()
930930
}
931931

@@ -957,7 +957,7 @@ func TestSetSheetVisible(t *testing.T) {
957957
func TestGetActiveSheetIndex(t *testing.T) {
958958
f := NewFile()
959959
f.WorkBook.BookViews = nil
960-
assert.Equal(t, 1, f.GetActiveSheetIndex())
960+
assert.Equal(t, 0, f.GetActiveSheetIndex())
961961
}
962962

963963
func TestRelsWriter(t *testing.T) {
@@ -974,7 +974,7 @@ func TestGetSheetView(t *testing.T) {
974974

975975
func TestConditionalFormat(t *testing.T) {
976976
f := NewFile()
977-
sheet1 := f.GetSheetName(1)
977+
sheet1 := f.GetSheetName(0)
978978

979979
fillCells(f, sheet1, 10, 15)
980980

@@ -1060,7 +1060,7 @@ func TestConditionalFormat(t *testing.T) {
10601060

10611061
func TestConditionalFormatError(t *testing.T) {
10621062
f := NewFile()
1063-
sheet1 := f.GetSheetName(1)
1063+
sheet1 := f.GetSheetName(0)
10641064

10651065
fillCells(f, sheet1, 10, 15)
10661066

‎merge_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestGetMergeCells(t *testing.T) {
108108
if !assert.NoError(t, err) {
109109
t.FailNow()
110110
}
111-
sheet1 := f.GetSheetName(1)
111+
sheet1 := f.GetSheetName(0)
112112

113113
mergeCells, err := f.GetMergeCells(sheet1)
114114
if !assert.Len(t, mergeCells, len(wants)) {
@@ -132,7 +132,7 @@ func TestUnmergeCell(t *testing.T) {
132132
if !assert.NoError(t, err) {
133133
t.FailNow()
134134
}
135-
sheet1 := f.GetSheetName(1)
135+
sheet1 := f.GetSheetName(0)
136136

137137
xlsx, err := f.workSheetReader(sheet1)
138138
assert.NoError(t, err)

‎rows_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func TestRowsError(t *testing.T) {
9292

9393
func TestRowHeight(t *testing.T) {
9494
xlsx := NewFile()
95-
sheet1 := xlsx.GetSheetName(1)
95+
sheet1 := xlsx.GetSheetName(0)
9696

9797
assert.EqualError(t, xlsx.SetRowHeight(sheet1, 0, defaultRowHeightPixels+1.0), "invalid row number 0")
9898

@@ -199,7 +199,7 @@ func TestRowVisibility(t *testing.T) {
199199

200200
func TestRemoveRow(t *testing.T) {
201201
f := NewFile()
202-
sheet1 := f.GetSheetName(1)
202+
sheet1 := f.GetSheetName(0)
203203
r, err := f.workSheetReader(sheet1)
204204
assert.NoError(t, err)
205205
const (
@@ -260,7 +260,7 @@ func TestRemoveRow(t *testing.T) {
260260

261261
func TestInsertRow(t *testing.T) {
262262
xlsx := NewFile()
263-
sheet1 := xlsx.GetSheetName(1)
263+
sheet1 := xlsx.GetSheetName(0)
264264
r, err := xlsx.workSheetReader(sheet1)
265265
assert.NoError(t, err)
266266
const (
@@ -292,7 +292,7 @@ func TestInsertRow(t *testing.T) {
292292
// It is important for insert workflow to be constant to avoid side effect with functions related to internal structure.
293293
func TestInsertRowInEmptyFile(t *testing.T) {
294294
xlsx := NewFile()
295-
sheet1 := xlsx.GetSheetName(1)
295+
sheet1 := xlsx.GetSheetName(0)
296296
r, err := xlsx.workSheetReader(sheet1)
297297
assert.NoError(t, err)
298298
assert.NoError(t, xlsx.InsertRow(sheet1, 1))

0 commit comments

Comments
 (0)