Forum OpenACS Development: List builder with .csv download and empty fields

Hello,

I am trying to create a list builder that exports to .csv. I have been successful with that part, but i have a couple of fields that must be left blank that are not being exported with the rest of the data. Is there a way to force the empty fields to export? I am using display_template to force the column to show empty in the browser, but when it is exported to csv the column is gone in excel. also can the .csv file have the labels be the column headers? The .csv will be viewed in excel.

template::list::create \
-name monitor \
-multirow monitor \
-key empapp_id \
-elements {
empapp_id {
label "reqnum"
}
lname {
label "lname"
}
fname {
label "fname"
}
race {
label "race"
}
gender {
label "sex"
}
birthdt {
label "birthdt"
display_template " " #I would like this blank column to be exported to the .csv
}
datesubmitted {
label "appdate"
}
} -selected_format csv \
-formats {
csv { output csv }
}

db_multirow ...

template::list::write_output -name monitor

Collapse
Posted by Brad Poulton on
I was able to create the .csv with empty fields by removing the display_template and adding a NULL AS birthdt to the SQL SELECT statement. Now i wonder how to export the labels as the headers for the columns of the .csv file.
Collapse
Posted by Dave Bauer on
You could have added the column as empty by adding it like this
db_multirow -extend {birthdt} my_data ...

The key is that it needed to exist as a column in the multirow.

To change the headers you'd need to modify template::list::write_csv

Something like this

foreach __element_name $list_properties(display_elements) {
template::list::element::get_reference -list_name $name -element_name $__element_name
if {!$element_properties(hide_p)} {
lappend __csv_cols [csv_quote $__element_name]
lappend __csv_labels $element_properties(label)
}
}
append __output "\"[join $__csv_labels "\",\""]\"\n"

Collapse
Posted by Brad Poulton on
Why doesn't template::list::write_output output the labels as the column headers automatically?

thanks

Collapse
Posted by Dave Bauer on
Good question. I'll update it on CVS so the next version uses the labels as headers. It is much nicer.
Collapse
Posted by Deds Castillo on
Dave,

If you make that the default, I think it's better if you can still pass a switch to use the column names so that a CSV download can easily be reused by a developer to use for code that accepts a CSV upload for processing.

Collapse
Posted by Brad Poulton on
I agree, I think adding a switch would be better than making the labels be default.