You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby on Rails中Caracal gem生成三列运营商地址列表问题求助

问题:Caracal生成三列运营商地址列表的字符串拆分异常

尝试用Ruby on Rails的Caracal gem生成三列邮寄标签样式的运营商地址列表,目前SQL查询可正常执行,docx文档也能生成,但始终无法按预期拆分字符串:要么多个运营商被塞进同一表格单元格,要么自定义分隔符被字面显示出来。

Rails代码

def create
  stored_procedure = "EXEC dbo.GetDocumentData_DoStuff"

begin
  # Retrieve data from the stored procedure
  result = ActiveRecord::Base.connection.exec_query(stored_procedure).first
  operator_list = result['operatorList'] || result['opList']

  # Ensure operator_list is not nil
  if operator_list.nil?
    flash[:error] = "No data available to generate the document."
    redirect_to request.referrer and return
  end

  # Prepare data for columns
  addresses = operator_list.split(" | ")

  # Create a new Document with Caracal
  Caracal::Document.save 'report.docx' do |docx|
    docx.style do
      id 'Normal'
      name 'Tahoma'
      font 'Tahoma'
      size 20 # Setting size in half-points (20 half-points = 10 points)
    end

    docx.h1 'Operator List Report'
    docx.h3 "Generated on #{Time.now.strftime('%Y-%m-%d')}"

    # Add Operator List with address in three columns
    addresses.each_slice(3) do |row|
      docx.table([row]) do
        row.each do |address|
          address.split(" /n ").each do |line|
            docx.p line, font: 'Tahoma', size: 18, align: :left
          end
        end
      end
    end
  end

  # Send the file to the user
  send_file 'report.docx', type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', filename: 'report.docx'
rescue => e
  flash[:error] = "There was an error generating the document: #{e.message}"
  redirect_to request.referrer
end

SQL语句

INSERT INTO #operatorTable (FormattedAddress)
SELECT DISTINCT 
    CONCAT_WS(' /n ',
        CONCAT(rc.CONAME, ' (', rc.CONO, ')'),
        NULLIF(rc.ADDR1, ''),
        NULLIF(rc.ADDR2, ''),
        RTRIM(CONCAT(
            NULLIF(rc.CITY, ''), 
            CASE 
                WHEN rc.CITY <> '' AND rc.STATE <> '' THEN ', ' ELSE '' 
            END,
            NULLIF(rc.STATE, ''), 
            ' ', 
            CASE 
                WHEN NULLIF(rc.ZIP2, '') IS NOT NULL THEN CONCAT(NULLIF(rc.ZIP1, ''), '-', NULLIF(rc.ZIP2, ''))
                ELSE NULLIF(rc.ZIP1, '')
            END
        )),
        ' | '
    )

当前最接近预期的效果如附图所示,需要解决字符串拆分问题。

内容的提问来源于stack exchange,提问作者Sean Gillespie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 11:57:41