Skip to content

Commit

Permalink
Merge pull request #14 from kawal7415/findandreplace-appendrow
Browse files Browse the repository at this point in the history
[feature] should solve most of #13
  • Loading branch information
justraman authored May 15, 2020
2 parents 520d803 + 2285d74 commit 2c52b07
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
25 changes: 14 additions & 11 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 = "/Users/kawal/Desktop/excel2.xlsx";
var file = "/home/raman/Documents/excel/example/test.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 Down Expand Up @@ -46,16 +46,19 @@ void main(List<String> args) {
sheet, CellIndex.indexByString("A5"), CellIndex.indexByString("E5"));

// Remove row at index = 17
excel.removeRow(sheet, 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 = 17;
excel.insertColumn(sheet, 2);
// // Insert column at index = 17;
// 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"]);
excel.findAndReplace(sheet, 'bustin', 'raman');

excel.setDefaultSheet(sheet).then((isSet) {
// isSet is bool which tells that whether the setting of default sheet is successful or not.
Expand Down Expand Up @@ -84,7 +87,7 @@ void main(List<String> args) {

// Saving the file

String outputFile = "/Users/kawal/Desktop/excel2copy.xlsx";
String outputFile = "/home/raman/Documents/excel/example/output.xlsx";
excel.encode().then((onValue) {
File(join(outputFile))
..createSync(recursive: true)
Expand Down
Binary file added example/output.xlsx
Binary file not shown.
Binary file added example/test.xlsx
Binary file not shown.
35 changes: 35 additions & 0 deletions lib/src/excel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,41 @@ abstract class Excel {
}
}

/// append row
appendRow(String sheetName, List<dynamic> data) {
_checkSheetArguments(sheetName);
var targetRow = _tables[sheetName].maxRows;
data.asMap().forEach((index, value) => updateCell(
sheetName,
CellIndex.indexByColumnRow(columnIndex: index, rowIndex: targetRow),
value));
}

/// find and replace
int findAndReplace(String sheetName, dynamic source, String target) {
_checkSheetArguments(sheetName);
var replaceCount = 0;
var rowIndex = 0;
for (var row in _tables[sheetName].rows) {
var sourceRegx = RegExp(source.toString());
if (source.runtimeType == RegExp) {
sourceRegx == source;
}
row.asMap().forEach((columnIndex, value) {
if (value != null && sourceRegx.hasMatch(value)) {
updateCell(
sheetName,
CellIndex.indexByColumnRow(
columnIndex: columnIndex, rowIndex: rowIndex),
value.toString().replaceAll(sourceRegx, target.toString()));
replaceCount += 1;
}
});
rowIndex += 1;
}
return replaceCount;
}

/// Remove row in [sheet] at position [rowIndex]
removeRow(String sheet, int rowIndex) {
if (!_sheets.containsKey(sheet) || rowIndex >= _tables[sheet]._maxRows) {
Expand Down

0 comments on commit 2c52b07

Please sign in to comment.