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, consulta el siguiente código de muestra y su salida en 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 configuras la propiedad DeleteOptions.update_reference 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 configurar DeleteOptions.update_reference como false, la fórmula en la celda E3 de la segunda hoja de cálculo seguirá siendo =Sheet1!C3 y se volverá inválida.
Ejemplo de Programación
from aspose.cells import DeleteOptions, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Create workbook | |
wb = 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 | |
sht1 = wb.worksheets[0] | |
sht1.cells.get("C1").put_value(4) | |
sht1.cells.get("K30").put_value(4) | |
# Access second sheet and add formula in cell E3 which refers to cell C1 in first sheet | |
sht2 = wb.worksheets[1] | |
sht2.cells.get("E3").formula = "'Sheet1'!C1" | |
# Calculate formulas of workbook | |
wb.calculate_formula() | |
# Print the formula and value of cell E3 in second sheet before deleting blank columns and rows in Sheet1. | |
print("Cell E3 before deleting blank columns and rows in Sheet1.") | |
print("--------------------------------------------------------") | |
print("Cell Formula: " + sht2.cells.get("E3").formula) | |
print("Cell Value: " + sht2.cells.get("E3").string_value) | |
# If you comment DeleteOptions.UpdateReference property below, then the formula in cell E3 in second sheet will not be updated | |
opts = DeleteOptions() | |
opts.update_reference = True | |
# Delete all blank rows and columns with delete options | |
sht1.cells.delete_blank_columns(opts) | |
sht1.cells.delete_blank_rows(opts) | |
# Calculate formulas of workbook | |
wb.calculate_formula() | |
# Print the formula and value of cell E3 in second sheet after deleting blank columns and rows in Sheet1. | |
print("") | |
print("") | |
print("Cell E3 after deleting blank columns and rows in Sheet1.") | |
print("--------------------------------------------------------") | |
print("Cell Formula: " + sht2.cells.get("E3").formula) | |
print("Cell Value: " + sht2.cells.get("E3").string_value) |
Salida de la consola
Esta es la salida en consola del código de muestra anterior cuando la propiedad DeleteOptions.update_reference 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 de la consola del código de ejemplo anterior cuando la propiedad DeleteOptions.update_reference se establece como false. Como se 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