簡體   English   中英

嘗試評估包含另一個計算單元格公式的單元格時出錯

[英]Error attempting to evaluate cell containing formula of another calculation cell

我有一個包含很多公式和幾個選項卡的電子表格。 選項卡之一是將數字輸入10個字段。 另一個選項卡用於查看計算公式的輸出。

使用Apache POI,我打開了電子表格並輸入了我的號碼。 當我嘗試評估電子表格時,問題就來了。 我試過了

FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
helper.createFormulaEvaluator();
evaluator.evaluateAll();

而且我得到一個錯誤(似乎沒人能回答): Unexpected arg eval type (org.apache.poi.ss.formula.eval.MissingArgEval)] with root cause

因此,我更改為單獨評估單元格,以便可以找出哪個單元格有錯誤,因此我的代碼如下所示:

FormulaEvaluator evaluator = this.workbook.getCreationHelper().createFormulaEvaluator();
for (Sheet sheet : this.workbook) {
  System.out.println("Evaluating next sheet");
  System.out.println(sheet.getSheetName());
  for (Row r : sheet) {
    System.out.println("Row Number:");
    System.out.println(r.getRowNum());
    for (Cell c : r) {
      if (c.getCellType() == Cell.CELL_TYPE_FORMULA) {
          System.out.println(c.getColumnIndex());
          try {
            evaluator.evaluateFormulaCell(c);
          } catch (Exception e) {
              rowArray.add(r.getRowNum());
              cellArray.add(c.getColumnIndex());
            System.out.println("Skipping failed cell");
          }
      }
    }
  }

我得到的錯誤與運行評估全部時的錯誤相同。 通過在其中進行一些調試,我發現錯誤來自單元格L3,該單元格包含公式: =D5 由於評估程序按row:column行,因此它先評估第3行的所有內容,然后再移至5,因此L3引用了一個尚未評估的字段,因此引發錯誤。

我嘗試捕獲錯誤並將行和單元格號存儲在數組中,然后在處理完工作表中的所有內容之后,嘗試重新處理未處理的單元格,但仍然得到相同的結果。 我有點困惑為什么重試不起作用。

重試代碼:

  // try to fix any failed evaluations here
  Iterator cellItr = cellArray.iterator();
  Iterator rowItr = rowArray.iterator();

  while (cellItr.hasNext()) {
    Integer cellElement = (int) cellItr.next();
    Integer rowElement = (int) rowItr.next();

      XSSFRow row = sheet.getRow(rowElement);
      XSSFCell cell = row.getCell(cellElement);

      System.out.println("Re-evaluating: " + rowElement + " : " + cellElement);
      evaluator.evaluateFormulaCell(cell);
  }
}

重試代碼給出了相同的結果。 我嘗試將原始評估程序更改為使用validateInCell將公式更改為實際數字,但這似乎無濟於事。

-----------------更新---------------------我剛剛意識到不建議使用EvaluateFormulaCellEnum 。 我將所有代碼放入一個函數中,並多次運行該函數,並意識到它正在一次又一次地評估所有單元格,因此我轉而使用validateInCell,發現它只對每個單元格進行一次評估,但仍然無法獲得通過提到的細胞。 這是我更新的代碼,我在運行5次的函數中擁有該代碼:

for (Sheet sheet : this.workbook) {
    System.out.println("Evaluating next sheet" + sheet.getSheetName());
  for (Row r : sheet) {
    for (Cell c : r) {
      if (c.getCellType() == Cell.CELL_TYPE_FORMULA) {
          System.out.println("Cell index: " + r.getRowNum() + " - " + c.getColumnIndex());
          try {
            evaluator.evaluateInCell(c);
          } catch (Exception e) {
              try {
                evaluator.evaluateFormulaCellEnum(c);
              } catch (Exception ee) {
                System.out.println("Skipping failed cell after 2 attempts");
              }
          }
      }
    }
  }

通過進行調試,我可以查看電子表格中哪些單元格失敗,因此我將失敗單元格中的公式保存在文本文檔中,並用其值替換了公式,然后重新編譯了代碼和電子表格。評價!

然后,我遍歷了所有單元格,然后將它們的公式兩個又兩個地放回去,直到再次破裂。 事實證明這是我已經知道的一個案例,但是在電子表格中搜索並不是一件容易的事。

這是有問題的公式: =ROUNDUP('HW page'!$H$53*'HW page'!$H$54,)我在最后一個參數中添加了0,因此它看起來像這樣: =ROUNDUP('HW assumptions'!$H$53*'HW assumptions'!$H$54,0) ,然后評估程序起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM