Axlsx Gem:Ruby – 创build一个下拉列表
wb.add_worksheet(name: 'Report') do |sheet| sheet.add_data_validation("D25", { :type => :list, :formula1 => 'list!D11:D17', :showDropDown => false, :showInputMessage => true, :promptTitle => 'blah blah', :prompt => 'Please select a valid blah' }) end
我不知道如何填充下拉菜单。 该模板显示了一个插话,告诉我它知道我希望这是一个下拉菜单。
用你的formula1 => 'list!D11:D17'
你可以在另一个Worksheet命名list
引用。
如果仅引用D11:D17
,则将从选定区域获取值:
require 'axlsx' Axlsx::Package.new do |p| wb = p.workbook wb.add_worksheet(name: 'Report') do |sheet| sheet.add_data_validation("A10", { :type => :list, :formula1 => 'A1:A9', :showDropDown => false, :showInputMessage => true, :promptTitle => 'blah blah', :prompt => 'Please select a valid blah' }) end p.serialize('simple.xlsx') puts "Wrote simple.xlsx" end
你可以用你的list!
引用,但是你必须命名你的工作表list
。
例:
require 'axlsx' Axlsx::Package.new do |p| wb = p.workbook wb.add_worksheet(name: 'list') do |sheet| 1.upto(10){|i| sheet.add_row([i])} end wb.add_worksheet(name: 'Report') do |sheet| sheet.add_data_validation("A1", { :type => :list, :formula1 => 'list!A1:A9', :showDropDown => false, :showInputMessage => true, :promptTitle => 'blah blah', :prompt => 'Please select a valid blah' }) end p.serialize('simple.xlsx') puts "Wrote simple.xlsx" end