Jump to content

Macro/Basic/Calc/Aree

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

Return to Index

Aree

Selezionare una cella per nome

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

Selezionare una cella in base alla posizione

sheet = ThisComponent.CurrentController.ActiveSheet

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

Selezionare un'area per nome

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

Selezionare un'area in base alla posizione

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)

Ottenere un indirizzo come stringa

sheet = ThisComponent.CurrentController.ActiveSheet

    cell = sheet.getCellRangeByName("B2")
    range = sheet.getCellRangeByName("B2:C5")

    Msgbox cell.AbsoluteName
    Msgbox range.AbsoluteName

Ottenere un indirizzo come struttura

  • Solo per le celle

sheet = ThisComponent.CurrentController.ActiveSheet

    cell = sheet.getCellRangeByName("B2")
    address = cell.CellAddress

    Msgbox address.Column
    Msgbox address.Row

  • Per le aree

sheet = ThisComponent.CurrentController.ActiveSheet

    range = sheet.getCellRangeByName("B2:C5")
    address = range.RangeAddress

    Msgbox address.StartColumn
    Msgbox address.StartRow
    Msgbox address.EndColumn
    Msgbox address.EndRow

Gruppi di aree

  • Creare e aggiungere aree

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

  • Si può usare una matrice

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

  • Eliminare

group.removeRangeAddress(range2.RangeAddress)

Colonne

  • Conta

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

    MsgBox range.Columns.Count

  • Seleziona per indice

MsgBox range.Columns(0).Name

  • Seleziona per nome

MsgBox range.Columns("E").Name

Righe

  • Conta

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

    MsgBox range.Rows.Count

  • Seleziona per indice

MsgBox range.Rows(1).AbsoluteName

Selezione corrente

selection = ThisComponent.CurrentSelection
    For Each service In selection.SupportedServiceNames
        MsgBox service
    Next service

ATTENZIONE: La seleziona corrente può essere diverse cose. Usare il metodo supportsService per evitare ambiguità.

Se la selezione é Nome del servizio supportato
Cella com.sun.star.sheet.SheetCell
Area com.sun.star.sheet.SheetCellRange
Aree com.sun.star.sheet.SheetCellRanges
Colonne com.sun.star.table.TableColumns
Righe com.sun.star.table.TableRows
Forma com.sun.star.drawing.ShapeCollection


Aree con nome

  • Gestire i nomi Ctrl+F3

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

    MsgBox range.AbsoluteName

  • Conta

names = ThisComponent.NamedRanges
    MsgBox names.Count

  • Seleziona per indice

range = names(0)
    MsgBox range.Name

  • Seleziona per nome

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

  • Aggiungi nuova

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

Per i flag vedete: NamedRangeFlag

  • Eliminare

names.removeByName("NewData")

  • Esportare un elenco

names.outputList(cell.CellAddress)

Cursori

  • Creare per area

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

    cursor = sheet.createCursorByRange(cell)
    MsgBox cursor.ImplementationName

  • Spostare alla cella seguente

cursor.gotoNext()

  • Spostare alla cella precedente

cursor.gotoPrevious()

  • Spostare a (Colonne, Righe)

cursor.gotoOffset(2, 5)

  • Spostare all'inizio

cursor.gotoStart()

  • Spostare alla fine

cursor.gotoEnd()

  • Estendere alla regione corrente

cursor.collapseToCurrentRegion()

Suggerimento: Selezionate l'ultima riga usata dell'area

cursor.gotoEnd()
    MsgBox cursor.RangeAddress.EndRow

    'Or

    cursor.collapseToCurrentRegion()
    MsgBox cursor.RangeAddress.EndRow

  • Espandere alla matrice corrente

cursor.collapseToCurrentArray()

  • Espandere all'area aggiunta

cursor.collapseToMergedArea()

  • Espandere a tutte le colonne

cursor.expandToEntireColumns()

  • Espandere a tutte le righe

cursor.expandToEntireRows()

  • Modificare le dimensioni dell'intervallo

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

  • Spostare all'inizio dell'area usata

cursor.gotoStartOfUsedArea(False)

  • Spostare alla fine dell'area usata

cursor.gotoEndOfUsedArea(False)

L'argomento espande (True) o meno (False) la selezione dell'area.


Selezioni speciali

Suggerimento: Tutti i metodi che seguono selezionano un gruppo di aree (ScCellRangesObj)

  • Seleziona le celle visibili

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

  • Seleziona le celle vuote

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

  • Seleziona le celle che contengono testo

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

Vedete le opzioni per i flag: CellFlags

Suggerimento: Potete sommare due o più valori.

  • Seleziona le celle con errori nella formula

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

Vedete le opzioni per i flag: FormulaResult

  • Seleziona in base alla differenza di valore della colonna.

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

  • Seleziona in base alla differenza di valore della riga.

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

  • Seleziona le celle dell'intersezione

sheet = ThisComponent.CurrentController.ActiveSheet
    range1 = sheet.getCellRangeByName("B2:I11")
    range2 = sheet.getCellRangeByName("G8:N17")

    ranges = rango1.queryIntersection(rango2.RangeAddress)


Lavorare con le aree

Copiare

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

    sheet.copyRange(target.CellAddress, source.RangeAddress)

Potete copiare delle colonne o delle righe, ma assicuratevi che la cella di destinazione sia la prima cella.

source_columns = sheet.Columns(2)
    source_rows = sheet.Rows(1)

    target_column = sheet.getCellRangeByName("H1")  'First row
    target_row = sheet.getCellRangeByName("A20")    'First column

Spostare

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

    sheet.moveRange(target.CellAddress, source.RangeAddress)

Inserire

  • Inserire e spostare le celle in basso

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)

Per altre opzioni vedete: CellInsertMode

  • Inserire colonne

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

  • Inserire righe

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

Eliminare

  • Eliminare e spostare le celle in alto

sheet = ThisComponent.CurrentController.ActiveSheet
    selection = ThisComponent.CurrentSelection

    sheet.removeRange(selection.RangeAddress, com.sun.star.sheet.CellDeleteMode.UP)

Per altre opzioni vedete: CellDeleteMode

  • Eliminare colonne

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

  • Eliminare righe

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