123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- const fs = require('fs');
- const path = require('path')
- const XLSX = require('xlsx');
- const {RedisConf, CommonConf} = require('./config')
- const Redis = require('ioredis');
- let dataPath = "../assets/scripts/data";
- let dataPrefix = "allstar_";
- function getSheet(filename) {
- let filePath = path.join(__dirname, "/excel", filename);
-
- let workbook = XLSX.readFileSync(filePath)
- let sheets = workbook.SheetNames;
- if(sheets.length == 1) {
- let firstSheet = sheets[0]
- let ws = workbook.Sheets[firstSheet]
- return XLSX.utils.sheet_to_json(ws, {range: 1})
- } else if(sheets.length > 1) {
- let sheetsObj = {}
- sheets.forEach(n => {
- let ws = workbook.Sheets[n]
- sheetsObj[n] = XLSX.utils.sheet_to_json(ws, {range: 1})
- })
- return sheetsObj;
- }
-
-
-
- }
- function writeToAssets (filename, content) {
- let writeFileName = filename.replace(/\.xlsx|\.xls/, '.js').replace(dataPrefix, "");
- let writePath = path.join(__dirname, dataPath, writeFileName);
- fs.writeFile(writePath, 'module.exports=' + JSON.stringify(content))
- console.log(JSON.stringify(content, null, 2));
- }
- function init () {
- let redis = new Redis(RedisConf.R1);
-
- fs.readdir("./excel", (err, file) => {
- file.forEach(n => {
- if(n.match(/\~\$/)) {
- return
- }
- let ws = getSheet(n);
- if(ws.length > 0) {
-
- let sheetName = 'client:' + n.replace("_", ":").replace(/\.xlsx|\.xls/, "");
- ws.forEach((value, index) => {
- let hkey = Object.keys(value)[0]
- redis.multi().hset(sheetName, value[hkey], JSON.stringify(value)).exec()
- })
- console.log(`Add ${sheetName} to redis`);
- } else {
-
- for(let i in ws) {
- let sheetName = 'client:' + n.replace("_", ":").replace(/\.xlsx|\.xls/, "") + ':' + i;
- ws[i].forEach((value, index) => {
- let hkey = Object.keys(value)[0]
- redis.multi().hset(sheetName, value[hkey], JSON.stringify(value)).exec()
- })
- console.log(`Add ${sheetName} to redis`);
- }
- }
-
-
- writeToAssets(n, ws);
- })
-
- })
- }
- init();
-
-
-
|