parseExcel function

Translation parseExcel({
  1. required String filename,
  2. required String sheetname,
  3. required String placeholderSheetname,
  4. int headerRow = _kRowHeader,
  5. int valueRow = _kRowValue,
})

Reads Excel sheet.

Uses arb_sheet -n path/to/file to create a translation file from the template.

Implementation

Translation parseExcel({
  required String filename,
  required String sheetname,
  required String placeholderSheetname,
  int headerRow = _kRowHeader,
  int valueRow = _kRowValue,
}) {
  final Uint8List buf = File(filename).readAsBytesSync();
  final Excel excel = Excel.decodeBytes(buf);
  final Sheet? sheet = excel.sheets[sheetname];
  if (sheet == null) {
    return const Translation();
  }

  final List<ARBItem> items = <ARBItem>[];
  final List<List<Data?>> sheetRows = sheet.rows;
  final List<Data?> columns = sheetRows[headerRow];
  for (int i = valueRow; i < sheetRows.length; i++) {
    final List<Data?> row = sheetRows[i];
    final String name = row[_kColName]?.value?.toString() ?? '';
    if (name.trim().isEmpty) continue;

    final String? description = row[_kColDescription]?.value?.toString();
    final String? placeholders = row[_kColPlaceholders]?.value?.toString();
    final Map<String, String> translations = <String, String>{};
    final ARBItem item = ARBItem(
      name: name,
      description: description,
      placeholders: placeholders,
      translations: translations,
    );

    for (int i = _kColValue; i < sheet.maxCols; i++) {
      final String lang = columns[i]?.value?.toString() ?? '';
      translations[lang] = row[i]?.value?.toString() ?? '';
    }

    items.add(item);
  }

  final List<String> languages = columns
      .where((Data? e) => e != null && e.colIndex >= _kColValue)
      .map<String>((Data? e) => e?.value?.toString() ?? '')
      .toList();
  final PredefinedPlaceholderTable table = getPredefinedPlaceholders(
    placeholderSheetname: placeholderSheetname,
    excel: excel,
  );

  return Translation(
    languages: languages,
    items: items,
    predefinedPlaceholderTable: table,
  );
}