How to make a macro similar to the behavior of the command Main menu> Data> Text to Columns…
Have a lot of data that comes from a csv, some columns are dates but they are in text format. I need to convert to date format to do some sorting.
Via macro I was able to modify the column format to date, but the content is not changed by this formatting modification. Now I need a macro that does the same function as the “Text to Columns…” command, I tried using setFormulaArray to do this but the result was not as expected, some cells were in date format others in text format.
def test_date_format(*args):
doc = XSCRIPTCONTEXT.getDocument()
sheet = doc.getCurrentController().getActiveSheet()
date_column = sheet['D:D']
#Modify column D to date format (DD/MM/YYYY), it works OK
#number_format_key = 36
numbers = doc.NumberFormats
locale = doc.CharLocale
number_format_key = numbers.queryKey('DD/MM/AAAA', locale, True)
date_column.NumberFormat = number_format_key
#does not work as expected, some cells are in date format, others in text format
content_range = sheet['D2:D30']
content_value = content_range.DataArray
content_range.setFormulaArray(content_value)
test_date.ods (31,2,KB)
Would anyone know how to do this, can it be vba or python macro.
Thanks
setFormulaArray:
https://forum.openoffice.org/en/forum/viewtopic.php?p=346911&sid=91487894ffe209a1c1dbd88975497cad#p346911