@@ -644,4 +644,170 @@ export class Worksheet {
644
644
setColumnFormats ( columnFormats : ExcelColumnFormat [ ] ) {
645
645
this . columnFormats = columnFormats ;
646
646
}
647
+
648
+ /**
649
+ * Returns worksheet XML header (everything before <sheetData>)
650
+ */
651
+ getWorksheetXmlHeader ( ) : string {
652
+ // const doc = Util.createXmlDoc(Util.schemas.spreadsheetml, 'worksheet');
653
+ // const worksheet = doc.documentElement;
654
+ // worksheet.setAttribute('xmlns:r', Util.schemas.relationships);
655
+ // worksheet.setAttribute('xmlns:mc', Util.schemas.markupCompat);
656
+
657
+ // let maxX = 0;
658
+ // const data = this.data;
659
+ // const columns = this.columns || [];
660
+ // for (let row = 0, l = data.length; row < l; row++) {
661
+ // const cellCount = data[row].length;
662
+ // maxX = cellCount > maxX ? cellCount : maxX;
663
+ // }
664
+
665
+ // if (maxX !== 0) {
666
+ // worksheet.appendChild(
667
+ // Util.createElement(doc, 'dimension', [
668
+ // ['ref', `${Util.positionToLetterRef(1, 1)}:${Util.positionToLetterRef(maxX, String(data.length))}`],
669
+ // ]),
670
+ // );
671
+ // } else {
672
+ // worksheet.appendChild(Util.createElement(doc, 'dimension', [['ref', Util.positionToLetterRef(1, 1)]]));
673
+ // }
674
+
675
+ // worksheet.appendChild(this.sheetView.exportXML(doc));
676
+
677
+ // if (this.columns.length) {
678
+ // worksheet.appendChild(this.exportColumns(doc));
679
+ // }
680
+
681
+ // // Add <sheetData> start tag
682
+ // const xml = doc.toString();
683
+ // return xml.substring(0, xml.indexOf('<sheetData>') + '<sheetData>'.length);
684
+ return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
685
+ <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
686
+ xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
687
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
688
+ <sheetData>` ;
689
+ }
690
+
691
+ /**
692
+ * Returns worksheet XML footer (everything after </sheetData>)
693
+ */
694
+ getWorksheetXmlFooter ( ) : string {
695
+ // const doc = Util.createXmlDoc(Util.schemas.spreadsheetml, 'worksheet');
696
+ // const worksheet = doc.documentElement;
697
+
698
+ // // Add all elements after <sheetData>
699
+ // if (this.sheetProtection) {
700
+ // worksheet.appendChild(this.sheetProtection.exportXML(doc));
701
+ // }
702
+
703
+ // if (this.hyperlinks.length > 0) {
704
+ // const hyperlinksEl = doc.createElement('hyperlinks');
705
+ // const hyperlinks = this.hyperlinks;
706
+ // for (let i = 0, l = hyperlinks.length; i < l; i++) {
707
+ // const hyperlinkEl = doc.createElement('hyperlink');
708
+ // const hyperlink: any = hyperlinks[i];
709
+ // hyperlinkEl.setAttribute('ref', String(hyperlink.cell));
710
+ // hyperlink.id = Util.uniqueId('hyperlink');
711
+ // this.relations.addRelation(
712
+ // {
713
+ // id: hyperlink.id,
714
+ // target: hyperlink.location,
715
+ // targetMode: hyperlink.targetMode || 'External',
716
+ // },
717
+ // 'hyperlink',
718
+ // );
719
+ // hyperlinkEl.setAttribute('r:id', this.relations.getRelationshipId(hyperlink));
720
+ // hyperlinksEl.appendChild(hyperlinkEl);
721
+ // }
722
+ // worksheet.appendChild(hyperlinksEl);
723
+ // }
724
+
725
+ // if (this.mergedCells.length > 0) {
726
+ // const mergeCells = doc.createElement('mergeCells');
727
+ // for (let i = 0, l = this.mergedCells.length; i < l; i++) {
728
+ // const mergeCell = doc.createElement('mergeCell');
729
+ // mergeCell.setAttribute('ref', `${this.mergedCells[i][0]}:${this.mergedCells[i][1]}`);
730
+ // mergeCells.appendChild(mergeCell);
731
+ // }
732
+ // worksheet.appendChild(mergeCells);
733
+ // }
734
+
735
+ // this.exportPageSettings(doc, worksheet);
736
+
737
+ // if (this._headers.length > 0 || this._footers.length > 0) {
738
+ // const headerFooter = doc.createElement('headerFooter');
739
+ // if (this._headers.length > 0) {
740
+ // headerFooter.appendChild(this.exportHeader(doc));
741
+ // }
742
+ // if (this._footers.length > 0) {
743
+ // headerFooter.appendChild(this.exportFooter(doc));
744
+ // }
745
+ // worksheet.appendChild(headerFooter);
746
+ // }
747
+
748
+ // for (let i = 0, l = this._drawings.length; i < l; i++) {
749
+ // const drawing = doc.createElement('drawing');
750
+ // drawing.setAttribute('r:id', this.relations.getRelationshipId(this._drawings[i]));
751
+ // worksheet.appendChild(drawing);
752
+ // }
753
+
754
+ // if (this._tables.length > 0) {
755
+ // const tables = doc.createElement('tableParts');
756
+ // tables.setAttribute('count', this._tables.length);
757
+ // for (let i = 0, l = this._tables.length; i < l; i++) {
758
+ // const table = doc.createElement('tablePart');
759
+ // table.setAttribute('r:id', this.relations.getRelationshipId(this._tables[i]));
760
+ // tables.appendChild(table);
761
+ // }
762
+ // worksheet.appendChild(tables);
763
+ // }
764
+
765
+ // // Get everything after </sheetData>
766
+ // const xml = doc.toString();
767
+ // return xml.substring(xml.indexOf('</sheetData>') + '</sheetData>'.length);
768
+ return '' ;
769
+ }
770
+
771
+ /**
772
+ * Serialize a chunk of rows to XML (same logic as in toXML)
773
+ */
774
+ serializeRows ( rows : ( number | string | boolean | Date | null | ExcelColumnMetadata ) [ ] [ ] , startRow = 0 ) : string {
775
+ const columns = this . columns || [ ] ;
776
+ let xml = '' ;
777
+ for ( let row = 0 , l = rows . length ; row < l ; row ++ ) {
778
+ const dataRow = rows [ row ] ;
779
+ const cellCount = dataRow . length ;
780
+ let rowXml = `<row r="${ startRow + row + 1 } ">` ;
781
+ for ( let c = 0 ; c < cellCount ; c ++ ) {
782
+ let cellValue = dataRow [ c ] ;
783
+ let cellType : any = typeof cellValue ;
784
+ // Always treat first row as text
785
+ if ( startRow + row === 0 ) {
786
+ cellType = 'text' ;
787
+ }
788
+ let cellXml = '' ;
789
+ const rAttr = ` r="${ String . fromCharCode ( 65 + c ) } ${ startRow + row + 1 } "` ;
790
+ switch ( cellType ) {
791
+ case 'number' :
792
+ cellXml = `<c${ rAttr } ><v>${ cellValue } </v></c>` ;
793
+ break ;
794
+ case 'text' :
795
+ default : {
796
+ let id : number | undefined ;
797
+ if ( typeof this . sharedStrings ?. strings [ cellValue as string ] !== 'undefined' ) {
798
+ id = this . sharedStrings . strings [ cellValue as string ] ;
799
+ } else {
800
+ id = this . sharedStrings ?. addString ( cellValue as string ) ;
801
+ }
802
+ cellXml = `<c${ rAttr } t="s"><v>${ id } </v></c>` ;
803
+ break ;
804
+ }
805
+ }
806
+ rowXml += cellXml ;
807
+ }
808
+ rowXml += '</row>' ;
809
+ xml += rowXml ;
810
+ }
811
+ return xml ;
812
+ }
647
813
}
0 commit comments