Actualizar referencias en otras hojas de cálculo al eliminar columnas y filas en blanco en una hoja de cálculo

Actualizar referencias en otras hojas de cálculo al eliminar columnas y filas en blanco en una hoja de cálculo

Por favor, consulte el siguiente código de ejemplo y su salida por consola. La celda E3 en la segunda hoja de cálculo tiene una fórmula =Sheet1!C3 que se refiere a la celda C3 en la primera hoja de cálculo. Si configura la propiedad DeleteOptions.UpdateReference como true, esta fórmula se actualizará y se convertirá en =Sheet1!A1 al eliminar columnas y filas en blanco en la primera hoja de cálculo. Sin embargo, si configura la propiedad DeleteOptions.UpdateReference como false, la fórmula en la celda E3 de la segunda hoja de cálculo seguirá siendo =Sheet1!C3 y será inválida.

// 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());

Salida de la consola

Esta es la salida por consola del código de ejemplo anterior cuando la propiedad DeleteOptions.UpdateReference se ha configurado como 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

Esta es la salida por consola del código de ejemplo anterior cuando la propiedad DeleteOptions.UpdateReference se ha configurado como false. Como puede ver, la fórmula en la celda E3 de la segunda hoja de cálculo no se actualiza y su valor de celda ahora es 0 en lugar de 4, lo cual es inválido.

 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