diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f5885a..668dbe6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,6 @@ All notable changes to this project will be documented in this file. ## [Future Updates coming soon] -- Find and Replace -- Add row / column from Iterables - Formulas - Font Family - Text Size @@ -11,6 +9,11 @@ All notable changes to this project will be documented in this file. - Underline - Bold +## [1.0.6] - 2020-05-16 +### Added Functionality +- Find and Replace +- Add row / column from Iterables + ## [1.0.5] - 2020-05-15 ### Removed - Bugs related to Spanning diff --git a/example/excel_example.dart b/example/excel_example.dart index 172b359..da978eb 100644 --- a/example/excel_example.dart +++ b/example/excel_example.dart @@ -58,7 +58,9 @@ void main(List args) { // excel.insertRow(sheet, 2); excel.appendRow(sheet, ["bustin", "jiebr"]); - excel.findAndReplace(sheet, 'bustin', 'raman'); + + int replacedCount = excel.findAndReplace(sheet, 'bustin', 'raman'); + print("Replaced Count:" + replacedCount.toString()); excel.setDefaultSheet(sheet).then((isSet) { // isSet is bool which tells that whether the setting of default sheet is successful or not. diff --git a/lib/src/excel.dart b/lib/src/excel.dart index d120c44..9e11f10 100644 --- a/lib/src/excel.dart +++ b/lib/src/excel.dart @@ -1094,28 +1094,50 @@ abstract class Excel { } } - /// append row - appendRow(String sheetName, List data) { + /// Append [row] + appendRow(String sheetName, List row) { + int targetRow = _tables[sheetName].maxRows; + insertRowIterables(sheetName, row, targetRow); + } + + insertRowIterables(String sheetName, List row, int rowIndex, + {CellIndex startingColumn}) { + if (row == null || rowIndex == null) { + return; + } _checkSheetArguments(sheetName); - var targetRow = _tables[sheetName].maxRows; - data.asMap().forEach((index, value) => updateCell( + _checkSheetMaxRow(sheetName, rowIndex); + int columnIndex = 0; + if (startingColumn != null) { + columnIndex = startingColumn.columnIndex; + } + _checkSheetMaxCol(sheetName, columnIndex + row.length); + row.asMap().forEach((index, value) => updateCell( sheetName, - CellIndex.indexByColumnRow(columnIndex: index, rowIndex: targetRow), + CellIndex.indexByColumnRow(columnIndex: index, rowIndex: rowIndex), value)); } - /// find and replace - int findAndReplace(String sheetName, dynamic source, String target) { + /// Replace the [source] with [target] + /// + /// Here [source] can also be a user's custom [RegExp] + /// + /// optional argument [first] can be used to replace count of source occuring first + /// + /// 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}) { _checkSheetArguments(sheetName); - var replaceCount = 0; - var rowIndex = 0; + int replaceCount = 0, rowIndex = 0; for (var row in _tables[sheetName].rows) { - var sourceRegx = RegExp(source.toString()); + RegExp sourceRegx = RegExp(source.toString()); if (source.runtimeType == RegExp) { sourceRegx == source; } row.asMap().forEach((columnIndex, value) { - if (value != null && sourceRegx.hasMatch(value)) { + if (value != null && + sourceRegx.hasMatch(value) && + (first == -1 || first != replaceCount)) { updateCell( sheetName, CellIndex.indexByColumnRow(