Rails将excel文件的列映射到db列

时间:2016-08-05 08:02:29

标签: ruby-on-rails ruby excel sqlite

我正在使用引擎,我使用Roo gem解析excel文件。我面临的问题是我不知道如何映射列。

例如我的db-tablefirst_namelast_nameemp_iddesignation等。但我的Excel文件可以包含任何列名,例如它可以是fnamelnamee_iddesignation,也可以是FirstNameLastNameEmployeeId等。< / p>

现在我如何映射数据,使其存储在相应的列中。

我已经检查了this question但是如何设置别名,因为我的excel列标题可以是任何内容。如果可以别名,请告诉我如何做到这一点。这是我第一次使用rails引擎。我也是红宝石的新手。 任何帮助,将不胜感激。

2 个答案:

答案 0 :(得分:1)

如果excel列序列是固定的,则可以存储如下数据:

@xls = Roo::Spreadsheet.open(file, extension: :xls)
for i in 2..@xls.last_row
  Table.create(first_name: @xls.row(i)[1],last_name: @xls.row(i)[2],..)
end

希望它能帮到你......

答案 1 :(得分:1)

对于这种东西,我会使用服务对象。

 C:\rie>npm install node-protobuf
/
> node-protobuf@1.3.3 install C:\rie\node_modules\node-protobuf
> node-gyp rebuild


C:\rie\node_modules\node-protobuf>if not defined npm_config_node_gyp (node "C:\Program Files\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node "" rebuild )
Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
  init.cpp
  native.cpp
  parse.cpp
  serialize.cpp
c:\rie\node_modules\node-protobuf\src\common.h(11): fatal error C1083: Cannot open include file: 'google/protobuf/descriptor.h': No suc
h file or directory (compiling source file ..\src\native.cpp) [C:\rie\node_modules\node-protobuf\build\protobuf.vcxproj]
c:\rie\node_modules\node-protobuf\src\common.h(11): fatal error C1083: Cannot open include file: 'google/protobuf/descriptor.h': No suc
h file or directory (compiling source file ..\src\serialize.cpp) [C:\rie\node_modules\node-protobuf\build\protobuf.vcxproj]
c:\rie\node_modules\node-protobuf\src\common.h(11): fatal error C1083: Cannot open include file: 'google/protobuf/descriptor.h': No suc
h file or directory (compiling source file ..\src\parse.cpp) [C:\rie\node_modules\node-protobuf\build\protobuf.vcxproj]
c:\rie\node_modules\node-protobuf\src\common.h(11): fatal error C1083: Cannot open include file: 'google/protobuf/descriptor.h': No suc
h file or directory (compiling source file ..\src\init.cpp) [C:\rie\node_modules\node-protobuf\build\protobuf.vcxproj]
gyp ERR! build error
gyp ERR! stack Error: `C:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onExit (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\build.js:276:23)
gyp ERR! stack     at emitTwo (events.js:87:13)
gyp ERR! stack     at ChildProcess.emit (events.js:172:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12)
gyp ERR! System Windows_NT 10.0.10586
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\rie\node_modules\node-protobuf
gyp ERR! node -v v4.4.5
gyp ERR! node-gyp -v v3.3.1
gyp ERR! not ok
npm ERR! Windows_NT 10.0.10586
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "node-protobuf"
npm ERR! node v4.4.5
npm ERR! npm  v2.15.5
npm ERR! code ELIFECYCLE

npm ERR! node-protobuf@1.3.3 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-protobuf@1.3.3 install script 'node-gyp rebuild'.