Skip to content

Adding New Format

Sergio Cambra edited this page Jul 22, 2024 · 1 revision

A new format to export can be added to conf.export.formats and defining some methods:

conf.export.formats << :xls

Then, the format will be available as a new radio button in the export form.

Add export_respond_to_<format> to the controller, with the code exporting the data to that format, setting response_body with the exported data or Enumerator to use streaming. The method can access the records to export in @records variable, and the requested columns in @export_columns.

It can use some helpers to get the data:

  • format_export_column_header_name(column) to get the header for a column
  • export_column_header_style(column, format) to get the styles for the column header, if the format supports formatting as xlsx does.
  • get_export_column_value(record, column, format) to get the value for a record's column, it will return an array with 2 items, value and style.

If views are not used to generate the data, helpers can be called as methods of view_context. See export_respond_to_csv and export_respond_to_xlsx in lib/active_scaffold/actions/export.rb for examples of exporting data.

Clone this wiki locally