سؤال

I have been commissioned to make a dynamic report building plugin for one of our systems here at work. the thing is that these reports are quite complex with 19 worksheets per report each sheet contains multiple tables, that are going to be populated with various amounts and types of data including data validations(drop-downs).

My initial thought was lets find a ruby gem. since its a rails app. there isn't much to pick from, but i did find Axlsx which looks like is my last hope.

One of the tables has 6 columns, of which 4 need data validation.

Now i have seen that you can style each column and even change formats through the .add_style method,

p.workbook do |wb|
  # define your regular styles
  styles = wb.styles
  title = styles.add_style :sz => 15, :b => true, :u => true
  default = styles.add_style :border => Axlsx::STYLE_THIN_BORDER
  header = styles.add_style :bg_color => '00', :fg_color => 'FF', :b => true
  profit = styles.add_style :border => Axlsx::STYLE_THIN_BORDER
  percent = styles.add_style :num_fmt => Axlsx::NUM_FMT_PERCENT, :border => Axlsx::STYLE_THIN_BORDER

and apply those styles when creating rows.

ws.add_row ['Quarter', 'Profit', '% of Total'], :style => header
ws.add_row ['Q1-2010', 'yes', '=B4/SUM(B4:B7)'], :style => [default, profit, percent]

and i have seen that there is a way of validating data.

p.workbook.add_worksheet(name: "dropdown") do |ws|
  ws.add_row ["rank_type"]
  ws.add_data_validation("A2:A1000", {
    :type => :list,
    :formula1 => 'lists!A2:A4',
    :showDropDown => false,
    :showErrorMessage => true,
    :errorTitle => '',
    :error => 'Please use the dropdown selector to choose a valid rank type',
    :errorStyle => :stop,
    :showInputMessage => true,
    :promptTitle => 'Rank type',
    :prompt => 'Please select a valid rank type'})
end

but is there a way to specify the cells that need validation when adding rows. or to add them into tables of variying sizes at all?

i was thinking it would be cool if you could specify data validations the same way you could styles. something like this.

 boolean_validation = format.add_data_validation :type => list, :formula => 'lists!A2:A4'... (rest of the options)

then when creating the rows

 ws.add_row ['Q1-2010', 'yes', '=B4/SUM(B4:B7)'], :style => [default, profit, percent], :format => [nil, boolean_validation, nil]

Has any one attempted something like this is this even possible? or any one got an idea on how to build such

هل كانت مفيدة؟

المحلول

As the library exists today, you will need to specify the data validation separately from your row inserts.

I would recommend having an object that is responsible for generating each table type that you require which can calculate and add the reference for the range that needs validation.

I think you might find the Axlsx.cell_range method handy, as it can take an array of cells and return the excel style 'A1:A9' type reference.

Unfortunately, the structure of the OOXML object tree makes it quite difficult to specify validation on a cell by cell basis, but feel free to add an issue to the github repository.

https://github.com/randym/axlsx

best

randym

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top