Open file error return added and UpdateLinkedValue function added to fix linked values within a spreadsheet are not updating.

formula
Ri Xu 8 years ago
parent 50863294f9
commit df8f85d6ab

@ -34,12 +34,15 @@ import (
) )
func main() { func main() {
xlsx := excelize.CreateFile() xlsx, err := excelize.CreateFile()
if err != nil {
fmt.Println(err)
}
xlsx.NewSheet(2, "Sheet2") xlsx.NewSheet(2, "Sheet2")
xlsx.NewSheet(3, "Sheet3") xlsx.NewSheet(3, "Sheet3")
xlsx.SetCellInt("Sheet2", "A23", 10) xlsx.SetCellInt("Sheet2", "A23", 10)
xlsx.SetCellStr("Sheet3", "B20", "Hello") xlsx.SetCellStr("Sheet3", "B20", "Hello")
err := xlsx.WriteTo("/home/Workbook.xlsx") err = xlsx.WriteTo("/home/Workbook.xlsx")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
@ -59,14 +62,17 @@ import (
) )
func main() { func main() {
xlsx := excelize.OpenFile("/home/Workbook.xlsx") xlsx, err := excelize.OpenFile("/home/Workbook.xlsx")
if err != nil {
fmt.Println(err)
}
xlsx.SetCellInt("Sheet2", "B2", 100) xlsx.SetCellInt("Sheet2", "B2", 100)
xlsx.SetCellStr("Sheet2", "C11", "Hello") xlsx.SetCellStr("Sheet2", "C11", "Hello")
xlsx.NewSheet(3, "TestSheet") xlsx.NewSheet(3, "TestSheet")
xlsx.SetCellInt("Sheet3", "A23", 10) xlsx.SetCellInt("Sheet3", "A23", 10)
xlsx.SetCellStr("Sheet3", "b230", "World") xlsx.SetCellStr("Sheet3", "b230", "World")
xlsx.SetActiveSheet(2) xlsx.SetActiveSheet(2)
err := xlsx.Save() err = xlsx.Save()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
@ -84,7 +90,10 @@ import (
) )
func main() { func main() {
xlsx := excelize.OpenFile("/home/Workbook.xlsx") xlsx, err := excelize.OpenFile("/home/Workbook.xlsx")
if err != nil {
fmt.Println(err)
}
cell := xlsx.GetCellValue("Sheet2", "D11") cell := xlsx.GetCellValue("Sheet2", "D11")
fmt.Println(cell) fmt.Println(cell)
} }

@ -11,21 +11,28 @@ import (
// File define a populated xlsx.File struct. // File define a populated xlsx.File struct.
type File struct { type File struct {
XLSX map[string]string XLSX map[string]string
Path string Path string
SheetCount int
} }
// OpenFile take the name of an XLSX file and returns a populated // OpenFile take the name of an XLSX file and returns a populated
// xlsx.File struct for it. // xlsx.File struct for it.
func OpenFile(filename string) *File { func OpenFile(filename string) (*File, error) {
var f *zip.ReadCloser var f *zip.ReadCloser
var err error
file := make(map[string]string) file := make(map[string]string)
f, _ = zip.OpenReader(filename) sheetCount := 0
file, _ = ReadZip(f) f, err = zip.OpenReader(filename)
return &File{ if err != nil {
XLSX: file, return &File{}, err
Path: filename,
} }
file, sheetCount, err = ReadZip(f)
return &File{
XLSX: file,
Path: filename,
SheetCount: sheetCount,
}, nil
} }
// SetCellInt provide function to set int type value of a cell // SetCellInt provide function to set int type value of a cell
@ -176,6 +183,7 @@ func replaceWorkSheetsRelationshipsNameSpace(workbookMarshal string) string {
// <c r="G15" s="1" /> // <c r="G15" s="1" />
// </row> // </row>
// //
// Noteice: this method could be very slow for large spreadsheets (more than 3000 rows one sheet).
func checkRow(xlsx xlsxWorksheet) xlsxWorksheet { func checkRow(xlsx xlsxWorksheet) xlsxWorksheet {
buffer := bytes.Buffer{} buffer := bytes.Buffer{}
for k, v := range xlsx.SheetData.Row { for k, v := range xlsx.SheetData.Row {
@ -207,3 +215,44 @@ 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
//
// Notice: after open XLSX file Excel will be update linked value and generate
// new value and will prompt save file or not.
//
// For example:
//
// <row r="19" spans="2:2">
// <c r="B19">
// <f>SUM(Sheet2!D2,Sheet2!D11)</f>
// <v>100</v>
// </c>
// </row>
//
// to
//
// <row r="19" spans="2:2">
// <c r="B19">
// <f>SUM(Sheet2!D2,Sheet2!D11)</f>
// </c>
// </row>
func (f *File) UpdateLinkedValue() {
for i := 1; i <= f.SheetCount; i++ {
var xlsx xlsxWorksheet
name := `xl/worksheets/sheet` + strconv.Itoa(i) + `.xml`
xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
for indexR, row := range xlsx.SheetData.Row {
for indexC, col := range row.C {
if col.F != nil && col.V != `` {
xlsx.SheetData.Row[indexR].C[indexC].V = ``
xlsx.SheetData.Row[indexR].C[indexC].T = ``
}
}
}
output, _ := xml.Marshal(xlsx)
f.saveFileList(name, replaceRelationshipsID(replaceWorkSheetsRelationshipsNameSpace(string(output))))
}
}

@ -7,7 +7,11 @@ import (
func TestExcelize(t *testing.T) { func TestExcelize(t *testing.T) {
// Test update a XLSX file // Test update a XLSX file
file := OpenFile("./test/Workbook1.xlsx") file, err := OpenFile("./test/Workbook1.xlsx")
if err != nil {
t.Log(err)
}
file.UpdateLinkedValue()
file.SetCellInt("SHEET2", "B2", 100) file.SetCellInt("SHEET2", "B2", 100)
file.SetCellStr("SHEET2", "C11", "Knowns") file.SetCellStr("SHEET2", "C11", "Knowns")
file.NewSheet(3, "TestSheet") file.NewSheet(3, "TestSheet")
@ -15,10 +19,20 @@ func TestExcelize(t *testing.T) {
file.SetCellStr("SHEET3", "b230", "10") file.SetCellStr("SHEET3", "b230", "10")
file.SetCellStr("SHEET10", "b230", "10") file.SetCellStr("SHEET10", "b230", "10")
file.SetActiveSheet(2) file.SetActiveSheet(2)
// Test read cell value with given illegal rows number
file.GetCellValue("Sheet2", "a-1")
// Test read cell value with given lowercase column number
file.GetCellValue("Sheet2", "a5")
file.GetCellValue("Sheet2", "C11")
file.GetCellValue("Sheet2", "D11")
file.GetCellValue("Sheet2", "D12")
// Test read cell value with given axis large than exists row
file.GetCellValue("Sheet2", "E13")
for i := 1; i <= 300; i++ { for i := 1; i <= 300; i++ {
file.SetCellStr("SHEET3", "c"+strconv.Itoa(i), strconv.Itoa(i)) file.SetCellStr("SHEET3", "c"+strconv.Itoa(i), strconv.Itoa(i))
} }
err := file.Save() err = file.Save()
if err != nil { if err != nil {
t.Log(err) t.Log(err)
} }
@ -45,13 +59,9 @@ func TestExcelize(t *testing.T) {
t.Log(err) t.Log(err)
} }
// Test read cell value with given illegal rows number // Test open a XLSX file with given illegal path
file.GetCellValue("Sheet2", "a-1") _, err = OpenFile("./test/Workbook.xlsx")
// Test read cell value with given lowercase column number if err != nil {
file.GetCellValue("Sheet2", "a5") t.Log(err)
file.GetCellValue("Sheet2", "C11") }
file.GetCellValue("Sheet2", "D11")
file.GetCellValue("Sheet2", "D12")
// Test read cell value with given axis large than exists row
file.GetCellValue("Sheet2", "E13")
} }

@ -14,19 +14,25 @@ import (
// ReadZip takes a pointer to a zip.ReadCloser and returns a // ReadZip takes a pointer to a zip.ReadCloser and returns a
// xlsx.File struct populated with its contents. In most cases // xlsx.File struct populated with its contents. In most cases
// ReadZip is not used directly, but is called internally by OpenFile. // ReadZip is not used directly, but is called internally by OpenFile.
func ReadZip(f *zip.ReadCloser) (map[string]string, error) { func ReadZip(f *zip.ReadCloser) (map[string]string, int, error) {
defer f.Close() defer f.Close()
return ReadZipReader(&f.Reader) return ReadZipReader(&f.Reader)
} }
// ReadZipReader can be used to read an XLSX in memory without // ReadZipReader can be used to read an XLSX in memory without
// touching the filesystem. // touching the filesystem.
func ReadZipReader(r *zip.Reader) (map[string]string, error) { func ReadZipReader(r *zip.Reader) (map[string]string, int, error) {
fileList := make(map[string]string) fileList := make(map[string]string)
worksheets := 0
for _, v := range r.File { for _, v := range r.File {
fileList[v.Name] = readFile(v) fileList[v.Name] = readFile(v)
if len(v.Name) > 18 {
if v.Name[0:19] == "xl/worksheets/sheet" {
worksheets++
}
}
} }
return fileList, nil return fileList, worksheets, nil
} }
// Read XML content as string and replace drawing property in XML namespace of sheet // Read XML content as string and replace drawing property in XML namespace of sheet

Binary file not shown.
Loading…
Cancel
Save