将空白 Excel 单元格保存到 Rails Postgres 数据库

Posted

技术标签:

【中文标题】将空白 Excel 单元格保存到 Rails Postgres 数据库【英文标题】:Saving Blank Excel Cells to Rails Postgres Database 【发布时间】:2017-01-25 19:45:19 【问题描述】:

我已经设法让 Roo Gem 工作,以便我可以导入 Excel 文件并将其保存到我的应用程序的数据库中。这已经成功了几次,但似乎在某些行中包含空白的电子表格中会出现错误。我的代码如下:

需要'roo'

xlsx = Roo::Excelx.new(File.expand_path('../Downloads/LOCATION.xlsx'))

xlsx.each_row_streaming(offset: 1) do |row|
    Location.find_or_create_by(ukprn: row[0]&.value, accomurl: row[1]&.value, instbeds: row[2]&.value, instlower: row[3]&.value, instupper: row[4]&.value, locid: row[5]&.value, name: row[6]&.value, lat: row[7]&.value, long: row[8]&.value, locukprn: row[9]&.value, loccountry: row[10]&.value, privatelower: row[11]&.value, privateupper: row[12]&.value, suurl: row[13]&.value)
end

我从 Ruby 2.3.0 中读到了这一点,在方法之前包含了一个 &,它不包含 nil 方法错误,所以我希望这会起作用。

前 4 行似乎工作正常,但是当它到达下面有空格的一行时,我得到的错误是 sn-p:

Location Load (1.1ms)  SELECT  "locations".* FROM "locations" WHERE "locations"."ukprn" = $1 AND "locations"."accomurl" = $2 AND "locations"."instbeds" IS NULL AND "locations"."instlower" = $3 AND "locations"."instupper" = $4 AND "locations"."locid" = $5 AND "locations"."name" = $6 AND "locations"."lat" = $7 AND "locations"."long" IS NULL AND "locations"."locukprn" = $8 AND "locations"."loccountry" = $9 AND "locations"."privatelower" = $10 AND "locations"."privateupper" = $11 AND "locations"."suurl" = $12 LIMIT $13  [["ukprn", "10000055"], ["accomurl", "http://www.***es.ac.uk/studying-at-***es/accommodation/halls-in-detail/"], ["instlower", 4081], ["instupper", "4684"], ["locid", "6463"], ["name", "AB"], ["lat", "Abingdon & Witney College (Abingdon Campus)"], ["locukprn", 51], ["loccountry", "-1.28696"], ["privatelower", 10000055], ["privateupper", "XF"], ["suurl", "4219"], ["LIMIT", 1]]
ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR:  invalid input syntax for type numeric: "Abingdon & Witney College (Abingdon Campus)"
: SELECT  "locations".* FROM "locations" WHERE "locations"."ukprn" = $1 AND "locations"."accomurl" = $2 AND "locations"."instbeds" IS NULL AND "locations"."instlower" = $3 AND "locations"."instupper" = $4 AND "locations"."locid" = $5 AND "locations"."name" = $6 AND "locations"."lat" = $7 AND "locations"."long" IS NULL AND "locations"."locukprn" = $8 AND "locations"."loccountry" = $9 AND "locations"."privatelower" = $10 AND "locations"."privateupper" = $11 AND "locations"."suurl" = $12 LIMIT $13
    from /home/dave/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:606:in `exec_prepared'

我假设 Postgres 对保存在数据库中的空数据不满意,因此将“Abingdon & Witney College (Abingdon Campus)”移动到另一列,因为相关记录是迁移中所示的字符串记录如下:

class CreateLocations < ActiveRecord::Migration[5.0]
  def change
    create_table :locations do |t|
      t.text :name
      t.decimal :lat
      t.decimal :long
      t.string :locid, index: true
      t.integer :locukprn
      t.string :loccountry
      t.integer :privatelower
      t.string :privateupper
      t.string :ukprn, index: true
      t.string :suurl
      t.string :accomurl
      t.integer :instbeds
      t.integer :instlower
      t.string :instupper
      t.references :institute, foreign_key: true, index: true
    end
  end
end

这是出了什么问题,有什么解决方法的建议吗?

【问题讨论】:

ERROR: invalid input syntax for type numeric: "Abingdon &amp;amp; Witney College (Abingdon Campus)"我可能读错了,但这个值不是数字? 但模式中的数据也不是,它是“文本”,因为它位于“名称”列下。 【参考方案1】:

您的选择有:

[
 (0)["ukprn", "10000055"],
 (1)["accomurl", "http://www.***es.ac.uk/studying-at-***es/accommodation/halls-in-detail/"],
 (2)["instlower", 4081],
 (3)["instupper", "4684"],
 (4)["locid", "6463"],
 (5)["name", "AB"],
 (6)["lat", "Abingdon &amp; Witney College (Abingdon Campus)"],
 (7)["locukprn", 51],
 (8)["loccountry", "-1.28696"],
 (9)["privatelower", 10000055],
 (10)["privateupper", "XF"],
 (11)["suurl", "4219"],
 (12)["LIMIT", 1]
]

此 lat 值不是数字,您的迁移有 t.decimal :lat

【讨论】:

如上述评论中所述,该条目位于第 6 行,因此应位于“名称”下,根据迁移的格式为“文本”。 在我看来find_or_createlocations.latrow[7] 拉出,因此SELECT"locations"."lat" = $7 名称在6 中对吗? 是的,应该是。 “Abingdon & Witney College (Abingdon Campus)”来自电子表格的第 6 行,因此我不明白这个错误。 为了更清晰,我重新编辑了我的帖子。接下来我会问是否在 excel locid = 6463name = "AB" 中? 不,locid 应该是 AB。

以上是关于将空白 Excel 单元格保存到 Rails Postgres 数据库的主要内容,如果未能解决你的问题,请参考以下文章

Excel VBA - 如何选择范围向下到第一个空白单元格

请教JAVA使用POI导出excel处理空白单元格的问题

Excel:如何使某些单元格保持空白

Excel VBA将单元格数据保存到单独的工作表

求助excel单元格不同自动添加3个空白行

仅将可见单元格从 Excel 保存到 CSV