Script to bypass all confirmations when saving copy?

I’m not sure how to search for an answer to this, so any help is appreciated.

My workflow involves saving not only the .xlsx version of a file, but also saving it as a csv (tab separated) version.

When I want to save a copy as csv I currently have to do this:

  1. click button to “save copy”
  2. change file type to “csv”
  3. click “yes” to confirm saving over existing file
  4. choose the options for exporting (which never change from save to save)
  5. click “OK” to acknowledge that only the active sheet was saved.

I have used the macro recorder to try to record all these steps. What I want is to be able to run the macro and have all of these things happen without any interaction by me.

However, when I run the macro, the only thing it seems to to is open the save copy dialogue. Here is the text of the macro:

REM  *****  BASIC  *****

Sub Main

End Sub


sub saveAsCsv
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:SaveACopy", "", 0, Array())


end sub

I’m really lost with this. I’m happy to learn more (I can do basic javascript things but this seems like maybe visual basic?). If someone could point me in the right direction, or even better tell me if what I want to do is even possible, I would greatly appreciate it.

Your sample code is a recorded macro, with some Dispatcher commands located in the StarBasic IDE.
You need write your macros based on the StarBasic syntact and the API functions of the LibreOffice - if you want to work with macros efficiently. API: Application Programming Interface.

Start the studying of these things with Andrew Pitonyak’s free macro books:
https://www.pitonyak.org/oo.php

And you need install one of the excellent Object Inspection Tools: MRI or XrayTool:
https://extensions.libreoffice.org/en/extensions/show/mri-uno-object-inspection-tool
https://berma.pagesperso-orange.fr/index2.html
Then you will able to examine the properties and methods of the programming objects.

1 Like

Thank you so much! So it seems I have some studying to do. Now I just have to decide if the time saved by using the macro is going to be greater than the time it will take to learn a new scripting language!

A followup question would be if you know by experience if what I want to do is even (potentially) possible. If someone can assure me that it is, that would help my determination to learn. But if it’s probably not possible I’d love to know that now.

Thanks again!

Of course it is possible. You need use (parametrize) the Export filters (for the .csv format), and the StoreAsURL, StoreToURL API functions.

Note: the .csv format can not store any formatting properties, and it is possible to save (at once) one sheet only.

1 Like

That’s a huge help. Nothing worse than spending hours learning how to do something only to find that it isn’t possible. Thank you!

Similar topics on an another (AOO/LO) forum - with sample code snippets:

https://forum.openoffice.org/en/forum/viewtopic.php?f=45&t=19695
https://forum.openoffice.org/en/forum/viewtopic.php?t=81188

1 Like

See also online help CSV Filter parameters . You can even do a conversion from the command line without writing any macro/script.

To add to zizi64’s answer, that is using actual macros, you should find details about CSV files management in the following refcard: LibreOffice Files Basic RefCard

1 Like

Thanks! It all is looking rather complicated.

What is your programming background?
Why csv?

1 Like

Really, it’s just javascript. It looks like to do macros in LO I need to learn some visual basic?

You can write macros in JavaScript. The immensely complex API is the major problem. Why is it necessary to use csv? What is the other application you try to exchange data with?

1 Like

Oh, sorry, I missed that bit.

So, I take the csv file and import it into a node.js script that turns it into a js object that I then use in a js website.

I hadn’t considered that there would be a simpler alternative to csv. I thought that was as simple as one could get. I suppose the alternative would be to get node to read the xlsx file? That seems
infinitely more complicated, but I’m just guessing.

csv is the most difficult file format because it is so simple. It is not even a file format. It is plain text with an implicit (but not obvious) agreement on how to interprete the plain text as a table. There are thousands of different csv flavours out in the wild. Spreadsheets make things even more complicated. It is a modern myth that spreadsheets are the preferable type of application to handle csv.
If you are able to handle JSON with JavaScript, you don’t need any csv nor office software in order to nail down any problem with structured data.

Interesting! Thanks.

But I’m using Calc to input/author my data. It’s such that a spreadsheet is the easiest way to enter it. And I don’t have any problem using the csv I create. I’m just trying to streamline the creation of that csv from the xlsx file.

You are the only person who knows the exact csv flavour. I’m tired. I will not write the 99th version of some csv export. I’m out.

Thanks for the information you shared.

Just to add for anyone else who might be reading this, I’m quite happy with exactly the save-copy-as csv file I can create just by clicking though things normally in Calc. The reason that I posted the question was because I tried creating a macro to do this using the macro recorder and was surprised when it actually recorded nothing but the first step. Wasn’t how I expected a macro recorder to work. :grin: But thanks to everyone’s comments I understand a little better what’s going on.

The Macro recorder has a very limited capability. And it works in the Writer and Calc applications only, but not in the Draw nor in Impress.
You must study the API, and you must WRITE your LO macros.

1 Like

Thanks! That’s helpful.

The most generic solution I can think of with zero technical info:

Sub exportCSV()
REM semicolon separated, all strings quoted, number formats as displayed
Dim a(1) as new com.sun.star.beans.PropertyValue
a(0).Name = "FilterName"
a(0).Value = "Text - txt - csv (StarCalc)"
a(1).Name = "FilterOptions"
a(1).Value = "59,34,76,1,,0,true,true,true,false,false,0"
s = ThisComponent.getURL() &".csv"
ThisComponent.storeToURL(s, a())
End Sub