You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.1KB

  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.10 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 && f.CalcChain.C != 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(index int, axis string) {
  32. calc := f.calcChainReader()
  33. if calc != nil {
  34. calc.C = xlsxCalcChainCollection(calc.C).Filter(func(c xlsxCalcChainC) bool {
  35. return !((c.I == index && c.R == axis) || (c.I == index && axis == ""))
  36. })
  37. }
  38. if len(calc.C) == 0 {
  39. f.CalcChain = nil
  40. delete(f.XLSX, "xl/calcChain.xml")
  41. content := f.contentTypesReader()
  42. for k, v := range content.Overrides {
  43. if v.PartName == "/xl/calcChain.xml" {
  44. content.Overrides = append(content.Overrides[:k], content.Overrides[k+1:]...)
  45. }
  46. }
  47. }
  48. }
  49. type xlsxCalcChainCollection []xlsxCalcChainC
  50. // Filter provides a function to filter calculation chain.
  51. func (c xlsxCalcChainCollection) Filter(fn func(v xlsxCalcChainC) bool) []xlsxCalcChainC {
  52. var results []xlsxCalcChainC
  53. for _, v := range c {
  54. if fn(v) {
  55. results = append(results, v)
  56. }
  57. }
  58. return results
  59. }