Jump to content

Cambiare la dimensione o il carattere di tutte le formule

From The Document Foundation Wiki
This page is a translated version of the page Macros/Math/001 and the translation is 86% complete.
Outdated translations are marked like this.


Descrizione

Quando volete modificare un attributo in tutte le formule che state per scrivere, seguite la procedura descritta in Faq/Math/008. Se volete cambiare "dopo il fatto", dovete riaprire tutte le formule e apportare le modifiche. Se il numero di formule è elevato, questo può diventare rapidamente noioso.

Questo script seleziona gli oggetti formula incorporati[1] nel documento, e aggiorna le proprietà delle formule previste[2]. Questa macro risolve il problema i#5092.

A (slightly) alternative approach is offered in Ask121655.

Codice

In LibreOffice Basic:

# coding: utf-8
from __future__ import unicode_literals

import uno
# from com.sun.star.document import OfficeDocument

FRAME_DESKTOP = 'com.sun.star.frame.Desktop'
FORMULA_PROPERTIES = 'com.sun.star.formula.FormulaProperties'

class UIFormulaCollection():
    """ Raccolta di formule del documento corrente """
    def __init__(self):
        """ Prendete il documento corrente dall'interfaccia utente per impostazione predefinita """
        # ctx = uno.getComponentContext()  # GetDefaultContext
        # smgr = ctx.getServiceManager()  # GetProcessServiceManager
        # desktop =  smgr.createInstanceWithContext(FRAME_DESKTOP, ctx)  # StarDesktop
        # self.doc = desktop.CurrentComponent  # ThisComponent
        self.doc = XSCRIPTCONTEXT.getDocument()
        self.objects = self.doc.EmbeddedObjects
        self.names = self.objects.getElementNames()
    def reformat(self, fontHeight=None, font=None):
        """ aggiungete tutte le proprietà che desiderate """
        for name in self.names:
            element = self.objects.getByName(name).Model
            if element.supportsService(FORMULA_PROPERTIES):
                if font:
                    element.FontNameFunctions = font
                    element.FontNameText = font
                    element.FontNameNumbers = font
                    element.FontNameVariables = font
                if fontHeight:
                    element.BaseFontHeight = fontHeight
            self.doc.reformat()


def reformatAllFormulas():
    ui = UIFormulaCollection()
    ui.reformat(font="Arial", fontHeight=14)

g_exportedScripts = reformatAllFormulas,
Option Explicit

Sub ChangeFormulasSizeAndFont()
    '''Cambia la dimensione o il carattere di tutte le formule matematiche nel documento'''
    Dim element As Object ' com.sun.star.text.TextEmbeddedObject.Model
    Dim realElem As Object ' com.sun.star.text.TextEmbeddedObject.(get)EmbeddedObject
    
    For each element in ThisComponent.getEmbeddedObjects() 

        If (not isNull(element.Model)) Then
            With element.Model
            If .supportsService("com.sun.star.formula.FormulaProperties") Then
                ' Force the update
                realElem = element.EmbeddedObject
                realElem.updateMode = com.sun.star.embed.EmbedUpdateModes.ALWAYS_UPDATE
                .BaseFontHeight = 14
                .FontNameVariables = "Arial"
                .FontNameFunctions = "Arial"
                .FontNameNumbers = "Arial"
                .FontNameText = "Arial"
            End If
            End With ' element As com.sun.star.formula.FormulaProperties
       End If

    Next element

    ThisComponent.reformat()

End Sub ' ChangeFormulasSizeAndFont

File ODT per provare la macro

Prendete qualsiasi Guida matematica pubblicata da LibreOffice a scopo di test.

Note

  • Consultate la documentazione API su FormulaProperties per esplorare altre proprietà che possono essere modificate.
  1. Si veda la documentazione API per com.sun.star.text.TextEmbeddedObjects e com.sun.star.text.TextEmbeddedObject servizi
  2. Consulta la documentazione API per com.sun.star.formula.FormulaProperties servizio