删除工作表中的空白列和行时更新其他工作表中的引用

删除工作表中的空白列和行时更新其他工作表中的引用

请参阅以下示例代码及其控制台输出。第二个工作表中的单元格 E3 具有一个公式 =Sheet1!C3,它引用了第一个工作表中的单元格 C3。如果您将 DeleteOptions.UpdateReference 属性设置为 true,此公式将会被更新为 =Sheet1!A1,当删除第一个工作表中的空白列和行时。然而,如果您将 DeleteOptions.UpdateReference 属性设置为 false,第二个工作表中单元格 E3 的公式将保持为 =Sheet1!C3 并变为无效。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(Updatereferencesinotherworksheetswhiledeletingblankcolumnsandrowsinworksheet.class);
//Create workbook
Workbook wb = new Workbook();
//Add second sheet with name Sheet2
wb.getWorksheets().add("Sheet2");
//Access first sheet and add some integer value in cell C1
//Also add some value in any cell to increase the number of blank rows and columns
Worksheet sht1 = wb.getWorksheets().get(0);
sht1.getCells().get("C1").putValue(4);
sht1.getCells().get("K30").putValue(4);
//Access second sheet and add formula in cell E3 which refers to cell C1 in first sheet
Worksheet sht2 = wb.getWorksheets().get(1);
sht2.getCells().get("E3").setFormula("'Sheet1'!C1");
//Calculate formulas of workbook
wb.calculateFormula();
//Print the formula and value of cell E3 in second sheet before deleting blank columns and rows in Sheet1.
System.out.println("Cell E3 before deleting blank columns and rows in Sheet1.");
System.out.println("--------------------------------------------------------");
System.out.println("Cell Formula: " + sht2.getCells().get("E3").getFormula());
System.out.println("Cell Value: " + sht2.getCells().get("E3").getStringValue());
//If you comment DeleteOptions.UpdateReference property below, then the formula in cell E3 in second sheet will not be updated
DeleteOptions opts = new DeleteOptions();
//opts.setUpdateReference(true);
//Delete all blank rows and columns with delete options
sht1.getCells().deleteBlankColumns(opts);
sht1.getCells().deleteBlankRows(opts);
//Calculate formulas of workbook
wb.calculateFormula();
//Print the formula and value of cell E3 in second sheet after deleting blank columns and rows in Sheet1.
System.out.println("");
System.out.println("");
System.out.println("Cell E3 after deleting blank columns and rows in Sheet1.");
System.out.println("--------------------------------------------------------");
System.out.println("Cell Formula: " + sht2.getCells().get("E3").getFormula());
System.out.println("Cell Value: " + sht2.getCells().get("E3").getStringValue());

控制台输出

这是以上示例代码的控制台输出,当 DeleteOptions.UpdateReference 属性被设置为 true 时。

 Cell E3 before deleting blank columns and rows in Sheet1.

\--------------------------------------------------------

Cell Formula: =Sheet1!C1

Cell Value: 4


Cell E3 after deleting blank columns and rows in Sheet1.

\--------------------------------------------------------

Cell Formula: =Sheet1!A1

Cell Value: 4

这是以上示例代码的控制台输出,当 DeleteOptions.UpdateReference 属性被设置为 false 时。如您所见,第二个工作表中单元格 E3 的公式没有被更新,其单元格值现在为 0 而非无效。

 Cell E3 before deleting blank columns and rows in Sheet1.

\--------------------------------------------------------

Cell Formula: =Sheet1!C1

Cell Value: 4


Cell E3 after deleting blank columns and rows in Sheet1.

\--------------------------------------------------------

Cell Formula: =Sheet1!C1

Cell Value: 0