Skip to content

Commit

Permalink
Fix #13
Browse files Browse the repository at this point in the history
  • Loading branch information
justkawal committed May 17, 2020
1 parent 4bb07cd commit 1b29cc4
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 44 deletions.
3 changes: 1 addition & 2 deletions .github/no-response.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ responseRequiredLabel: "needs info"
closeComment: >-
Without additional information, we are unfortunately not sure how to
resolve this issue. We are therefore reluctantly going to close this
bug for now. Please don't hesitate to comment on the bug if you have
issue for now. Please don't hesitate to comment on the issue if you have
any more information for us; we will reopen it right away!
Thanks for your contribution.
35 changes: 22 additions & 13 deletions example/excel_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import 'package:path/path.dart';
import 'package:excel/excel.dart';

void main(List<String> args) {
var file = "/home/raman/Documents/excel/example/test.xlsx";
var file = "/Users/kawal/Desktop/excel2.xlsx";
var bytes = File(file).readAsBytesSync();
//var excel = Excel.createExcel();
var excel = Excel.createExcel();
// or
var excel = Excel.decodeBytes(bytes, update: true);
//var excel = Excel.decodeBytes(bytes, update: true);
for (var table in excel.tables.keys) {
print(table);
print(excel.tables[table].maxCols);
Expand All @@ -21,7 +21,9 @@ void main(List<String> args) {
var sheet = 'Sheet24';

excel.updateCell(sheet, CellIndex.indexByString("A1"), "Here Value of A1",
backgroundColorHex: "#1AFF1A", horizontalAlign: HorizontalAlign.Center);
backgroundColorHex: "#1AFF1A",
horizontalAlign: HorizontalAlign.Center,
verticalAlign: VerticalAlign.Center);

excel.updateCell(
sheet,
Expand All @@ -45,17 +47,17 @@ void main(List<String> args) {
excel.merge(
sheet, CellIndex.indexByString("A5"), CellIndex.indexByString("E5"));

// Remove row at index = 2
// excel.removeRow(sheet, 2);
//Remove row at index = 2
excel.removeRow(sheet, 2);

// // Remove column at index = 2
// excel.removeColumn(sheet, 2);
// Remove column at index = 2
excel.removeColumn(sheet, 2);

// // Insert column at index = 2;
// excel.insertColumn(sheet, 2);
// Insert column at index = 2;
excel.insertColumn(sheet, 2);

// // Insert row at index = 2;
// excel.insertRow(sheet, 2);
// Insert row at index = 2;
excel.insertRow(sheet, 2);

excel.appendRow(sheet, ["bustin", "jiebr"]);

Expand All @@ -75,6 +77,13 @@ void main(List<String> args) {
print("Default Sheet:" + value.toString());
});

excel.insertRowIterables(sheet, ["A", "B", "C", "D", "E", "F", "G", "H"], 2,
startingColumn: 3, overwriteMergedCells: false);

excel.insertRowIterables(
sheet, ["Insert", "ing", "in", "9th", "row", "as", "iterables"], 8,
startingColumn: 13);

// Check which cells are merged
List<String> mergedCells = excel.getMergedCells(sheet);
mergedCells.forEach((cells) {
Expand All @@ -89,7 +98,7 @@ void main(List<String> args) {

// Saving the file

String outputFile = "/home/raman/Documents/excel/example/output.xlsx";
String outputFile = "/Users/kawal/Desktop/excel_example.xlsx";
excel.encode().then((onValue) {
File(join(outputFile))
..createSync(recursive: true)
Expand Down
1 change: 1 addition & 0 deletions lib/excel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:archive/archive.dart';
import 'package:xml/xml.dart';

part 'src/excel.dart';
part 'src/utility.dart';
part 'src/xlsx.dart';
part 'src/cell_index.dart';
part 'src/data_table.dart';
Expand Down
198 changes: 169 additions & 29 deletions lib/src/excel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1094,60 +1094,192 @@ abstract class Excel {
}
}

/// Append [row]
/// Append [row] iterables just post the last filled index in the [sheetName]
appendRow(String sheetName, List<dynamic> row) {
int targetRow = _tables[sheetName].maxRows;
insertRowIterables(sheetName, row, targetRow);
}

/// getting the List of _Span Objects which have the rowIndex containing and
/// also lower the range by giving the starting columnIndex
List<_Span> _getSpannedObjects(
String sheetName, int rowIndex, int startingColumnIndex) {
List<_Span> obtained;

if (_isContain(_spanMap) && _isContain(_spanMap[sheetName])) {
obtained = List<_Span>();
_spanMap[sheetName].forEach((spanObject) {
if (spanObject != null &&
spanObject.rowSpanStart <= rowIndex &&
rowIndex <= spanObject.rowSpanEnd &&
startingColumnIndex <= spanObject.columnSpanEnd) {
obtained.add(spanObject);
}
});
}
return obtained;
}

/// Checking if the columnIndex and the rowIndex passed is inside ?
/// the spanObjectList which is got from above function
bool _isInsideSpanObject(
List<_Span> spanObjectList, int columnIndex, int rowIndex) {
for (int i = 0; i < spanObjectList.length; i++) {
_Span spanObject = spanObjectList[i];

if (spanObject != null &&
spanObject.columnSpanStart <= columnIndex &&
columnIndex <= spanObject.columnSpanEnd &&
spanObject.rowSpanStart <= rowIndex &&
rowIndex <= spanObject.rowSpanEnd) {
if (columnIndex < spanObject.columnSpanEnd) {
return false;
} else if (columnIndex == spanObject.columnSpanEnd) {
return true;
}
}
}
return true;
}

/// Helps to add the [row] iterables in the given row = [rowIndex] in [sheetName]
///
/// [startingColumn] tells from where we should start puttin the [row] iterables
///
/// [overwriteMergedCells] when set to [true] tells it to consider overwriting mergedCell
/// [overwriteMergedCells] when set to [false] puts the cell value to next unique cell.
///
insertRowIterables(String sheetName, List<dynamic> row, int rowIndex,
{CellIndex startingColumn}) {
if (row == null || rowIndex == null) {
{int startingColumn = 0, bool overwriteMergedCells = true}) {
if (row == null || rowIndex == null || row.length == 0) {
return;
}
_checkSheetArguments(sheetName);
_checkSheetMaxRow(sheetName, rowIndex);
int columnIndex = 0;
if (startingColumn != null) {
columnIndex = startingColumn.columnIndex;
if (startingColumn > 0) {
columnIndex = startingColumn;
}
_checkSheetMaxCol(sheetName, columnIndex + row.length);
row.asMap().forEach((index, value) => updateCell(
sheetName,
CellIndex.indexByColumnRow(columnIndex: index, rowIndex: rowIndex),
value));
int rowsLength = _tables[sheetName].maxRows,
maxIterationIndex = row.length - 1,
currentRowPosition = 0; // position in [row] iterables

if (overwriteMergedCells || rowIndex >= rowsLength) {
// Normally iterating and putting the data present in the [row] as we are on the last index.

while (currentRowPosition <= maxIterationIndex) {
updateCell(
sheetName,
CellIndex.indexByColumnRow(
columnIndex: columnIndex, rowIndex: rowIndex),
row[currentRowPosition]);
currentRowPosition++;
columnIndex++;
}
} else {
// expensive function as per time complexity
_selfCorrectSpanMap();
List<_Span> _spanObjectsList =
_getSpannedObjects(sheetName, rowIndex, columnIndex);

if (_spanObjectsList == null || _spanObjectsList.length <= 0) {
while (currentRowPosition <= maxIterationIndex) {
updateCell(
sheetName,
CellIndex.indexByColumnRow(
columnIndex: columnIndex, rowIndex: rowIndex),
row[currentRowPosition]);
currentRowPosition++;
columnIndex++;
}
} else {
while (currentRowPosition <= maxIterationIndex) {
if (_isInsideSpanObject(_spanObjectsList, columnIndex, rowIndex)) {
updateCell(
sheetName,
CellIndex.indexByColumnRow(
columnIndex: columnIndex, rowIndex: rowIndex),
row[currentRowPosition]);
currentRowPosition++;
}
columnIndex++;
}
}
}
}

/// Replace the [source] with [target]
/// Returns the [count] of replaced [source] with [target]
///
/// Yipee [source] is dynamic which allows you to pass your custom [RegExp]
///
/// Here [source] can also be a user's custom [RegExp]
/// optional argument [first] is used to replace the number of [first] earlier occurrences
///
/// optional argument [first] can be used to replace count of source occuring first
/// Example: If [first] is set to [3] then it will replace only first 3 occurrences of the [source] with [target].
///
/// If [first] is set to [3] then it will replace only first 3 occurences of the source
int findAndReplace(String sheetName, dynamic source, String target,
{int first = -1}) {
/// Other [options] are used to narrow down the starting and ending ranges of cells.
int findAndReplace(String sheetName, dynamic source, dynamic target,
{int first = -1,
int startingRow = -1,
int endingRow = -1,
int startingColumn = -1,
int endingColumn = -1}) {
_checkSheetArguments(sheetName);
int replaceCount = 0, rowIndex = 0;
for (var row in _tables[sheetName].rows) {
RegExp sourceRegx = RegExp(source.toString());
if (source.runtimeType == RegExp) {
sourceRegx == source;
int replaceCount = 0,
_startingRow = 0,
_endingRow = -1,
_startingColumn = 0,
_endingColumn = -1;

if (startingRow != -1 && endingRow != -1) {
if (startingRow > endingRow) {
_endingRow = startingRow;
_startingRow = endingRow;
} else {
_endingRow = endingRow;
_startingRow = startingRow;
}
row.asMap().forEach((columnIndex, value) {
}

if (startingColumn != -1 && endingColumn != -1) {
if (startingColumn > endingColumn) {
_endingColumn = startingColumn;
_startingColumn = endingColumn;
} else {
_endingColumn = endingColumn;
_startingColumn = startingColumn;
}
}

int rowsLength = _tables[sheetName].maxRows,
columnLength = _tables[sheetName].maxCols;
RegExp sourceRegx;
if (source.runtimeType == RegExp) {
sourceRegx == source;
} else {
sourceRegx = RegExp(source.toString());
}

for (int i = _startingRow; i < rowsLength; i++) {
if (_endingRow != -1 && i > _endingRow) {
break;
}
for (int j = _startingColumn; j < columnLength; j++) {
if (_endingColumn != -1 && j > _endingColumn) {
break;
}
var value = _tables[sheetName].rows[i][j];
if (value != null &&
sourceRegx.hasMatch(value) &&
(first == -1 || first != replaceCount)) {
updateCell(
sheetName,
CellIndex.indexByColumnRow(
columnIndex: columnIndex, rowIndex: rowIndex),
value.toString().replaceAll(sourceRegx, target.toString()));
_tables[sheetName].rows[i][j] =
value.toString().replaceAll(sourceRegx, target.toString());

replaceCount += 1;
}
});
rowIndex += 1;
}
}

return replaceCount;
}

Expand Down Expand Up @@ -1521,6 +1653,14 @@ abstract class Excel {
return [startColumn, startRow, endColumn, endRow];
}

String getColumnAlphabet(int collIndex) {
return '${numericToLetters(collIndex + 1)}';
}

int getColumnIndex(String columnAlphabet) {
return cellCoordsFromCellId('${columnAlphabet}2')[1];
}

String getCellId(int colI, int rowI) =>
'${numericToLetters(colI + 1)}${rowI + 1}';

Expand All @@ -1531,7 +1671,7 @@ abstract class Excel {

/// returns an Iterable of cell-Id for the previously merged cell-Ids.
Iterable<String> getMergedCells(String sheet) {
return _spannedItems != null && _spannedItems.containsKey(sheet)
return _spannedItems != null && _isContain(_spannedItems[sheet])
? List<String>.of(_spannedItems[sheet])
: [];
}
Expand Down
5 changes: 5 additions & 0 deletions lib/src/utility.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
part of excel;

bool _isContain(dynamic d) {
return (d ?? null) != null;
}

0 comments on commit 1b29cc4

Please sign in to comment.