Bereiche einfügen
Einführung
In Excel können Sie einen Bereich auswählen, dann einen Bereich einfügen und andere Daten nach rechts oder nach unten verschieben.
Bereiche einfügen mit Aspose.Cells für Python Excel-Bibliothek
Aspose.Cells für Python via .NET bietet die Methode Cells.insert_range zum Einfügen eines Bereichs an.
Wie man Bereiche einfügt und Zellen nach rechts verschiebt
Fügen Sie einen Bereich ein und verschieben Sie Zellen nach rechts, wie in den folgenden Codes mit Aspose.Cells gezeigt:
from aspose.cells import CellArea, ShiftType, Workbook | |
# Instantiate a new Workbook. | |
workbook = Workbook() | |
# Get all the worksheets in the book. | |
worksheets = workbook.worksheets | |
# Get the first worksheet in the worksheets collection. | |
worksheet = workbook.worksheets[0] | |
cells = worksheet.cells | |
# Create a range of cells. | |
sourceRange = cells.create_range("A1", "A2") | |
startRow = sourceRange.first_row | |
startCol = sourceRange.first_column | |
# Input some data with some formattings into | |
# A few cells in the range. | |
cells.get(startRow, startCol).put_value("Test") | |
cells.get(startRow + 1, startCol).put_value("123") | |
ca = CellArea.create_cell_area("A1", "A2") | |
worksheet.cells.insert_range(ca, ShiftType.RIGHT) | |
print(worksheet.cells.get("B1").string_value == "Test") |
Wie man Bereiche einfügt und Zellen nach unten verschiebt
Fügen Sie einen Bereich ein und verschieben Sie Zellen nach unten, wie in den folgenden Codes mit Aspose.Cells gezeigt:
from aspose.cells import CellArea, ShiftType, Workbook | |
# Instantiate a new Workbook. | |
workbook = Workbook() | |
# Get all the worksheets in the book. | |
worksheets = workbook.worksheets | |
# Get the first worksheet in the worksheets collection. | |
worksheet = workbook.worksheets[0] | |
cells = worksheet.cells | |
# Create a range of cells. | |
sourceRange = cells.create_range("A1", "A2") | |
startRow = sourceRange.first_row | |
startCol = sourceRange.first_column | |
# Input some data with some formattings into | |
# A few cells in the range. | |
cells.get(startRow, startCol).put_value("Test") | |
cells.get(startRow + 1, startCol).put_value("123") | |
ca = CellArea.create_cell_area("A1", "A2") | |
worksheet.cells.insert_range(ca, ShiftType.DOWN) | |
print(worksheet.cells.get("A3").string_value == "Test") |