public void Cleanup(ISheet sheet) { for (var r = sheet.FirstRowNum; r <= sheet.LastRowNum; r++) { var row = sheet.GetRow(r); if (row == null) { continue; } foreach (var cell in row.Cells) { var cellValue = GetStringCellValue(cell); var cellOriginalValue = cellValue; if (LiquidHelper.ContainsLiquidTag(cellValue)) { // TODO: This leaves empty rows sheet.RemoveRow(row); // check only first cell break; } foreach (var liquidObject in LiquidHelper.GetLiquidObjects(cellValue)) { cellValue = cellValue.Replace(liquidObject, string.Empty); } if (cellValue != cellOriginalValue) { cell.SetCellValue(cellValue); } } } }
public void EvaluateObjects(ISheet sheet) { for (var r = sheet.FirstRowNum; r <= sheet.LastRowNum; r++) { var row = sheet.GetRow(r); if (row == null) { continue; } foreach (var cell in row.Cells) { var cellValue = GetStringCellValue(cell); var cellOriginalValue = cellValue; foreach (var liquidObject in LiquidHelper.GetLiquidObjects(cellValue)) { var value = Binder.Eval(LiquidHelper.UnwrapLiquidObject(liquidObject)); if (value != null) { cellValue = cellValue.Replace(liquidObject, value); } } if (cellValue != cellOriginalValue) { cell.SetCellValue(cellValue); } } } }
public void EvaluateLoops(ISheet sheet) { for (var r = sheet.FirstRowNum; r <= sheet.LastRowNum; r++) { var row = sheet.GetRow(r); if (row == null) { continue; } var rowIndex = 0; foreach (var cell in row.Cells) { // skip affected rows if (rowIndex > row.RowNum) { continue; } var cellValue = GetStringCellValue(cell); if (LiquidHelper.ContainsLiquidTag(cellValue) && Regex.IsMatch(cellValue, @"{%\s*for")) { var loop = LiquidLoop.Parse(LiquidHelper.UnwrapLiquidTag(cellValue)); if (loop == null) { continue; } loop.NumOfRepeats = Binder.Count(loop.CollectionName); rowIndex = row.RowNum + 1; while (loop.Next()) { WorkbookProvider.DuplicateRow(sheet, rowIndex); EvaluateRow(sheet.GetRow(rowIndex), loop); rowIndex++; } // check only first cell break; } } } }
public void EvaluateRow(IRow row, LiquidLoop loop) { foreach (var cell in row.Cells) { var cellValue = GetStringCellValue(cell); var cellOriginalValue = cellValue; foreach (var liquidObject in LiquidHelper.GetLiquidObjects(cellValue)) { var objectName = LiquidHelper.UnwrapLiquidObject(liquidObject); // strip loop variable name objectName = Regex.Replace(objectName, @"^" + loop.VariableName + @"\.", ""); var value = Binder.Eval(objectName, loop.CollectionName, loop.Index); if (value != null) { cellValue = cellValue.Replace(liquidObject, value); } } if (cellValue != cellOriginalValue) { cell.SetCellValue(cellValue); } } }