Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

Libreoffice 7.5 используя XCellProperties напиши код на С++ для задания шрифта XCell

  1. User

    Libreoffice 7.5 используя XCellProperties напиши код на С++ для задания шрифта XCell

  2. Assistant

    Приведенный ниже код на С++ иллюстрирует, как использовать XCellProperties в LibreOffice 7.5 для задания шрифта ячейки в таблице.

    #include <cppuhelper/bootstrap.hxx> #include <com/sun/star/uno/XComponentContext.hpp> #include <com/sun/star/frame/XModel.hpp> #include <com/sun/star/sheet/XSpreadsheetDocument.hpp> #include <com/sun/star/sheet/XSpreadsheet.hpp> #include <com/sun/star/table/XCellProperties.hpp> #include <com/sun/star/style/XStyleFamiliesSupplier.hpp> #include <com/sun/star/style/XStyle.hpp> #include <com/sun/star/style/CellStyle.hpp> #include <com/sun/star/beans/PropertyValue.hpp> #include <com/sun/star/beans/PropertyValues.hpp>

    using namespace com::sun::star::uno; using namespace com::sun::star::frame; using namespace com::sun::star::sheet; using namespace com::sun::star::table; using namespace com::sun::star::style; using namespace com::sun::star::beans;

    int main() { // Инициализация Uno Runtime bootstrap();

    // Получение программного контекста LibreOffice
    Reference<XComponentContext> xContext( 
        cppu::defaultBootstrap_InitialComponentContext() );
    
    // Получение модели текущего документа
    Reference<XComponent> xComponent =
        xContext->getServiceManager()->createInstanceWithContext(
            "com.sun.star.frame.Desktop", xContext)->queryInterface(
            Reference<XDesktop>());
    
    Reference<XFrame> xFrame = xComponent->getCurrentFrame();
    Reference<XModel> xModel = xFrame->getModel();
    Reference<XSpreadsheetDocument> xSpreadsheetDocument(xModel,UNO_QUERY);
    
    if (xSpreadsheetDocument.is())
    {
        // Получение первого листа таблицы документа
        Reference<XSpreadsheets> xSpreadsheets = xSpreadsheetDocument->getSheets();
        Reference<XIndexAccess> xIA(xSpreadsheets,UNO_QUERY);
        Reference<XSpreadsheet> xSheet(xIA->getByIndex(0),UNO_QUERY);
    
        // Получение первой ячейки
        Reference<XCell> xCell = xSheet->getCellByPosition(0,0);
    
        // Получение свойств ячейки
        Reference<XCellProperties> xProps(xCell,UNO_QUERY);
    
        if (xProps.is())
        {
            // Создание объекта PropertyValue для установки нового шрифта
            PropertyValue aValue;
            aValue.Name = "CharFontName";
            Any aValueAny;
            aValueAny <<= OUString::createFromAscii("Arial");
            aValue.Value = aValueAny;
    
            PropertyValues aProps;
            aProps.setPropertyValue("CharWeight", Any((sal_Int16)
                com::sun::star::awt::FontWeight::BOLD));
            aProps.setPropertyValue("CharFontFamily", Any(
                (sal_Int16)com::sun::star::awt::FontFamily::SWISS));
            aProps.set(aValue);
    
            // Установка свойств шрифта ячейки
            xProps->setPropertyValue("CellStyle", Any(
                (Reference<XPropertySet>)xSheet->getStyleFamilies()->
                getByName("CellStyles")->getByName("Default")));
            xProps->setPropertyValue("CharHeight", Any((float)12));
            xProps->setPropertyValue("CharColor", Any((sal_Int32)0xFF0000));
            xProps->setPropertyValue("IsCellHidden", Any(false));
            xProps->setPropertyValue("IsCellProtect", Any(false));
            xProps->setPropertyValue("IsCellLocked", Any(false));
            xProps->setPropertyValue("IsTextWrapped", Any(false));
            xProps->setPropertyValue("NumberFormat", Any((sal_Int32)0));
            xProps->setPropertyValue("Condition", Any());
            xProps->setPropertyValue("Annotation", Any());
            xProps->setPropertyValue("CharEscapementType", Any(
                (sal_Int16)com::sun::star::awt::Escapement::NORMAL));
            xProps->setPropertyValue("Orientation", Any(
                (sal_Int16)com::sun::star::table::CellOrientation::TOP_TO_BOTTOM));
            xProps->setPropertyValue("HoriJustify", Any(
                (sal_Int16)com::sun::star::sheet::CellHoriJustify::LEFT));
            xProps->setPropertyValue("VertJustify", Any(
                (sal_Int16)com::sun::star::sheet::CellVertJustify::TOP));
            xProps->setPropertyValue("RotationAngle", Any((sal_Int16)0));
            xProps->setPropertyValue("HyperLink", Any());
            xProps->setPropertyValue("HyperLinkURL", Any());
            xProps->set(aProps);
        }
    }
    
    // Освобождение ресурсов Uno Runtime
    shutdown();
    
    return 0;
    

    }