Save and close ods file from command line

I can open a .ods file from the commmand line (soffice -o file.ods).

Can I also save and close it from the command line?

I ask this because I have cells with external links that I need to update. My hope is that I can write a script to 1. open the file which will automatically update the cells and then 2. save the file 3. close libreoffice calc (I guess I can always use “kill” on the terminal for this last step).

Thanks!!

Welcome!
Executing scripts from the command line has been discussed in several threads here, you can easily find them if you need more information.
To done your work, do the following:

  1. Choose Tools - Macros - Organize Macros - LibreOffice Basic, click the Organizer button.

  2. In the My Macros & Dialogs section, create a new library, name it, for example, updateRefs.

  3. Go to edit (upper right button) and rename the created Module1 to UpdateHidden

  4. Paste the following code into the module:

Option Explicit 

Sub updateAndSave(sSourcePath As String)
Dim sPath As String
Dim oDoc As Variant 
Dim loadComponentProperties(0) As New com.sun.star.beans.PropertyValue
Rem Prevent screen flashing - perform file upload in "stealth mode"
	loadComponentProperties(0).Name= "Hidden"
	loadComponentProperties(0).Value= True
Rem We can load the spreadsheet using loadComponentFromURL().
Rem But the OpenDocument()  from the Tools library does it better.
Rem Therefore, we load the Tools library:
	GlobalScope.BasicLibraries.LoadLibrary("Tools")
Rem Convert the full path to the file, received in the parameter, 
Rem into the correct URL:
	sPath = ConvertToURL(sSourcePath)
Rem If a file with that name exists (and it may not exist):
	If FileExists(sPath) Then
Rem Open spreadsheet:
		oDoc = OpenDocument(sPath,loadComponentProperties)
Rem Recalculate all formulas (and update external links at the same time)
		oDoc.calculateAll()
Rem Save:
		oDoc.store()
Rem and close
    	oDoc.close(True)
    EndIf 
End Sub

(Of course, you can delete the comments, I inserted them to explain what exactly the script should do)

Now if you execute

"C:\Program Files\LibreOffice\program\soffice.exe"  --headless --nologo "macro:///UpdateRefs.UpdateHidden.updateAndSave(C:\TEMP\myTestFile.ods)

from the command line you will get the desired result.

image

3 Likes