Can I permanently apply default CSV filter settings?

Hi.

Currently, I have a workflow that requires the use of CSV files to manage some data. I need to be able to export my CSV files with formulas in-tact in case I want to close the file and open it again later. Formulas are used to help visually separate data in this document, so losing that capability after closing the app isn’t very good.

Currently this is sort of possible, by checking the Edit Filter Settings option in the Save As dialogue. However, the values here are not persistent; exporting another CSV file after closing and reopening the app will export it with evaluated formulas, instead of the actual formula itself. Every time, I have to go to “Save As”, click “Edit Filter Settings”, and then export that way. I can’t just save the CSV file like normal.

Is there any way that I can make the application permanently export with formulas in tact so that I don’t have to remember to check this box every time I reopen the file? I’d like it to remember that I want formulas exported instead of evaluated.

Note: I do not want to enable the Formulas checkbox in the app’s display preferences; I need the formulas to display as evaluated while editing the CSV. This is not a solution, it should only affect the file on disk.

Thank you for your time.

CSV is a database exchange format. If it has formulas, it is a spreadsheet. Save the file in a spreadsheet format.

At first level: No you can’t, as giving you the last used settings is the predefined way.

.
As we talk of open-source software, at a second level you can change this, if you can modify the source code and compile your own version of LibreOffice…
.
As I don’t think you will create your own fork “XanOffice” my suggestion would be to create a little macro for your SaveAs>csv and bind it to a Menu or Key.
EDIT: Some basics here:
https://forum.openoffice.org/en/forum/viewtopic.php?t=101993
and maybe useful: Something like villeroys ShowFilterOptions for input

2 Likes

Yes.
You need to write a few lines of code for the export and again for the import.
The filter name to use is an ordinary string, and so is the sequence of tokens describing the set of option values for export and again for import. These strings as needed in your case(es) you can store (e.g.) assigning them to global variables defined in a module of your Standard library for Basic code in your user profile.
The only little problem is to study, to understand, and to apply correctly how these sequences of tokens need to be formed.
See CSV Filter parameters.
The methods you need for your code then are StarDesktop.LoadComponentFromURL() or
myCalcDocument.storeToURL() .
Unfortunately the structures you need are a bit unhandy.
Fortunately you won’t need to build the application from changed source code if you go along this way.
(You surely know that export to csv only treats the active sheet of the current spreadsheet document.)

3 Likes

Hey. Thank you for mentioning this, Wanderer. Your answer alongside the documentation mentioned by @Lupp

… has allowed me to set up a macro to achieve this. I was able to take the script provided on the forum you linked and adapt it into something more usable for me.

Here’s what I created. It’s roughly the same as the script provided on the forum, except this one overwrites the current file. Additionally, this will display a warning popup if the user is about to overwrite a file that doesn’t have the .csv extension, to help prevent accidental destruction of data on other spreadsheet formats.

The code is probably a bit lacking, I don’t know any BASIC (I’m a C#/C++ guy) but it gets the job done.

Thank you all for your advice.

View Macro Code
Sub ExportCSVWithFormulas
	Dim document as Object
	Dim dispatcher as Object
	document = ThisComponent.CurrentController.Frame
	dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
	
	Dim currentFilePath as String
	Dim pathLength as Integer
	Dim doExport as Boolean
	currentFilePath = ThisComponent.getURL()
	pathLength = Len(currentFilePath)
	doExport = True
		
	If pathLength >= 4 Then
		Dim isCSV as Integer
		isCSV = StrComp(Right(currentFilePath, 4), ".csv", 0)
		
		If isCSV <> 0 Then
			Dim userChoice as Integer
			userChoice = MsgBox("The current file does not have a CSV extension. Are you sure you want to overwrite it as a CSV?", 4, "Overwrite Different File?") 
			doExport = userChoice <> 7 And userChoice <> 0
		End If
	End If
	
	If doExport Then
		Dim args1(2) as new com.sun.star.beans.PropertyValue
		args1(0).Name = "URL"
		args1(0).Value = currentFilePath
		args1(1).Name = "FilterName"
		args1(1).Value = "Text - txt - csv (StarCalc)"
		args1(2).Name = "FilterOptions"
		args1(2).Value = "59,34,76,1,,0,false,true,false,true,false"
		rem https://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options#Token_7.2C_csv_import
		
		dispatcher.executeDispatch(document, ".uno:SaveAs", "", 0, args1())
	End If
End Sub
2 Likes

Thanks for sharing your solution here.

1 Like