Jump to content

Macro/Guida a Python/Calc/Fogli di Calc

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


⇐ Ritorna all'indice


Fogli

In questa Guida a Python utilizziamo la seguente convenzione:

  • foglio è un singolo foglio di calcolo all'interno di un documento
  • foglio elettronico è un documento di Calc


ATTENZIONE:

Copiate o importate la funzione msgbox dalle Funzioni utili

Nomi

  • Ricavare i nomi di tutti i fogli

doc = XSCRIPTCONTEXT.getDocument()
    names = doc.Sheets.ElementNames
    msgbox(names)


Contare tutti i fogli in un foglio elettronico

doc = XSCRIPTCONTEXT.getDocument()
    count = doc.Sheets.Count
    msgbox(count)


Iterare attraverso tutti i fogli

doc = XSCRIPTCONTEXT.getDocument()
    for sheet in doc.Sheets:
        msgbox(sheet.Name)


Ottenere i fogli

  • Ottenere il foglio attivo del documento in uso.

doc = XSCRIPTCONTEXT.getDocument()
    sheet = doc.getCurrentController().getActiveSheet()
    msgbox(sheet.Name)


  • Ottenere il foglio attivo del documento in uso, in base al suo indice

sheet = doc.Sheets[0]
    msgbox(sheet.Name)


  • Ottenere il foglio attivo del documento in uso, in base al suo nome

sheet = doc.Sheets['data']
    msgbox(sheet.Name)


Attivare un foglio

Impostare il focus su di un foglio

doc = XSCRIPTCONTEXT.getDocument()
    sheet = doc.Sheets[0]
    doc.getCurrentController().setActiveSheet(sheet)


Modificare il nome

  • Modificare il nome visibile all'utente

sheet = doc.Sheets[0]
    msgbox(sheet.Name)
    sheet.Name = 'OtherName'
    msgbox(sheet.Name)


  • Modificare il nome visibile solamente dal codice

sheet = doc.Sheets[0]
    msgbox(sheet.CodeName)
    sheet.CodeName = 'Data'
    msgbox(sheet.CodeName)


Impostare la visibilità di un foglio

  • Impostare IsVisible su False per nascondere un foglio e su True per visualizzarlo.

sheet = doc.Sheets[0]
    msgbox(sheet.IsVisible)
    sheet.IsVisible = False
    msgbox(sheet.IsVisible)
    sheet.IsVisible = True


Gestione delle password

  • Impostare una password

sheet = doc.Sheets[0]
    contra = 'letmein'
    sheet.protect(contra)


  • Eliminare la password

sheet = doc.Sheets[0]
    contra = 'letmein'
    sheet.unprotect(contra)


Controllare l'esistenza di un foglio all'interno del foglio elettronico

doc = XSCRIPTCONTEXT.getDocument()
    exists = 'data' in doc.Sheets
    msgbox(exists)


Inserire un foglio vuoto

insertNewByName(NAME, POSITION)

doc = XSCRIPTCONTEXT.getDocument()
    doc.Sheets.insertNewByName('new', 0)


Inserire un foglio esistente in un foglio elettronico

insertByName(SHEET, POSITION)

doc = XSCRIPTCONTEXT.getDocument()
    sheet = doc.createInstance('com.sun.star.sheet.Spreadsheet')
    doc.Sheets.insertByName(sheet, 0)


Spostare un foglio all'interno di un foglio elettronico

moveByName(NAME, NEW_POSITION)

doc = XSCRIPTCONTEXT.getDocument()
    doc.Sheets.moveByName('new', 3)


Eliminare un foglio dal foglio elettronico

removeByName(NAME)

doc = XSCRIPTCONTEXT.getDocument()
    doc.Sheets.removeByName('new')


Sostituzione di un foglio con uno vuoto

replaceByName(NAME, SHEET)

ATTENZIONE: Usatela con cautela. Questa funzione sostituisce un foglio NAME con un foglio vuoto.

doc = XSCRIPTCONTEXT.getDocument()
    sheet = doc.createInstance('com.sun.star.sheet.Spreadsheet')
    doc.Sheets.replaceByName('data', sheet)


Copiare un foglio

  • Rinominare un foglio

copyByName(SOURCE_NAME, NEW_NAME, POSITION)

ATTENZIONE: Se il nuovo nome esiste già, il foglio verrà copiato con il nome SOURCE_NAME_2 e non genererà alcun messaggio di errore.

  • Copiare un foglio all'interno dello stesso documento

doc = XSCRIPTCONTEXT.getDocument()
    doc.Sheets.copyByName('data', 'NewData', 10)


  • Copiare (o importare) un foglio da un altro documento

importSheet(SOURCE, SOURCE_NAME, POSITION)

source = XSCRIPTCONTEXT.getDocument()

    desktop = app.create_instance('com.sun.star.frame.Desktop', True)
    path = 'private:factory/scalc'
    doc = desktop.loadComponentFromURL(path, '_default', 0, ())

    doc.Sheets.importSheet(source, 'Services', 0)


Colorare la linguetta di un foglio

ATTENZIONE

Copiate o importate la funzione get_color dalle Funzioni utili

sheet = doc.Sheets['Sheet1']
sheet.TabColor = get_color(255, 0, 0)


⇐ Ritorna all'indice