Ask Your Question
0

How to append date variable in macro to export to .pdf file?

asked 2018-01-03 17:51:08 +0100

Montyj gravatar image

In LO calc, I have a simple macro to export a single sheet to a .pdf file. This export line works:

oDoc.storeToURL("file:///home/username/Desktop/Spreadsheets/test.pdf", aMediaDescriptor())

But I need to append the current simple date to the filename, trying this:

oDoc.storeToURL("file:///home/monty/Desktop/Spreadsheets/test'$(date +%Y-%m-%d)'.pdf", aMediaDescriptor())

That saves the filename literally as:

test'$(date +%Y-%m-%d)'.pdf

I have tried various combinations of parenthesis and quotes but am missing something!

Thanks!

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2018-01-03 21:50:24 +0100

Jim K gravatar image

updated 2018-01-04 01:49:42 +0100

Many things are easier in Python-UNO. This saves as test2018-01-03.pdf.

import datetime
today = datetime.date.today()
filename = "file:///home/monty/Desktop/Spreadsheets/test%s.pdf" % today
oDoc = XSCRIPTCONTEXT.getDocument()
oDoc.storeToURL(filename, aMediaDescriptor)

For more details, see my answer at https://ask.libreoffice.org/en/questi....

There are also a number of solutions for Basic given at https://forum.openoffice.org/en/forum.... It looks like the best way is to use service com.sun.star.util.NumberFormatter.

EDIT:

Here is Basic code.

sDatestamp = Format(Now(),"YYYY-MM-DD")
filename = "file:///home/monty/Desktop/Spreadsheets/test" & sDatestamp & ".pdf"
oDoc = ThisComponent
oDoc.storeToURL(filename, aMediaDescriptor)

Based on @Mike Kaganski's comment, it seems the link I posted from the OOo forum is partly out of date. "MMMM" now works properly.

edit flag offensive delete link more

Comments

1

Re: "It looks like the best way is to use service com.sun.star.util.NumberFormatter": this shows that Format function can easily be used in Basic.

Mike Kaganski gravatar imageMike Kaganski ( 2018-01-03 22:33:54 +0100 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2018-01-03 17:51:08 +0100

Seen: 155 times

Last updated: Jan 04 '18