getRangeByName method
Get range by Name.
Workbook workbook = Workbook();
Worksheet sheet = workbook.worksheets[0];
sheet.getRangeByName('A1:D4');
List<int> bytes = workbook.saveAsStream();
File('Range.xlsx').writeAsBytes(bytes);
workbook.dispose();
Implementation
Range getRangeByName(String cellReference) {
if (cellReference == '') {
throw Exception('cellReference should not be null');
} else if (cellReference.length < 2) {
throw Exception('cellReference cannot be less then 2 symbols');
} else if (cellReference.isEmpty) {
final Error error =
ArgumentError('cellReference - Value cannot be empty.');
throw error;
}
final List<String> cells = cellReference.split(':');
int firstRow = 0;
int lastRow = 0;
int firstColumn = 0;
int lastColumn = 0;
// ignore: prefer_final_locals
for (int i = 0, len = cells.length; i < len; i++) {
final String cellReference = cells[i];
int iLetterStart = -1;
int iLetterCount = 0;
int iDigitStart = -1;
// ignore: prefer_final_locals
for (int j = 0, len = cellReference.length; j < len; j++) {
final String ch = cellReference[j];
if (_isNumeric(ch)) {
if (iDigitStart < 0) {
iDigitStart = j;
}
} else if (!_isNumeric(ch)) {
if (iLetterStart < 0) {
iLetterStart = j;
}
iLetterCount++;
}
}
if (iDigitStart == -1) {
iDigitStart = 1;
}
if (iLetterStart == -1) {
iLetterStart = 0;
}
if (cellReference.length < 2) {
final Error error = ArgumentError(
'cellReference - name cannot be less then 2 symbols.');
throw error;
}
final String strNumber = cellReference.substring(iDigitStart);
final String strAlpha =
cellReference.substring(iLetterStart, iLetterCount);
if (i == 0) {
firstRow = lastRow = int.parse(strNumber);
firstColumn = lastColumn = getColumnIndex(strAlpha);
} else if (i == 1) {
lastRow = int.parse(strNumber);
lastColumn = getColumnIndex(strAlpha);
}
}
return getRangeByIndex(firstRow, firstColumn, lastRow, lastColumn);
}