浏览代码

Move execute checkRow logic when XLSX file open, speed up library write file.

Ri Xu 9 年之前
父节点
当前提交
0dd0fba96b
共有 3 个文件被更改,包括 12 次插入8 次删除
  1. 5 7
      excelize.go
  2. 1 1
      excelize_test.go
  3. 6 0
      lib.go

+ 5 - 7
excelize.go

@@ -27,7 +27,7 @@ func OpenFile(filename string) (*File, error) {
 	if err != nil {
 	if err != nil {
 		return &File{}, err
 		return &File{}, err
 	}
 	}
-	file, sheetCount, err = ReadZip(f)
+	file, sheetCount, _ = ReadZip(f)
 	return &File{
 	return &File{
 		XLSX:       file,
 		XLSX:       file,
 		Path:       filename,
 		Path:       filename,
@@ -50,7 +50,6 @@ func (f *File) SetCellInt(sheet string, axis string, value int) {
 	rows := xAxis + 1
 	rows := xAxis + 1
 	cell := yAxis + 1
 	cell := yAxis + 1
 
 
-	xlsx = checkRow(xlsx)
 	xlsx = completeRow(xlsx, rows, cell)
 	xlsx = completeRow(xlsx, rows, cell)
 	xlsx = completeCol(xlsx, rows, cell)
 	xlsx = completeCol(xlsx, rows, cell)
 
 
@@ -79,7 +78,6 @@ func (f *File) SetCellStr(sheet string, axis string, value string) {
 	rows := xAxis + 1
 	rows := xAxis + 1
 	cell := yAxis + 1
 	cell := yAxis + 1
 
 
-	xlsx = checkRow(xlsx)
 	xlsx = completeRow(xlsx, rows, cell)
 	xlsx = completeRow(xlsx, rows, cell)
 	xlsx = completeCol(xlsx, rows, cell)
 	xlsx = completeCol(xlsx, rows, cell)
 
 
@@ -193,7 +191,7 @@ func checkRow(xlsx xlsxWorksheet) xlsxWorksheet {
 		}
 		}
 		endR := getColIndex(v.C[lenCol-1].R)
 		endR := getColIndex(v.C[lenCol-1].R)
 		endRow := getRowIndex(v.C[lenCol-1].R)
 		endRow := getRowIndex(v.C[lenCol-1].R)
-		endCol := titleToNumber(endR)
+		endCol := titleToNumber(endR) + 1
 		if lenCol < endCol {
 		if lenCol < endCol {
 			oldRow := xlsx.SheetData.Row[k].C
 			oldRow := xlsx.SheetData.Row[k].C
 			xlsx.SheetData.Row[k].C = xlsx.SheetData.Row[k].C[:0]
 			xlsx.SheetData.Row[k].C = xlsx.SheetData.Row[k].C[:0]
@@ -216,9 +214,9 @@ func checkRow(xlsx xlsxWorksheet) xlsxWorksheet {
 	return xlsx
 	return xlsx
 }
 }
 
 
-// UpdateLinkedValue fix linked values within a spreadsheet are not updating.
-// This function will be remove value tag when met a cell have a linked value.
-// Reference https://social.technet.microsoft.com/Forums/office/en-US/e16bae1f-6a2c-4325-8013-e989a3479066/excel-2010-linked-cells-not-updating?forum=excel
+// UpdateLinkedValue fix linked values within a spreadsheet are not updating in
+// Office Excel 2007 and 2010. This function will be remove value tag when met a
+//  cell have a linked value. Reference https://social.technet.microsoft.com/Forums/office/en-US/e16bae1f-6a2c-4325-8013-e989a3479066/excel-2010-linked-cells-not-updating?forum=excel
 //
 //
 // Notice: after open XLSX file Excel will be update linked value and generate
 // Notice: after open XLSX file Excel will be update linked value and generate
 // new value and will prompt save file or not.
 // new value and will prompt save file or not.

+ 1 - 1
excelize_test.go

@@ -12,7 +12,7 @@ func TestExcelize(t *testing.T) {
 		t.Log(err)
 		t.Log(err)
 	}
 	}
 	file.UpdateLinkedValue()
 	file.UpdateLinkedValue()
-	file.SetCellInt("SHEET2", "B2", 100)
+	file.SetCellInt("SHEET2", "A1", 100)
 	file.SetCellStr("SHEET2", "C11", "Knowns")
 	file.SetCellStr("SHEET2", "C11", "Knowns")
 	file.NewSheet(3, "TestSheet")
 	file.NewSheet(3, "TestSheet")
 	file.SetCellInt("Sheet3", "A23", 10)
 	file.SetCellInt("Sheet3", "A23", 10)

+ 6 - 0
lib.go

@@ -3,6 +3,7 @@ package excelize
 import (
 import (
 	"archive/zip"
 	"archive/zip"
 	"bytes"
 	"bytes"
+	"encoding/xml"
 	"io"
 	"io"
 	"log"
 	"log"
 	"math"
 	"math"
@@ -28,6 +29,11 @@ func ReadZipReader(r *zip.Reader) (map[string]string, int, error) {
 		fileList[v.Name] = readFile(v)
 		fileList[v.Name] = readFile(v)
 		if len(v.Name) > 18 {
 		if len(v.Name) > 18 {
 			if v.Name[0:19] == "xl/worksheets/sheet" {
 			if v.Name[0:19] == "xl/worksheets/sheet" {
+				var xlsx xlsxWorksheet
+				xml.Unmarshal([]byte(strings.Replace(fileList[v.Name], "<drawing r:id=", "<drawing rid=", -1)), &xlsx)
+				xlsx = checkRow(xlsx)
+				output, _ := xml.Marshal(xlsx)
+				fileList[v.Name] = replaceRelationshipsID(replaceWorkSheetsRelationshipsNameSpace(string(output)))
 				worksheets++
 				worksheets++
 			}
 			}
 		}
 		}