代码拉取完成,页面将自动刷新
package internal
import (
"encoding/xml"
"math"
)
// Sheet represents a sheet.
type Sheet struct {
xml xSheet
name string
path string
}
// Name returns the sheet name.
func (s *Sheet) Name() string {
return s.name
}
// IRow returns the row by index.
func (s *Sheet) IRow(ridx int) *Row {
for len(s.xml.Rows) <= ridx {
s.xml.Rows = append(s.xml.Rows, new(Row))
}
return s.xml.Rows[ridx]
}
// Rows return the rows in the sheet.
func (s *Sheet) Rows() []*Row {
return s.xml.Rows
}
// Fill sheet with template values.
func (s *Sheet) Fill() {
for _, row := range s.Rows() {
row.fill()
}
}
// MarshalXML encodes the receiver as zero or more XML elements.
func (s *Sheet) MarshalXML(encoder *xml.Encoder, start xml.StartElement) error {
return encoder.Encode(&s.xml)
}
// UnmarshalXML decodes a single XML element beginning with the given start element.
func (s *Sheet) UnmarshalXML(decoder *xml.Decoder, start xml.StartElement) error {
err := decoder.DecodeElement(&s.xml, &start)
if err == nil {
normXMLSpace(&s.xml.Attrs)
}
return err
}
func (s *Sheet) clone() *Sheet {
sheet := *s
sheet.xml.Rows = nil
return &sheet
}
func (s *Sheet) parse() error {
for _, row := range s.Rows() {
if err := row.parse(); err != nil {
return err
}
}
return nil
}
func (s *Sheet) put(ridx int, row *Row) {
if ridx < len(s.xml.Rows) {
s.xml.Rows[ridx] = row
} else {
for ridx > len(s.xml.Rows) {
s.xml.Rows = append(s.xml.Rows, new(Row))
}
s.xml.Rows = append(s.xml.Rows, row)
}
}
func (s *Sheet) merge(ref string) {
ridx1, cidx1, ridx2, cidx2 := RangeIdx(ref)
primary := s.IRow(ridx1).ICell(cidx1)
for i := ridx1; i <= ridx2; i++ {
for j := cidx1; j <= cidx2; j++ {
s.IRow(i).ICell(j).primary = primary
}
}
}
func (s *Sheet) afterDecode(sst *SST) {
rs := s.xml.Rows
s.xml.Rows = nil
for i, r := range rs {
row := *r
row.xml.Cells = nil
ridx := RowIdx(r.xml.Ref)
if ridx < 0 {
ridx = i
}
s.put(ridx, &row)
for j, cell := range r.xml.Cells {
if cell.xml.Type == "s" {
if ssi := sst.Get(cell.xml.Value); ssi != nil {
if ssi.IsRich() {
cell.xml.Type = "inlineStr"
cell.xml.Is = ssi
} else {
cell.str = ssi.String()
}
}
}
_, cidx := CellIdx(cell.xml.Ref)
if cidx < 0 {
cidx = j
}
row.put(cidx, cell)
}
}
for _, entry := range s.xml.MergeCells.Entries {
s.merge(entry.Ref)
}
}
func (s *Sheet) beforeEncode(sst *SST) {
s.xml.MergeCells.Entries = nil
ridx1, cidx1 := math.MaxInt, math.MaxInt
ridx2, cidx2 := 0, 0
for i, row := range s.Rows() {
if row.isEmpty() {
continue
}
row.xml.Ref = RowRef(i)
if ridx1 > i {
ridx1 = i
}
if ridx2 < i {
ridx2 = i
}
sidx1, sidx2 := math.MaxInt, 0
for j, cell := range row.Cells() {
if cell.isEmpty() {
continue
}
cell.xml.Ref = ColRef(j) + row.xml.Ref
if cell.xml.Type == "s" {
cell.xml.Value = sst.Put(cell.str)
}
if sidx1 > j {
sidx1 = j
}
if sidx2 < j {
sidx2 = j
}
if cell.primary == cell {
s.addMergeCell(cell, i, j)
}
}
if sidx1 > sidx2 {
sidx1 = sidx2
}
row.xml.Spans = RowRef(sidx1) + ":" + RowRef(sidx2)
if cidx1 > sidx1 {
cidx1 = sidx1
}
if cidx2 < sidx2 {
cidx2 = sidx2
}
}
if ridx1 > ridx2 {
ridx1 = ridx2
}
s.xml.Dimension.Ref = RangeRef(ridx1, cidx1, ridx2, cidx2)
s.xml.MergeCells.Count = len(s.xml.MergeCells.Entries)
}
func (s *Sheet) addMergeCell(primary *Cell, ridx, cidx int) {
n := 0
for _, row := range s.xml.Rows[ridx+1:] {
if len(row.xml.Cells) <= cidx || row.xml.Cells[cidx].primary != primary {
break
}
n++
}
m := 0
for _, cell := range s.xml.Rows[ridx].xml.Cells[cidx+1:] {
if cell.primary != primary {
break
}
m++
}
if n > 0 || m > 0 {
s.xml.MergeCells.Entries = append(s.xml.MergeCells.Entries, &MergeCell{Ref: RangeRef(ridx, cidx, ridx+n, cidx+m)})
}
}
// Dimension represents a dimension element.
type Dimension struct {
Ref string `xml:"ref,attr"`
}
// MergeCells represents the mergeCell elements.
type MergeCells struct {
Count int `xml:"count,attr"`
Entries []*MergeCell `xml:"mergeCell,omitempty"`
}
// MergeCell represents a mergeCell element.
type MergeCell struct {
Ref string `xml:"ref,attr"`
}
type xSheet struct {
XMLName xml.Name
Attrs []xml.Attr `xml:",any,attr,omitempty"`
SheetPr *Element `xml:"sheetPr,omitempty"`
Dimension Dimension `xml:"dimension"`
SheetViews *Element `xml:"sheetViews,omitempty"`
SheetFormatPr *Element `xml:"sheetFormatPr"`
Cols *Element `xml:"cols,omitempty"`
Rows []*Row `xml:"sheetData>row"`
SheetProtection *Element `xml:"sheetProtection,omitempty"`
AutoFilter *Element `xml:"autoFilter,omitempty"`
MergeCells MergeCells `xml:"mergeCells"`
ConditionalFormatting *Element `xml:"conditionalFormatting,omitempty"`
DataValidations *Element `xml:"dataValidations,omitempty"`
Hyperlinks *Element `xml:"hyperlinks,omitempty"`
PrintOptions *Element `xml:"printOptions,omitempty"`
PageMargins *Element `xml:"pageMargins,omitempty"`
PageSetup *Element `xml:"pageSetup,omitempty"`
HeaderFooter *Element `xml:"headerFooter,omitempty"`
Drawing *Element `xml:"drawing,omitempty"`
LegacyDrawing *Element `xml:"legacyDrawing,omitempty"`
TableParts *Element `xml:"tableParts,omitempty"`
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。