Skip to content

Commit

Permalink
Merge branch 'publish' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
FauconSpartiate authored Oct 25, 2023
2 parents ce94f8c + 944866f commit f8329e1
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ sheetObject.removeRow(80);
### Cell-Style Options

| key | description |
| ------------------ | --------------------------------------------------------------------------------------------------------------------------------------- |
|--------------------|-----------------------------------------------------------------------------------------------------------------------------------------|
| fontFamily | eg. getFontFamily(`FontFamily.Arial`) or getFontFamily(`FontFamily.Comic_Sans_MS`) `There is total 182 Font Families available for now` |
| fontSize | specify the font-size as integer eg. fontSize = 15 |
| bold | makes text bold - when set to `true`, by-default it is set to `false` |
Expand Down
235 changes: 213 additions & 22 deletions lib/src/save/save_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -761,34 +761,225 @@ class Save {
});
}

/// Writing the value of excel cells into the separate
/// sharedStrings file so as to minimize the size of excel files.
_setSharedStrings() {
var uniqueCount = 0;
var count = 0;
List<int>? _save() {
if (_excel._colorChanges) {
_processStylesFile();
}
_setSheetElements();
if (_excel._defaultSheet != null) {
_setDefaultSheet(_excel._defaultSheet);
}
_setSharedStrings();

if (_excel._mergeChanges) {
_setMerge();
}

XmlElement shareString = _excel
._xmlFiles['xl/${_excel._sharedStringsTarget}']!
.findAllElements('sst')
.first;
if (_excel._rtlChanges) {
_setRTL();
}

shareString.children.clear();
for (var xmlFile in _excel._xmlFiles.keys) {
var xml = _excel._xmlFiles[xmlFile].toString();
var content = utf8.encode(xml);
_archiveFiles[xmlFile] = ArchiveFile(xmlFile, content.length, content);
}
return ZipEncoder().encode(_cloneArchive(_excel._archive));
}

_excel._sharedStrings._map.forEach((string, ss) {
uniqueCount += 1;
count += ss.count;
_setColumnWidth(String sheetName) {
final sheetObject = _excel._sheetMap[sheetName];
if (sheetObject == null) return;

shareString.children.add(string.node);
});
var xmlFile = _excel._xmlFiles[_excel._xmlSheetId[sheetName]];
if (xmlFile == null) return;

final colElements = xmlFile.findAllElements('cols');

if (sheetObject.getColWidths.isEmpty &&
sheetObject.getColAutoFits.isEmpty) {
if (colElements.isEmpty) {
return;
}

final cols = colElements.first;
final worksheet = xmlFile.findAllElements('worksheet').first;
worksheet.children.remove(cols);
return;
}

if (colElements.isEmpty) {
final worksheet = xmlFile.findAllElements('worksheet').first;
final sheetData = xmlFile.findAllElements('sheetData').first;
final index = worksheet.children.indexOf(sheetData);

worksheet.children.insert(index, XmlElement(XmlName('cols'), [], []));
}

var cols = colElements.first;

if (cols.children.isNotEmpty) {
cols.children.clear();
}

final autoFits = sheetObject.getColAutoFits.asMap();
final customWidths = sheetObject.getColWidths.asMap();

final columnCount = max(autoFits.length, customWidths.length);

List<double> colWidths = <double>[];
int min = 0;

[
['count', '$count'],
['uniqueCount', '$uniqueCount']
].forEach((value) {
if (shareString.getAttributeNode(value[0]) == null) {
shareString.attributes.add(XmlAttribute(XmlName(value[0]), value[1]));
for (var index = 0; index < columnCount; index++) {
double value = _defaultColumnWidth;

if (autoFits.containsKey(index) &&
autoFits[index] == true &&
(!customWidths.containsKey(index) ||
customWidths[index] == _defaultColumnWidth)) {
value = _calcAutoFitColWidth(sheetObject, index);
} else {
shareString.getAttributeNode(value[0])!.value = value[1];
if (customWidths.containsKey(index)) {
value = customWidths[index]!;
}
}

colWidths.add(value);

if (index != 0 && colWidths[index - 1] != value) {
_addNewCol(cols, min, index - 1, colWidths[index - 1]);
min = index;
}

if (index == (columnCount - 1)) {
_addNewCol(cols, index, index, value);
}
}
}

bool _setDefaultSheet(String? sheetName) {
if (sheetName == null || _excel._xmlFiles['xl/workbook.xml'] == null) {
return false;
}
List<XmlElement> sheetList =
_excel._xmlFiles['xl/workbook.xml']!.findAllElements('sheet').toList();
XmlElement elementFound = XmlElement(XmlName(''));

int position = -1;
for (int i = 0; i < sheetList.length; i++) {
var _sheetName = sheetList[i].getAttribute('name');
if (_sheetName != null && _sheetName.toString() == sheetName) {
elementFound = sheetList[i];
position = i;
break;
}
}

if (position == -1) {
return false;
}
if (position == 0) {
return true;
}

_excel._xmlFiles['xl/workbook.xml']!
.findAllElements('sheets')
.first
.children
..removeAt(position)
..insert(0, elementFound);

String? expectedSheet = _excel._getDefaultSheet();

return expectedSheet == sheetName;
}

void _setHeaderFooter(String sheetName) {
final sheet = _excel._sheetMap[sheetName];
if (sheet == null) return;

final xmlFile = _excel._xmlFiles[_excel._xmlSheetId[sheetName]];
if (xmlFile == null) return;

final sheetXmlElement = xmlFile.findAllElements("worksheet").first;

final results = sheetXmlElement.findAllElements("headerFooter");
if (results.isNotEmpty) {
sheetXmlElement.children.remove(results.first);
}

if (sheet.headerFooter == null) return;

sheetXmlElement.children.add(sheet.headerFooter!.toXmlElement());
}

/// Writing the merged cells information into the excel properties files.
_setMerge() {
_selfCorrectSpanMap(_excel);
_excel._mergeChangeLook.forEach((s) {
if (_excel._sheetMap[s] != null &&
_excel._sheetMap[s]!._spanList.isNotEmpty &&
_excel._xmlSheetId.containsKey(s) &&
_excel._xmlFiles.containsKey(_excel._xmlSheetId[s])) {
Iterable<XmlElement>? iterMergeElement = _excel
._xmlFiles[_excel._xmlSheetId[s]]
?.findAllElements('mergeCells');
late XmlElement mergeElement;
if (iterMergeElement?.isNotEmpty ?? false) {
mergeElement = iterMergeElement!.first;
} else {
if ((_excel._xmlFiles[_excel._xmlSheetId[s]]
?.findAllElements('worksheet')
.length ??
0) >
0) {
int index = _excel._xmlFiles[_excel._xmlSheetId[s]]!
.findAllElements('worksheet')
.first
.children
.indexOf(_excel._xmlFiles[_excel._xmlSheetId[s]]!
.findAllElements("sheetData")
.first);
if (index == -1) {
_damagedExcel();
}
_excel._xmlFiles[_excel._xmlSheetId[s]]!
.findAllElements('worksheet')
.first
.children
.insert(
index + 1,
XmlElement(XmlName('mergeCells'),
[XmlAttribute(XmlName('count'), '0')]));

mergeElement = _excel._xmlFiles[_excel._xmlSheetId[s]]!
.findAllElements('mergeCells')
.first;
} else {
_damagedExcel();
}
}

List<String> _spannedItems =
List<String>.from(_excel._sheetMap[s]!.spannedItems);

[
['count', _spannedItems.length.toString()],
].forEach((value) {
if (mergeElement.getAttributeNode(value[0]) == null) {
mergeElement.attributes
.add(XmlAttribute(XmlName(value[0]), value[1]));
} else {
mergeElement.getAttributeNode(value[0])!.value = value[1];
}
});

mergeElement.children.clear();

_spannedItems.forEach((value) {
mergeElement.children.add(XmlElement(XmlName('mergeCell'),
[XmlAttribute(XmlName('ref'), '$value')], []));
});
}
});
}
Expand Down

0 comments on commit f8329e1

Please sign in to comment.