calcchain.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX files. Support reads and writes XLSX file generated by
  7. // Microsoft Excel™ 2007 and later. Support save file without losing original
  8. // charts of XLSX. This library needs Go version 1.8 or later.
  9. package excelize
  10. import "encoding/xml"
  11. // calcChainReader provides a function to get the pointer to the structure
  12. // after deserialization of xl/calcChain.xml.
  13. func (f *File) calcChainReader() *xlsxCalcChain {
  14. if f.CalcChain == nil {
  15. var c xlsxCalcChain
  16. _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML("xl/calcChain.xml")), &c)
  17. f.CalcChain = &c
  18. }
  19. return f.CalcChain
  20. }
  21. // calcChainWriter provides a function to save xl/calcChain.xml after
  22. // serialize structure.
  23. func (f *File) calcChainWriter() {
  24. if f.CalcChain != nil {
  25. output, _ := xml.Marshal(f.CalcChain)
  26. f.saveFileList("xl/calcChain.xml", output)
  27. }
  28. }
  29. // deleteCalcChain provides a function to remove cell reference on the
  30. // calculation chain.
  31. func (f *File) deleteCalcChain(axis string) {
  32. calc := f.calcChainReader()
  33. if calc != nil {
  34. for i, c := range calc.C {
  35. if c.R == axis {
  36. calc.C = append(calc.C[:i], calc.C[i+1:]...)
  37. }
  38. }
  39. }
  40. if len(calc.C) == 0 {
  41. f.CalcChain = nil
  42. delete(f.XLSX, "xl/calcChain.xml")
  43. content := f.contentTypesReader()
  44. for k, v := range content.Overrides {
  45. if v.PartName == "/xl/calcChain.xml" {
  46. content.Overrides = append(content.Overrides[:k], content.Overrides[k+1:]...)
  47. }
  48. }
  49. }
  50. }