Actualice referencias en otras hojas de trabajo mientras elimina columnas y filas en blanco en una hoja de trabajo
Actualice referencias en otras hojas de trabajo mientras elimina columnas y filas en blanco en una hoja de trabajo
Consulte el siguiente código de ejemplo y su salida de consola. La celda E3 de la segunda hoja de trabajo tiene una fórmula =Hoja1!C3 que hace referencia a la celda C3 de la primera hoja de trabajo. si vas a establecerDeleteOptions.UpdateReference propiedad comoverdadero , 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 estableceDeleteOptions.UpdateReference propiedad comofalso, la fórmula en la celda E3 de la segunda hoja de cálculo seguirá siendo =Hoja1!C3 y dejará de ser válida.
Ejemplo de programación
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook | |
Workbook wb = new Workbook(); | |
// Add second sheet with name Sheet2 | |
wb.Worksheets.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.Worksheets[0]; | |
sht1.Cells["C1"].PutValue(4); | |
sht1.Cells["K30"].PutValue(4); | |
// Access second sheet and add formula in cell E3 which refers to cell C1 in first sheet | |
Worksheet sht2 = wb.Worksheets[1]; | |
sht2.Cells["E3"].Formula = "'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. | |
Console.WriteLine("Cell E3 before deleting blank columns and rows in Sheet1."); | |
Console.WriteLine("--------------------------------------------------------"); | |
Console.WriteLine("Cell Formula: " + sht2.Cells["E3"].Formula); | |
Console.WriteLine("Cell Value: " + sht2.Cells["E3"].StringValue); | |
// 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.UpdateReference = true; | |
// Delete all blank rows and columns with delete options | |
sht1.Cells.DeleteBlankColumns(opts); | |
sht1.Cells.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. | |
Console.WriteLine(""); | |
Console.WriteLine(""); | |
Console.WriteLine("Cell E3 after deleting blank columns and rows in Sheet1."); | |
Console.WriteLine("--------------------------------------------------------"); | |
Console.WriteLine("Cell Formula: " + sht2.Cells["E3"].Formula); | |
Console.WriteLine("Cell Value: " + sht2.Cells["E3"].StringValue); |
Salida de consola
Esta es la salida de la consola del código de muestra anterior cuandoDeleteOptions.UpdateReference la propiedad se ha establecido comoverdadero.
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 de la consola del código de muestra anterior cuandoDeleteOptions.UpdateReference la propiedad se ha establecido comofalso. Como puede ver, la fórmula en la celda E3 de la segunda hoja de trabajo no se actualiza y su valor de celda ahora es 0 en lugar de 4, lo cual no es vá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