Hướng Dẫn Convert Dữ Liệu Từ File Excel Sang JSON Cho App Pugo

Skip to content Search Gists Search Gists All gists Back to GitHub Sign in Sign up Sign in Sign up Dismiss alert {{ message }}

Instantly share code, notes, and snippets.

@lephuhai lephuhai/README.md Last active March 26, 2017 05:49 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
  • Embed Select an option
    • Embed Embed this gist in your website.
    • Share Copy sharable link for this gist.
    • Clone via HTTPS Clone using the web URL.

    No results found

    Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/lephuhai/95e62d82fe70e57dc60622421375ca4d.js"></script>
  • Save lephuhai/95e62d82fe70e57dc60622421375ca4d to your computer and use it in GitHub Desktop.
Code Revisions 3 Embed Select an option
  • Embed Embed this gist in your website.
  • Share Copy sharable link for this gist.
  • Clone via HTTPS Clone using the web URL.

No results found

Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/lephuhai/95e62d82fe70e57dc60622421375ca4d.js"></script> Save lephuhai/95e62d82fe70e57dc60622421375ca4d to your computer and use it in GitHub Desktop. Download ZIP Hướng dẫn convert dữ liệu từ file Excel sang JSON cho app Pugo Raw README.md
  • Bước 1: Cài đặt Nodejs

  • Bước 2: Cài đặt các node modules sử dụng npm

npm install
  • Bước 3: Sử dụng nodejs để chạy file script.js
node script.js

hoặc

npm start

Chú ý: Mỗi lần thực hiện convert data thì phải xóa các file json đã có trong thư mục result. Script sẽ đọc dữ liệu từ file pugo.xlsx và tên sheet của file excel là Sheet1

Raw script.js This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters
"use strict"
let xlsx = require('xlsx'),
_ = require('lodash'),
workbook = xlsx.readFile('./pugo.xlsx'),
data = workbook.Sheets.Sheet1,
fs = require('fs')
const directory = 'result'
let result = {};
for (let k in data) {
if (data.hasOwnProperty(k)) {
if (k[0] === '!') continue;
if (!_.isArray(result[k.slice(1)])) result[k.slice(1)] = [];
result[k.slice(1)].push(data[k].v);
}
}
const keys = Object.keys(result)
for (let i = 1; i < keys.length; i++) {
if (result[i][0] !== 'STT') {
fs.appendFileSync(`./${directory}/${result[i][1]}.json`,`
{
"Time" : ${result[i][8]},
"Question" : "${result[i][2]}",
"A" : "${result[i][3]}",
"B" : "${result[i][4]}",
"C" : "${result[i][5]}",
"D" : "${result[i][6]}",
"Answer" : "${result[i][7]}"
},`)
}
}
fs.readdir(`./${directory}`, function (err, files) {
files.forEach(function (file) {
let x = fs.readFileSync(`./${directory}/${file}`, 'utf8')
fs.writeFileSync(`./${directory}/${file}`, `[${x.substring(0, x.length -1).trim()}]`, {mode: 0x1b6})
})
})
console.log("[*] Create JSON files successfully!!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment You can’t perform that action at this time.

Từ khóa » Chuyển Excel Sang Json