Skip to content

Instantly share code, notes, and snippets.

@arton
Last active April 1, 2024 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arton/39662998a7a5582ceab6f36a6502128f to your computer and use it in GitHub Desktop.
Save arton/39662998a7a5582ceab6f36a6502128f to your computer and use it in GitHub Desktop.
dropdown list by fast_excel
module Libxlsxwriter
class Validator < FFI::Struct
layout :validate, :uint8,
:criteria, :uint8,
:ignore_blank, :uint8,
:show_input, :uint8,
:show_error, :uint8,
:dropdown, :uint8,
:value_number, :double,
:value_formula, :string,
:value_list, :pointer,
:value_datetime, Datetime.by_value,
:minimum_number, :double,
:minimum_formula, :string,
:minimum_datetime, Datetime.by_value,
:maximum_number, :double,
:maxmum_formula, :string,
:maximum_datetime, Datetime.by_value,
:input_title, :string,
:input_message, :string,
:error_title, :string,
:error_message, :string
end
attach_function :worksheet_data_validation_range, :worksheet_data_validation_range, [Worksheet, :uint32, :uint16, :uint32, :uint16, Validator], :error
module WorksheetWrappers
def data_validation_range(row_begin, col_begin, row_end, col_end, rb_list)
v = Libxlsxwriter::Validator.new
v[:validate] = 5
v[:criteria] = 0
v[:show_input] = v[:show_error] = 0
v[:dropdown] = 2
sp = rb_list.map {|s| FFI::MemoryPointer.from_string(s)}
sp << FFI::Pointer::NULL
list = FFI::MemoryPointer.new(:pointer, sp.length)
list.put_array_of_pointer(0, sp)
v[:value_list] = list
Libxlsxwriter.worksheet_data_validation_range(self, row_begin, col_begin, row_end, col_end, v)
end
end
end
tmp = Tempfile.open(['test', '.xlsx'])
book = FastExcel.open(tmp.path)
ws = book.add_worksheet('test_sheet')
ws.data_validation_range(1, 1, 100, 1, ['A', 'B', 'C'])
ws.write_value(row = 1, col = 1, 'AX')
ws.write_value(row = 2, col = 1, 'BX')
book.close
tmp.rewind
IO::binwrite('test.xlsx', tmp.read)
tmp.close!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment