Página 1 de 1

Exemplo de uso LibreOffice

Enviado: 27 Fev 2025 20:13
por Itamar M. Lins Jr.
Olá!

Código: Selecionar todos

/*
   LC_FirstSampleToWrite.prg
   First sample to write with Libre Office Calc.
   Using HB32, Windows10, Libre Office last version.

   Compile with -lhbwin

   This code is a basic sample for tests.

   Copy the constants files from my web site.

   Last change : 2023-06-05
*/

// Parameter.
#define BM_FILE_CALC hb_dirbase() + "_Result_Calc.odt"         // File to create with his path.

#include "lo_LibreOfficeConstants.h"  // http://bernard.mouille.free.fr/mso-hb32/lo_LibreOfficeConstants.h
#include "bh_rgbColors.h"             // http://bernard.mouille.free.fr/mso-hb32/bh_rgbColors.h

procedure Main

   local args_Book          // Book create or open parameter(s).
   local oRgs               // Property object.
   local oServiceManager    // Libre Office Object first.
   local oDesktop           // Libre Office Object 2.
   local oBook              // Book object.
   local oSheet             // Sheet object.
   local oCursor            // Cursor object.

   setmode( 25, 80 )
   setcolor( "GR+/B" )
   @ 0, 0, maxrow(), maxcol() box space( 9 )

   ? "First sample to write with Libre Office Calc."
   ?
   ferase( BM_FILE_CALC )                               // Delete the old file created with this program.

   // Create the Libre Office objects.
   oServiceManager := win_oleCreateObject( "com.sun.star.ServiceManager" )
   oDesktop        := oServiceManager:createInstance( "com.sun.star.frame.Desktop" )

   // Create the create book property : display Calc.
   oRgs       := oServiceManager:bridge_GetStruct( "com.sun.star.beans.PropertyValue" )
   oRgs:Name  := "Hidden"
   oRgs:Value := .F.
   args_Book := { oRgs }

   // Create the book.
   oBook := oDesktop:loadComponentFromURL( "private:factory/scalc", "_blank", 0, args_Book )

   // Load the first sheet = sheet( 0 ).
   oSheet := oBook:Sheets:getByIndex( 0 )
   oSheet:Name := "First sheet created"                  // Rename the new sheet.

   // Borders for all the cells.
   with object oSheet:getCellRangeByPosition( 0, 0, 16383, 1048575 )  // Select all the cells.
      :setPropertyValue( "TopBorder"   , { nil, nil, 20, nil } )
      :setPropertyValue( "BottomBorder", { nil, nil, 20, nil } )
      :setPropertyValue( "LeftBorder"  , { nil, nil, 20, nil } )
      :setPropertyValue( "RightBorder" , { nil, nil, 20, nil } )
   end with

   // Write in row 0.
   oSheet:getCellByPosition( 0, 0 ):String := "My first Calc program"   // Write some text in cell A1.

   oSheet:getCellByPosition( 1, 0 ):NumberFormat := 10030           // Date year 4 digits default NumberFormat B1.
   oSheet:getCellByPosition( 1, 0 ):Value        := date()          // Write some date in cell B1.

   oSheet:getCellByPosition( 2, 0 ):NumberFormat := 10099           // Boolean default NumberFormat C1.
   oSheet:getCellByPosition( 2, 0 ):Value  := .F.                   // Write some logical in cell C1.

   oSheet:getCellByPosition( 3, 0 ):Value  := 18.5                  // Write some numeric in cell D1.

   // Write in row 2.
   oSheet:getCellByPosition( 0, 1 ):NumberFormat := 10100           // Text default NumberFormat A2.
   oSheet:getCellByPosition( 0, 1 ):String := "This is the row 2"   // Write some text in cell A2.

   oSheet:getCellByPosition( 1, 1 ):NumberFormat := 10049           // Date SQL year 4 digits default NumberFormat B2.
   oSheet:getCellByPosition( 1, 1 ):Value        := date()          // Write some date in cell B2.

   oSheet:getCellByPosition( 2, 1 ):NumberFormat := 10099           // Boolean default NumberFormat C2.
   oSheet:getCellByPosition( 2, 1 ):Value := .T.                    // Write some logical in cell C2.

   oSheet:getCellByPosition( 3, 1 ):NumberFormat := 10002           // Numeric 2 decimals default NumberFormat D2.
   oSheet:getCellByPosition( 3, 1 ):Value := 10                     // Write some numeric in cell D2.

   // Write in row 4
   oSheet:getCellByPosition( 2, 3 ):String := "Sum cells D1 to D2"   // Write some text in cell C4.

   oSheet:getCellByPosition( 3, 3 ):NumberFormat := 10002            // Numeric 2 decimals default NumberFormat D4.
   oSheet:getCellByPosition( 3, 3 ):Formula      := "=SUM(D1:D2)"    // Write some formula in cell D4.

   // Write in row 6
   with object oSheet:getCellByPosition( 0, 5 )                  // Write some text in cell A6 with font style.
      :String        := "Your country string"
      :HoriJustify   := loCellHoriJustifyRIGHT
      :charUnderline := loFontUnderlineSINGLE
      :charPosture   := loFontSlantITALIC
   end with

   with object oSheet:getCellByPosition( 1, 5 )                  // Write your country string in cell B6 with font style.
      :String        := oBook:CharLocale:Language + "-" + oBook:CharLocale:Country
      :HoriJustify   := loCellHoriJustifyCENTER
      :CellBackColor := rgb_yellow
      :CharHeight    := 16
      :CharWeight    := loFontWeightBLACK  // Bold.
      :CharColor     := rgb_red
   end with

   // Set automatic cells size.
   oCursor := oSheet:createCursor()
   oCursor:gotoStartOfUsedArea( .F. )
   oCursor:gotoEndOfUsedArea( .T. )
   oCursor:Columns:OptimalWidth = .T.

   oBook:storeAsURL( bh_FileToUrl( BM_FILE_CALC ), {} )  // Write the file on the disk.
   oServiceManager := nil
   return

// Tools functions.

// Convert a file and path Windows name to URL.
function bh_FileToUrl( cFile )
   local wFile := cFile
   if hb_at( "/", cFile ) > 0
      return cFile
   endif
   if left( wFile, 1 ) == "\"
      wFile := left( hb_dirbase(), 2 ) + wFile
   endif
   wFile := strtran( wFile, ":", "|" )
   wFile := strtran( wFile, "\", "/" )
   wFile := strtran( wFile, " ", "%20" )
   wFile := "///" + wFile
   return "file:" + wFile
Obs. Constantes adicionada aqui no forum caso o site dele tenha algum problema.
Regards,
Bernard
ConstantesExemploLibreOffice.rar
(4 KiB) Baixado 94 vezes
Saudações,
Itamar M. Lins Jr.