Macros/Basic/Calc/Rangos

    From The Document Foundation Wiki
    This page is a translated version of the page Macros/Basic/Calc/Ranges and the translation is 100% complete.
    Other languages:

    Return to Index

    Rangos

    Celda por nombre

    sheet = ThisComponent.CurrentController.ActiveSheet
        cell = sheet.getCellRangeByName("A1")

    Celda por posición

    sheet = ThisComponent.CurrentController.ActiveSheet
    
        'Cell C6
        column = 2
        row = 5
        cell = sheet.getCellByPosition(column, row)

    Rango por nombre

    sheet = ThisComponent.CurrentController.ActiveSheet
        range = sheet.getCellRangeByName("B5:D10")

    Rango por posición

    sheet = ThisComponent.CurrentController.ActiveSheet
    
        'Range B5:D10
        start_column = 1
        start_row = 4
        end_column = 3
        end_row = 9
        range = sheet.getCellRangeByPosition(start_column, start_row, end_column, end_row)

    Obtener dirección como cadena

    sheet = ThisComponent.CurrentController.ActiveSheet
    
        cell = sheet.getCellRangeByName("B2")
        range = sheet.getCellRangeByName("B2:C5")
    
        Msgbox cell.AbsoluteName
        Msgbox range.AbsoluteName

    Obtener dirección como estructura

    • Solo para celdas

    sheet = ThisComponent.CurrentController.ActiveSheet
    
        cell = sheet.getCellRangeByName("B2")
        address = cell.CellAddress
    
        Msgbox address.Column
        Msgbox address.Row

    • Para rangos

    sheet = ThisComponent.CurrentController.ActiveSheet
    
        range = sheet.getCellRangeByName("B2:C5")
        address = range.RangeAddress
    
        Msgbox address.StartColumn
        Msgbox address.StartRow
        Msgbox address.EndColumn
        Msgbox address.EndRow

    Grupo de rangos

    • Crear y agregar rangos

    doc = ThisComponent
        sheet = doc.CurrentController.ActiveSheet
        group = doc.createInstance("com.sun.star.sheet.SheetCellRanges")
    
        range1 = sheet.getCellRangeByName("A1:B2")
        range2 = sheet.getCellRangeByName("D5:F10")
        range3 = sheet.getCellRangeByName("H15:J20")
    
        group.addRangeAddress(range1.RangeAddress, False)
        group.addRangeAddress(range2.RangeAddress, False)
        group.addRangeAddress(range3.RangeAddress, False)
    
        MsgBox group.RangeAddressesAsString

    • Podemos usar un array

    ranges = Array(range1.RangeAddress, range2.RangeAddress, range3.RangeAddress)
        group.addRangeAddresses(ranges, False)

    • Borrar

    group.removeRangeAddress(range2.RangeAddress)

    Columnas

    • Contar

    sheet = ThisComponent.CurrentController.ActiveSheet
        range = sheet.getCellRangeByName("D5:F10")
    
        MsgBox range.Columns.Count

    • Acceso por índice

    MsgBox range.Columns(0).Name

    • Acceso por nombre

    MsgBox range.Columns("E").Name

    Filas

    • Contar

    sheet = ThisComponent.CurrentController.ActiveSheet
        range = sheet.getCellRangeByName("D5:F10")
    
        MsgBox range.Rows.Count

    • Acceso por índice

    MsgBox range.Rows(1).AbsoluteName

    Selección actual

    selection = ThisComponent.CurrentSelection
        MsgBox selection.ImplementationName

    PRECAUCIÓN: La selección actual pueden ser muchas cosas.

    Si la selección es: ImplementationName
    Celda ScCellObj
    Rango ScCellRangeObj
    Rangos ScCellRangesObj
    Columnas ScTableColumnsObj
    Filas ScTableRowsObj
    Forma com.sun.star.drawing.SvxShapeCollection

    Rangos con nombre

    • Administrar nombres Ctrl+F3

    sheet = ThisComponent.CurrentController.ActiveSheet
        range = sheet.getCellRangeByName("Address")
    
        MsgBox range.AbsoluteName

    • Contar

    names = ThisComponent.NamedRanges
        MsgBox names.Count

    • Acceso por índice

    range = names(0)
        MsgBox range.Name

    • Acceso por nombre

    range = names("Address")
        MsgBox range.Name

    • Agregar nuevo

    sheet = ThisComponent.CurrentController.ActiveSheet
        cell = sheet.getCellRangeByName("A1")
        names = ThisComponent.NamedRanges
        flag = 0
        names.addNewByName("NewData", "$A$1:$B$7", cell.CellAddress, flag)

    Para el argumento flags mira: NamedRangeFlag

    • Borrar

    names.removeByName("NewData")

    • Exportar lista

    names.outputList(cell.CellAddress)

    Cursores

    • Crear desde rango

    sheet = ThisComponent.CurrentController.ActiveSheet
        cell = sheet.getCellRangeByName("A1")
    
        cursor = sheet.createCursorByRange(cell)
        MsgBox cursor.ImplementationName

    • Mover a la siguiente celda

    cursor.gotoNext()

    • Mover a la celda previa.

    cursor.gotoPrevious()

    • Mover a (Columnas, Filas).

    cursor.gotoOffset(2, 5)

    • Mover al inicio

    cursor.gotoStart()

    • Mover al final

    cursor.gotoEnd()

    • Expandir a la región actual

    cursor.collapseToCurrentRegion()

    Tip: Obtener la última fila usada en el rango.

    cursor.gotoEnd()
        MsgBox cursor.RangeAddress.EndRow
    
        'Or
    
        cursor.collapseToCurrentRegion()
        MsgBox cursor.RangeAddress.EndRow

    • Expandir a la matriz actual

    cursor.collapseToCurrentArray()

    • Expandir a las celdas combinadas.

    cursor.collapseToMergedArea()

    • Expandir a columnas enteras.

    cursor.expandToEntireColumns()

    • Expandir a filas enteras.

    cursor.expandToEntireRows()

    • Cambiar el tamaño del rango.

    columns = 2
        rows = 5
        cursor.collapseToSize(columns, rows)

    • Mover al inicio del área de usuario.

    cursor.gotoStartOfUsedArea(False)

    • Mover al final del área de usuario.

    cursor.gotoEndOfUsedArea(False)

    Argumento para expandir (True) o no (False) la selección del rango.


    Selecciones especiales

    Tip: Todos los siguiente métodos, siempre regresan un grupo de rangos (ScCellRangesObj).

    • Obtener las celdas visibles.

    selection = ThisComponent.CurrentSelection
        ranges = selection.queryVisibleCells()

    • Obtener las celdas vacías.

    selection = ThisComponent.CurrentSelection
        ranges = selection.queryEmptyCells()

    • Obtener las celdas con texto.

    selection = ThisComponent.CurrentSelection
        ranges = selection.queryContentCells(com.sun.star.sheet.CellFlags.STRING)

    Mira: CellFlags

    Tip: Puedes sumar dos o más valores.

    • Obtener celdas con error en la formula.

    selection = ThisComponent.CurrentSelection
        ranges = selection.queryFormulaCells(com.sun.star.sheet.FormulaResult.ERROR)

    Mira: FormulaResult

    • Obtener valores diferentes por columna.

    selection = ThisComponent.CurrentSelection
        source = selection.getCellByPosition(0, 0)
        ranges = selection.queryColumnDifferences(source.CellAddress)

    • Obtener valores diferentes por fila.

    selection = ThisComponent.CurrentSelection
        source = selection.getCellByPosition(0, 0)
        ranges = selection.queryRowDifferences(source.CellAddress)

    • Obtener la intersección de rangos.

    sheet = ThisComponent.CurrentController.ActiveSheet
        range1 = sheet.getCellRangeByName("B2:I11")
        range2 = sheet.getCellRangeByName("G8:N17")
    
        ranges = rango1.queryIntersection(rango2.RangeAddress)


    Trabajando con rangos

    Copiar

    sheet = ThisComponent.CurrentController.ActiveSheet
        source = sheet.getCellRangeByName("A1:B3")
        target = sheet.getCellRangeByName("D10")
    
        sheet.copyRange(target.CellAddress, source.RangeAddress)

    Podemos copiar columnas y filas, pero hay que asegurarse que la celda destino es la primer celda.

    source_columns = sheet.Columns(2)
        source_rows = sheet.Rows(1)
    
        target_column = sheet.getCellRangeByName("H1")  'First row
        target_row = sheet.getCellRangeByName("A20")    'First column

    Mover

    sheet = ThisComponent.CurrentController.ActiveSheet
        source = sheet.getCellRangeByName("A1:B3")
        target = sheet.getCellRangeByName("D10")
    
        sheet.moveRange(target.CellAddress, source.RangeAddress)

    Insertar

    • Insertar y mover celdas abajo

    sheet = ThisComponent.CurrentController.ActiveSheet
        target = CreateUnoStruct("com.sun.star.table.CellRangeAddress")
    
        With target
            .Sheet = sheet.RangeAddress.Sheet
            .StartColumn = 1
            .StartRow = 1
            .EndColumn = 3
            .EndRow = 3
        End With
    
        sheet.insertCells(target, com.sun.star.sheet.CellInsertMode.DOWN)

    Para otras opciones ver: CellInsertMode

    • Insertar columnas

    sheet = ThisComponent.CurrentController.ActiveSheet
        start = 4
        columns = 2
        sheet.Columns.insertByIndex(start, columns)

    • Insertar filas

    sheet = ThisComponent.CurrentController.ActiveSheet
        start = 2
        rows = 5
        sheet.Rows.insertByIndex(start, rows)

    Borrar

    • Borrar y mover las celdas arriba

    sheet = ThisComponent.CurrentController.ActiveSheet
        selection = ThisComponent.CurrentSelection
    
        sheet.removeRange(selection.RangeAddress, com.sun.star.sheet.CellDeleteMode.UP)

    Para otras opciones ver: CellDeleteMode

    • Borrar columnas

    sheet = ThisComponent.CurrentController.ActiveSheet
        start = 0
        columns = 2
        sheet.Columns.removeByIndex(start, columns)

    • Borrar filas

    sheet = ThisComponent.CurrentController.ActiveSheet
        start = 9
        rows = 3
        sheet.Rows.removeByIndex(start, rows)