<commit_before>package charts.builder.spreadsheet;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.Color;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFColor;

import charts.builder.DataSource;
import charts.builder.Value;

import com.google.common.collect.Lists;

public abstract class SpreadsheetDataSource implements DataSource {

  private Workbook workbook;

  private FormulaEvaluator evaluator;
  
  private final int defaultSheet;

  private static class SpreadsheetCellValue implements Value {

    private final Cell cell;

    private final FormulaEvaluator evaluator;

    public SpreadsheetCellValue(Cell cell, FormulaEvaluator evaluator) {
      this.cell = cell;
      this.evaluator = evaluator;
    }

    @Override
    public String getValue() {
      String result;
      CellValue cellValue = evaluator.evaluate(cell);
      if (cellValue == null) {
        return "";
      }
      switch (cellValue.getCellType()) {
      case Cell.CELL_TYPE_BOOLEAN:
        result = Boolean.toString(cellValue.getBooleanValue());
        break;
      case Cell.CELL_TYPE_NUMERIC:
        double val = cellValue.getNumberValue();
        result = Double.toString(val);
        break;
      case Cell.CELL_TYPE_STRING:
        result = cellValue.getStringValue();
        break;
      case Cell.CELL_TYPE_BLANK:
        result = "";
        break;
      case Cell.CELL_TYPE_ERROR:
        result = ErrorEval.getText(cellValue.getErrorValue());
        break;
      // CELL_TYPE_FORMULA will never happen
      case Cell.CELL_TYPE_FORMULA:
        result = "#FORMULAR";
        break;
      default:
        result = "#DEFAULT";
      }
      return result;<commit_msg>fix: if the cell can not be evaluated then try to return the cached value. this is particularly useful with erternal refs in xlsx files as they are currently not implemted in poi<commit_after>package charts.builder.spreadsheet;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.Color;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.FormulaError;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFColor;

import charts.builder.DataSource;
import charts.builder.Value;

import com.google.common.collect.Lists;

public abstract class SpreadsheetDataSource implements DataSource {

  private Workbook workbook;

  private FormulaEvaluator evaluator;
  
  private final int defaultSheet;

  private static class SpreadsheetCellValue implements Value {

    private final Cell cell;

    private final FormulaEvaluator evaluator;

    public SpreadsheetCellValue(Cell cell, FormulaEvaluator evaluator) {
      this.cell = cell;
      this.evaluator = evaluator;
    }

    @Override
    public String getValue() {
      String result = "";
      try {
        CellValue cellValue = evaluator.evaluate(cell);
        if (cellValue == null) {
          return "";
        }
        switch (cellValue.getCellType()) {
        case Cell.CELL_TYPE_BOOLEAN:
          result = Boolean.toString(cellValue.getBooleanValue());
          break;
        case Cell.CELL_TYPE_NUMERIC:
          double val = cellValue.getNumberValue();
          result = Double.toString(val);
          break;
        case Cell.CELL_TYPE_STRING:
          result = cellValue.getStringValue();
          break;
        case Cell.CELL_TYPE_BLANK:
          result = "";
          break;
        case Cell.CELL_TYPE_ERROR:
          result = ErrorEval.getText(cellValue.getErrorValue());
          break;
        // CELL_TYPE_FORMULA will never happen
        case Cell.CELL_TYPE_FORMULA:
          result = "#FORMULAR";
          break;
        default:
          result = "#DEFAULT";
        }
      } catch(RuntimeException e) {
        if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
          switch(cell.getCachedFormulaResultType()) {
            case Cell.CELL_TYPE_NUMERIC: 
              double val = cell.getNumericCellValue();
              result = Double.toString(val);
              break;
            case Cell.CELL_TYPE_ERROR:
              FormulaError fe = FormulaError.forInt(cell.getErrorCellValue());
              result = fe.getString();
              break;
            case Cell.CELL_TYPE_STRING:
              result = cell.getStringCellValue();
              break;
            case Cell.CELL_TYPE_BOOLEAN:
              result = Boolean.toString(cell.getBooleanCellValue());
              break;
            default:
              result = "";
          }
        }
      }
      return result;
{'fraction_non_alphanumeric': 0.09289220141792401, 'fraction_numerical': 0.00036357025995273584, 'mean_word_length': 2.7175675675675675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1214566', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1638, 'n_words': 333}