Commit 05dbb0f5 authored by Carver_Hu's avatar Carver_Hu

excel脚本优化、文件整理

parent 0a78a9ca
var query = ...
query.begin()
try {
query.insertRow({
table: '',
data: {}
})
if (query.lastError().isValid()) throw query.lastError().text()
}
catch(e) {
}
\ No newline at end of file
{
"fileCollectionPath": "./",
"runnFile": "flag.txt",
"fieldMatch": {
"order_no": "Order\n生管投料单号",
"confirmation": "Confirmation\n总数量",
"plant": "Plant\n厂别",
"material": "Material\n批号",
"material_description": "Material Description\n批次说明",
"posting_date": "Posting Date\n生产时间",
"confirmation_entry_time": "Confirmation Entry Time\n确认时间",
"activity": "Activity",
"work_center": "Work Center\n生产站别",
"confirmed_yield": "Confirmed Yield (GMEIN)",
"base_unit": "Base Unit of Measure (=GMEIN)",
"confirmed_scrap": "Confirmed scrap (MEINH)\n报废数量",
"reason_for_variance": "Reason for Variance\n报废代码",
"rework": "Rework\n重工数量",
"confirmation_text": "Confirmation text",
"entered_by_user": "Entered by User",
"ind_final_confirmation": "Ind.Final Confirmation",
"milestone_confirmed": "Milestone confirmed",
"ind_delete_doc": "Ind. delete doc.",
"attr_data": {}
}
}
\ No newline at end of file
...@@ -129,7 +129,7 @@ var get_range_table = function(path){ ...@@ -129,7 +129,7 @@ var get_range_table = function(path){
first_row_data.push(sheet.getCellValue(0, k)); first_row_data.push(sheet.getCellValue(0, k));
}; };
//判断生成时间和 确认时间的列的索引位置 //判断生成时间 和 确认时间的列的索引位置
var posting_date_index = 0; var posting_date_index = 0;
var entry_time_index = 0; var entry_time_index = 0;
first_row_data.forEach(function(item, index){ first_row_data.forEach(function(item, index){
...@@ -346,7 +346,6 @@ function data_insert(iData, database_data, new_field_list){ ...@@ -346,7 +346,6 @@ function data_insert(iData, database_data, new_field_list){
var db_index = arr_primary.indexOf(item_primary_splice); var db_index = arr_primary.indexOf(item_primary_splice);
if(db_index > -1){ if(db_index > -1){
//是重复数据就判断是否需要更新
var update_obj = check_update(item, database_data[db_index], other_field); var update_obj = check_update(item, database_data[db_index], other_field);
if(Object.keys(update_obj).length > 0){ if(Object.keys(update_obj).length > 0){
//存在更新数据 //存在更新数据
...@@ -366,10 +365,10 @@ function data_insert(iData, database_data, new_field_list){ ...@@ -366,10 +365,10 @@ function data_insert(iData, database_data, new_field_list){
updateRow(item); updateRow(item);
}); });
cl.log('本次导入共更新数据:' + update_data.length + '条'); cl.log('本次导入共更新数据:' + update_data.length + '条');
cl.log('更新数据如下:'); // cl.log('更新数据如下:');
update_data.forEach(function(item){ // update_data.forEach(function(item){
cl.log(item); // cl.log(item);
}); // });
}else{ }else{
cl.log('本次导入没有更新数据'); cl.log('本次导入没有更新数据');
}; };
...@@ -398,9 +397,15 @@ function make_json_file(field_list, range_table_data){ ...@@ -398,9 +397,15 @@ function make_json_file(field_list, range_table_data){
obj[use_field_list[index]] = item obj[use_field_list[index]] = item
}); });
var jsonObj = {};
jsonObj['fileCollectionPath'] = './';
jsonObj['runnFile'] = 'flag.txt';
jsonObj['fieldMatch'] = obj;
//反序列化 //反序列化
var load_data = JSON.stringify(obj); var load_data = JSON.stringify(jsonObj);
var file = fs.openFile(config['match_field_address'], 'w'); var file = fs.openFile(config['match_field_address'], 'w');
// var file = fs.openFile('excelConfig.json', 'w');
file.write(load_data); file.write(load_data);
file.close(); file.close();
}; };
...@@ -422,7 +427,11 @@ function col_parse(col_name){ ...@@ -422,7 +427,11 @@ function col_parse(col_name){
var str = ''; var str = '';
if(col_name.indexOf('\n') != -1){ if(col_name.indexOf('\n') != -1){
str = col_name.trim().split('\n')[0]; str = col_name.trim().split('\n')[0];
}else{
//没有\n就默认 没有中文
str = col_name;
}; };
str = str.trim().toLowerCase(); str = str.trim().toLowerCase();
str_arr = str.split(' '); str_arr = str.split(' ');
...@@ -432,7 +441,7 @@ function col_parse(col_name){ ...@@ -432,7 +441,7 @@ function col_parse(col_name){
arr.push(item.trim()); arr.push(item.trim());
}; };
}); });
cl.log(arr)
str = arr.join('_'); str = arr.join('_');
str = str.split('.').join('_'); str = str.split('.').join('_');
...@@ -444,11 +453,12 @@ function add_filed(new_filed_name){ ...@@ -444,11 +453,12 @@ function add_filed(new_filed_name){
query.begin(); query.begin();
try { try {
query.execSql('alter table sec_production_order_confirmation_1 add ' + new_filed_name + ' char(64);'); query.execSql('alter table sec_production_order_confirmation_1 add ' + new_filed_name + ';');
//此处lastError报错 //此处lastError报错
// if (query.lastError().isValid()){ // if (query.lastError().isValid()){
// throw query.lastError().text(); // throw query.lastError().text();
// } // }
cl.log('新增字段:' + new_filed_name);
query.commit(); query.commit();
} }
catch (error) { catch (error) {
...@@ -493,6 +503,7 @@ function match_field(col_name_list){ ...@@ -493,6 +503,7 @@ function match_field(col_name_list){
cl.log('本次导入excel没有新增列'); cl.log('本次导入excel没有新增列');
}else{ }else{
cl.log('本次导入excel存在新增列:' + new_insert_field); cl.log('本次导入excel存在新增列:' + new_insert_field);
//若有新增列,对其进行格式化字段处理,并在数据库表中新增该字段,同时将匹配关系其更新到json文件中 //若有新增列,对其进行格式化字段处理,并在数据库表中新增该字段,同时将匹配关系其更新到json文件中
//注意:要先保证数据库字段新建成功 //注意:要先保证数据库字段新建成功
...@@ -501,7 +512,7 @@ function match_field(col_name_list){ ...@@ -501,7 +512,7 @@ function match_field(col_name_list){
var col_name = col_parse(item); var col_name = col_parse(item);
//数据库新增字段 //数据库新增字段
var new_filed_name = 'Attr_' + col_name var new_filed_name = 'attr_' + col_name
add_filed(new_filed_name); add_filed(new_filed_name);
//向json文件中更新新增字段与列名的配对关系 //向json文件中更新新增字段与列名的配对关系
...@@ -588,12 +599,12 @@ if(unread_list.length == 0){ ...@@ -588,12 +599,12 @@ if(unread_list.length == 0){
//excel表数据入库格式拼接 //excel表数据入库格式拼接
var iData = get_format_data(range_table_data); var iData = get_format_data(range_table_data);
//提取数据库中所有数据
var database_data = get_database_data('*');
//重新提取表字段(从文件中提取最新的表字段) //重新提取表字段(从文件中提取最新的表字段)
var new_field_list = Object.keys(get_field_json()); var new_field_list = Object.keys(get_field_json());
//提取数据库中所有数据
var database_data = get_database_data(new_field_list);
//数据插入操作 //数据插入操作
var flag = data_insert(iData, database_data, new_field_list); var flag = data_insert(iData, database_data, new_field_list);
...@@ -603,10 +614,12 @@ if(unread_list.length == 0){ ...@@ -603,10 +614,12 @@ if(unread_list.length == 0){
if(flag == 0){ if(flag == 0){
//说明上述操作都执行成功,移动该文件至已读文件夹 //说明上述操作都执行成功,移动该文件至已读文件夹
move_file_to_read(unread_path_list[i]); move_file_to_read(unread_path_list[i]);
cl.log('文件导入完毕,移动文件' + unread_path_list[i] + '至已读文件夹'); cl.log('文件' + unread_path_list[i] + '导入完毕');
}; };
}; };
}; };
//移除数据库连接 //移除数据库连接
db.removeConnection('MES_DB'); db.removeConnection('MES_DB');
//版本2残存的作用是为了生成版本3的配置文件
\ No newline at end of file
var fs = require('fs');
var _ = require('lodash');
var cl = require('console');
var book = require('topsin.excelxs');
var fileData = jsonRead(); //读取配置文件
var fileCollectionPath = fileData['fileCollectionPath']; //文件收集地址
var fileSavePath = fileCollectionPath + 'fileStore/';
var haveReadPath = fileSavePath + 'haveRead/';
var unReadPath = fileSavePath + 'unRead/';
var db = require('topsin.database'); //数据库连接
db.addConnection({
database_type:'pg',
database_host:'139.196.104.13:5433',
database_name:'PMO_TRAINING',
database_user:'toplinker',
database_pwd:'TopLinker0510'
}, "MES_DB");
var query = db.query('MES_DB'); //获取数据库操作对象
var runfile = fileData['runnFile'];
var result = fs.fileExists(runfile); //文件执行状态
if(result){
cl.log('脚本正在运行,请稍后再试');
}else{
checkFilePath(); //需求目录检测
var file = fs.openFile(runfile, 'w'); //程序开始时生成执行状态文件
file.write('running');
file.close();
var fileListInfo = fs.listDir(fileCollectionPath, 1); //获取目录下文件信息
var excelList = arrExcel(fileListInfo); //获取目录下所有excel文件列表
var haveReadInfo = fs.listDir(haveReadPath, 1); //获取已读目录下文件信息
var haveReadList = arrExcel(haveReadInfo);
var res1 = excelList.map(function(item){
return 'confirmation' + item.split('confirmation')[1];
});
var res2 = haveReadList.map(function(item){
return 'confirmation' + item.split('confirmation')[1];
});
var unReadMoveList = _.difference(res1, res2) //获取目录下未被读取到过的excel文件
unReadMoveList.forEach(function(item){ //复制未读excel文件到未读目录下
var iSrc = fileCollectionPath + item;
var iDest = unReadPath + item;
fs.copyFile(iSrc, iDest ,true);
});
var unReadInfo = fs.listDir(unReadPath, 1); //获取未读目录下文件信息
var unReadList = arrExcel(unReadInfo);
if(unReadList.length == 0){
cl.log('暂无待导入的excel文件...');
}else{
for(var i=0; i<unReadList.length; i++){
cl.log('正在导入文件' + unReadList[i]);
var nowfileData = jsonRead(); //重新获取配置文件
var tableData = getRangeTable(unReadList[i], nowfileData); //获取excel文件数据
var iData = getFormatData(tableData); //excel表数据入库格式拼接
var newfileData = jsonRead(); //获取最新的配置文件信息
var newfieldMatch = newfileData['fieldMatch'];
var newfieldKeys = _.keys(newfieldMatch); //获取最新的表字段
var dbData = getDbData(newfieldKeys); //获取当前数据库对应写入字段的所有数据
var flag = dataInsert(iData, dbData, newfieldKeys);
if(flag == 0){ //说明上述操作都执行成功,移动该文件至已读文件夹
var movePath = haveReadPath + 'confirmation' + unReadList[i].split('confirmation')[1];
fs.rename(unReadList[i], movePath);
cl.log('文件' + unReadList[i] + '导入完毕');
};
};
};
fs.unlink(runfile); //程序运行结束删除执行状态文件
};
function checkFilePath(){ //自定义目录检测
if(fs.dirExists(fileSavePath) == false){
fs.mkdir(fileSavePath);
};
if(fs.dirExists(haveReadPath) == false){
fs.mkdir(haveReadPath);
};
if(fs.dirExists(unReadPath) == false){
fs.mkdir(unReadPath);
};
};
function jsonRead(){ //读取json文件
try {
var file = fs.openFile('excelConfig.json');
var data = JSON.parse(file.readAll());
return data;
} catch (error) {
cl.log(error);
};
};
function jsonWrite(data){ //写入json文件
var loadData = JSON.stringify(data);
var file = fs.openFile('excelConfig.json', 'w');
file.write(loadData);
file.close();
};
function arrExcel(fileList){ //提取excel路径
var list = [];
_.filter(fileList, function(o){
if(o['baseName'].indexOf('confirmation') > -1 ){
list.push(_.at(o, 'path')[0]);
}
});
return list;
};
function getRangeTable(path, fileData){ //获取excel表的数据
try {
book.open(path);
var tableData = [];
var sheet = book.getActiveSheet(); //获取当前表
var countCol = sheet.getLastCol(); //当前表总列数
var countRow = sheet.getLastRow(); //当前表总行数
var firstRow = []; //提取第一行数据
for(var k=0; k<countCol; k++){
firstRow.push(sheet.getCellValue(0, k));
};
var fieldMatch = fileData['fieldMatch']; //读取字段匹配信息
var filedValues = _.values(fieldMatch);
var newRowlList1 = _.difference(firstRow, filedValues); //与固定字段作对比
if(newRowlList1.length != 0){
var attrDataValues = _.values(fieldMatch['attr_data']);
var newRowlList2 = _.difference(newRowlList1, attrDataValues); //与之前的新增字段作对比
if(newRowlList2.length != 0){ //存在新增字段
cl.log('新增列名为:',newRowlList2);
var parseFielList = newRowlList2.map(function(item){ // 解析新增列名为合适的字段名
return colParse(item);
});
parseFielList.forEach(function(item, index){ //将新增字段名和对应的列名存入配置文件
fieldMatch['attr_data'][item] = newRowlList2[index];
});
cl.log('新增字段为:',parseFielList);
jsonWrite(fileData); //将更新的字段信息写入配置文件
};
};
var postDateIndex = 0;
var entryTimeIndex = 0;
firstRow.forEach(function(item, index){ //判断生成时间 和 确认时间的列的索引位置
//模糊匹配不严谨
if(item.search('Posting Date') != -1 || item.search('生产时间') != -1){
postDateIndex = index;
};
if(item.search('Confirmation Entry Time') != -1 || item.search('确认时间') != -1){
entryTimeIndex = index;
};
});
//表数据写入数组
for(var i=0; i<countRow; i++){
var arry = [];
for(var j=0; j<countCol; j++){ //如果列被改变,那么要考虑需要转变时间格式的两列还是不是日期格式的那两列
if(i != 0 && j == postDateIndex){
arry.push(formatDate(sheet.getCellValue(i, j), '-'));
}else if(i != 0 && j == entryTimeIndex){
arry.push(formatTime(sheet.getCellValue(i, j), ':'));
}else{
arry.push(sheet.getCellValue(i, j));
};
};
tableData.push(arry);
};
book.close();
return tableData;
} catch (error) {
cl.log(error);
};
};
function colParse(colName){ //列名解析成合适的字段名(根据之前的列名来判断)
var str = '';
if(colName.indexOf('\n') != -1){
str = colName.trim().split('\n')[0];
}else{
str = colName; //没有 \n 就默认 没有中文
};
str = str.trim().toLowerCase();
strArr = str.split(' ');
var arr = [];
strArr.forEach(function(item){
if(item != '' && item[0] != '(' && item[-1] != ')'){
arr.push(item.trim());
};
});
str = arr.join('_');
str = str.split('.').join('_');
return str;
};
function formatDate(numb, format) { //对年月日进行格式化处理
if (numb != undefined) {
var time = new Date((numb - 1) * 24 * 3600000 + 1);
time.setYear(time.getFullYear() - 70);
var year = time.getFullYear() + '';
var month = time.getMonth() + 1 + '';
var date = time.getDate() + '';
if (format && format.length === 1) {
return year + format + (month < 10 ? '0' + month : month) + format + (date < 10 ? '0' + date : date);
};
return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date);
} else {
return undefined;
};
};
function formatTime(numb, format) { //对时分秒进行格式化处理
var hourTmp = numb * 24;
var hour = Math.floor(hourTmp);
var minuteTmp = hourTmp - hour;
var minute = Math.floor(minuteTmp * 60);
var secondTmp = (minuteTmp * 60) - minute;
var second = Math.round(secondTmp * 60);
if(second == 60){
second = 0;
minute = minute + 1;
};
if(minute == 60){
minute = 0;
hour = hour + 1;
};
return (hour < 10 ? '0' + hour : hour) + format + (minute < 10 ? '0' + minute : minute) + format + (second < 10 ? '0' + second : second);
};
function getFormatData(tableData){
var batchData = [];
var colNameList = tableData[0]; //获取excel第一行列名
var fileData = jsonRead(); //获取最新的配置文件信息
var fieldMatch = fileData['fieldMatch'];
var fieldKeys = _.keys(fieldMatch);
var fieldValues = _.values(fieldMatch);
var attrDataKeys = _.keys(fieldMatch['attr_data']); //获取新增字段的键值
var attrDataValues = _.values(fieldMatch['attr_data']);
var importData = _.drop(tableData); //删除第一行列名
importData.forEach(function(item1){ //处理批量插入的数据格式
var obj = {};
item1.forEach(function(item2, index2){ //获取字段对应列名的实际索引来得到字段的value值
if(fieldKeys[index2] == 'attr_data'){ //获取新增字段对应的数据
//待测试:如果没有新增字段,是插入的数据是没有attr_data的,因为索引长度不够
var jsonObj = {};
attrDataKeys.forEach(function(item3, index3){
jsonObj[item3] = item1[colNameList.indexOf(attrDataValues[index3])];
});
obj[fieldKeys[index2]] = jsonObj; //先不转成joson,因为后面要进行更新对比
}else{
obj[fieldKeys[index2]] = item1[colNameList.indexOf(fieldValues[index2])];
};
});
batchData.push(obj);
});
return batchData;
};
function getDbData(fieldList){
query.begin(); //事务
try {
var data = query.selectArrayMap({
table:'sec_production_order_confirmation_1',
field: fieldList,
field_format:{"tags":'array', attr_data:'json'}
});
// if (query.lastError().isValid()){ //此处lastError报错
// throw query.lastError().text();
// }
query.commit();
}
catch (error) {
query.rollback();
};
return data;
};
function dataInsert(iData, dbData, fieldList){
var flag = 0;
var primaryField = ['confirmation', 'plant', 'material', 'posting_date', 'confirmation_entry_time', 'activity']
var otherField = _.difference(fieldList, primaryField);
cl.log('当前数据库表中已存在数据:' + dbData.length + '条');
if(dbData.length == 0){ //首次批量插入数据
batchInsert(iData, fieldList);
cl.log('本次导入共插入数据:' + iData.length + '条')
}else{
var batchInsertData = []; //待批量插入的数据
var updateData = []; //待更新的数据
var arrPrimary = [];
dbData.forEach(function(item){ //主键对应值拼接(用于比对插入数据是否重复)
arrPrimary.push(getValueSplice(item, primaryField));
});
iData.forEach(function(item){
var itemPrimarySplice = getValueSplice(item, primaryField); //待插入数据的主键值拼接
var dbIndex = arrPrimary.indexOf(itemPrimarySplice);
if(dbIndex > -1){
var updateObj = checkUpdate(item, dbData[dbIndex], otherField); //检查是否存在更新数据
if(Object.keys(updateObj).length > 0){ //说明存在更新数据
var updateInfo = {};
updateInfo['iData'] = updateObj;
updateInfo['iWhere'] = getPrimaryData(item, primaryField);
updateData.push(updateInfo);
};
}else{
batchInsertData.push(item); //不是重复数据就直接插入
};
});
if(updateData.length != 0){
updateData.forEach(function(item){
updateRow(item);
});
cl.log('本次导入共更新数据:' + updateData.length + '条');
// cl.log('更新数据如下:');
// updateData.forEach(function(item){
// cl.log(item);
// });
}else{
cl.log('本次导入没有更新数据');
};
if(batchInsertData.length != 0){
batchInsert(batchInsertData, fieldList);
cl.log('本次共导入新增数据:' + batchInsertData.length + '条');
// cl.log('插入数据如下:');
// batchInsertData.forEach(function(item){
// cl.log(item);
// });
}else{
cl.log('本次导入没有新增数据');
};
};
return flag;
};
function batchInsert(data, fieldList){ //批量插入数据
var iData = [];
data.forEach(function(item){ //attr_data字段值要转成json字符串
if('attr_data' in item){
jsonStr = JSON.stringify(item['attr_data']);
item['attr_data'] = jsonStr
iData.push(item);
}else{
iData.push(item);
};
});
// iData.forEach(function(item){
// cl.log(item)
// });
query.begin(); //事务
try {
query.batchInsert(
'sec_production_order_confirmation_1',
fieldList,
iData
);
// if (query.lastError().isValid()){ //此处lastError报错
// throw query.lastError().text();
// }
query.commit();
}
catch (error) {
cl.log('批量插入数据报错:' + error);
query.rollback();
};
};
function getValueSplice(obj, primaryField){ //主键字段值拼接
var str = '';
primaryField.forEach(function(item){
str += obj[item];
});
return str;
};
function checkUpdate(insertData, dbData, otherField){ //检查是否需要更新
var obj = {};
otherField.forEach(function(item){
if(item == 'attr_data'){
var equal = _.isEqual(insertData[item], dbData[item]);
if(equal == false){
obj[item] = insertData[item];
};
}else{
if(insertData[item] != dbData[item]){
obj[item] = insertData[item];
};
};
});
return obj;
};
function getPrimaryData(data ,primaryField){ //获取主键数据对象,用于更新数据时找到要更新的记录
var obj = {};
primaryField.forEach(function(item){
obj[item] = data[item];
});
return obj;
};
function updateRow(updateData){ //更新数据
iData = updateData['iData'];
iWhere = updateData['iWhere'];
query.begin(); //事务
try {
query.updateRow({
table:'sec_production_order_confirmation_1',
data: iData,
where:iWhere,
update_policy:{tags:'array_append', attr_data:'json_merge'}
});
//此处lastError报错
// if (query.lastError().isValid()){
// throw query.lastError().text();
// }
query.commit();
}
catch (error) {
cl.log('更新数据报错:' + error);
query.rollback();
};
};
db.removeConnection('MES_DB'); //移除数据库连接
// 待扩展
// 1、插入字段的数据类型约束
// 2、日志文件
// 3、邮件告警
//注意事项
// 1、新增列名不能为空,否则新增字段会为显示为空并更新数据库
// 2、新增列名不能重名,否则会与之前的列名数据冲突
// 3、若手动修改配置文件中的新增字段名,则会在数据库中新增该字段信息,
// 且其数据取该字段对应的列名的数据,若被修改的字段与其对应的列名在分别在数据库中都不存在,
// 则会更新整体新增字段,虽然数据不会变,但是没有意义,不要这样修改
{"fileCollectionPath":"./","runnFile":"flag.txt","fieldMatch":{"order_no":"Order\n生管投料单号","confirmation":"Confirmation\n总数量","plant":"Plant\n厂别","material":"Material\n批号","material_description":"Material Description\n批次说明","posting_date":"Posting Date\n生产时间","confirmation_entry_time":"Confirmation Entry Time\n确认时间","activity":"Activity","work_center":"Work Center\n生产站别","confirmed_yield":"Confirmed Yield (GMEIN)","base_unit":"Base Unit of Measure (=GMEIN)","confirmed_scrap":"Confirmed scrap (MEINH)\n报废数量","reason_for_variance":"Reason for Variance\n报废代码","rework":"Rework\n重工数量","confirmation_text":"Confirmation text","entered_by_user":"Entered by User","ind_final_confirmation":"Ind.Final Confirmation","milestone_confirmed":"Milestone confirmed","ind_delete_doc":"Ind. delete doc.","attr_data":{"test3":"test3","test1":"test1","test2":"test2","":""}}}
\ No newline at end of file
背景:项目需要执行一个定时任务,每隔一段时间读取一个excel中的数据并写入到数据库中;
excel文件客户会不定期导入到固定的共享路径中。
1. 数据库连接地址:pg 139.196.104.13:5433 PMO_TRAINING toplinker TopLinker0510
2. 数据表:sec_production_order_confirmation_1,sec_production_order_confirmation_2(每个人用1张表)
3. excel中的列后续可能会调整,程序中需考虑后续便于扩展修改
注意事项:
1、存储文件和sql操作要捕获异常
2、用联合主键控制唯一来进行去重
3、高频运作脚本时需要考虑数据冲突问题
4、其他扩展:传入数据到DB时的字段类型限制、日志、邮件
5、关注时间格式问题:若分钟和秒达到60了,看是否会进一
\ No newline at end of file
{"order_no":"Order\n生管投料单号","confirmation":"Confirmation\n总数量","plant":"Plant\n厂别","material":"Material\n批号","material_description":"Material Description\n批次说明","posting_date":"Posting Date\n生产时间","confirmation_entry_time":"Confirmation Entry Time\n确认时间","activity":"Activity","work_center":"Work Center\n生产站别","confirmed_yield":"Confirmed Yield (GMEIN)","base_unit":"Base Unit of Measure (=GMEIN)","confirmed_scrap":"Confirmed scrap (MEINH)\n报废数量","reason_for_variance":"Reason for Variance\n报废代码","rework":"Rework\n重工数量","confirmation_text":"Confirmation text","entered_by_user":"Entered by User","ind_final_confirmation":"Ind.Final Confirmation","milestone_confirmed":"Milestone confirmed","ind_delete_doc":"Ind. delete doc."}
\ No newline at end of file
it is a test file
\ No newline at end of file
这是一个测试文件
123123123123
圣诞快乐
\ No newline at end of file
aaaaaaaaaaaaaa
\ No newline at end of file
哈哈哈哈哈哈哈哈哈
\ No newline at end of file
var _ = require('lodash');
var cl = require('console');
// print(_.toString(null));//=>空字符
// print(_.toString(undefined));//=>空字符
// print(_.toString('abcd'));//=>abcd
// print(_.toString(true));//=>true
// print(typeof _.toString(true));//string
// print(_.toString(false));//=>false
// print(typeof _.toString(false));//string
// print(_.toString(0.01));//=>0.01
// print(typeof _.toString(0.01));//string
// print(_.toString({a:'aa'}));//=>{"a":"aa"}
// print(typeof _.toString({a:'aa'}));//string
// print(_.toString([0,'a',0.11]))//=>[0,"aa",0.11]
// print(typeof _.toString([0,'a',0.11]));//string
// print(_.toString(function(){return 'abcdef';}));//=>function(){return 'abcdef';}
// print(typeof _.toString(function(){return 'abcdef';}));//string
//=>以下打印true
// print(_.isEmpty(null));
// print(_.isEmpty(undefined));
// print(_.isEmpty(''));
// print(_.isEmpty([]));
// print(_.isEmpty({}));
//=>以下打印false
// print(_.isEmpty(['a']));
// print(_.isEmpty({a:1}));
// print(_.isEmpty(false));
// print(_.isEmpty(true));
// print(_.isEmpty(0));
// print(_.isEmpty('abc'));
// print(_.isEmpty(0.1234));
// var n = _.toNumber(undefined); //=>0
// print(n);
//=>以下打印false
// print(_.isValid(null));
// print(_.isValid(undefined));
//=>以下打印true
// print(_.isValid(true));
// print(_.isValid(0));
// print(_.isValid('abc'));
// print(_.isValid(0.1234));
// print(_.isValid(false));
// print(_.isValid([]));
// print(_.isValid({}));
//=>以下打印false
// print(_.isTrue(null));
// print(_.isTrue(undefined));
// print(_.isTrue(''));
// print(_.isTrue([]));
// print(_.isTrue({}));
// print(_.isTrue(false));
// print(_.isTrue(0));
// print(_.isTrue('0'));
//=>以下打印true
// print(_.isTrue(['a']));
// print(_.isTrue({a:1}));
// print(_.isTrue(true));
// print(_.isTrue(1));
// print(_.isTrue('abc'));
// print(_.isEmpty(0.1234));
// var template = "your {0} is {1}";
// var res = _.format(template, "name", 'huzhihui ');
// print(res);
// var res = _.formatUnicorn("${name} is ${age} old", {name: 'huzhihui', age: '24'});
// print(res);
// lodash的所有函数都不会在原有的数据上进行操作,而是复制出一个新的数据而不改变原有数据
// var cl = require('console');
// s = ['a', 'b', 'c', 'd', 'e', 'f'];
// cl.log(_.chunk(s, 1));
// cl.log(_.chunk(s, 2));
// cl.log(_.chunk(s, 3));
// cl.log(_.chunk(s, 4));
// 使用lodash来遍历
// arr = ['a', 'b', 'c', 'd', 'e', 'f'];
// var arr1 = _.forEach(arr, function(item, index){
// // arr[index] = item + '-';
// if(index = 1){
// };
// });
// cl.log(arr1);
// cl.log(_.first(arr));
// cl.log(_.drop(arr));
// cl.log(_.drop(arr, 2));
// cl.log(_.drop(arr, 3));
// var a = _.compact([0, 1, false, 2, '', 3]);
// cl.log(a);
// cl.log(_.fill([4, 6, 8, 10, 1, 2, 3, 23, 23], '*', 1, 6));//替换,但范围是顾头不顾尾
arr = {'a': 1, 'b': '2', 'c': 3};
cl.log(_.pick(arr, ['a']))
cl.log(arr)
var object = { 'a': 1, 'b': '2', 'c': 3 };
cl.log(_.pickBy(object, _.isNumber));
cl.log(object)
// var users = {
// 'barney': { 'age': 36, 'active': true },
// 'fred': { 'age': 40, 'active': false },
// 'pebbles': { 'age': 1, 'active': true }
// };
// cl.log(_.findKey(users, function(o) { return o.age < 40; }));
// cl.log(_.findKey(users, { 'age': 1, 'active': true }));
// cl.log(_.findKey(users, ['active', false]));
// cl.log(_.findKey(users, 'active'));
// arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
// cl.log(_.filter(arr, function(o){
// return o > 3;
// }));
arr = ['confirmation123', 'aaa', 'con', 'bbb', 'confirmation1231312']
cl.log(_.filter(arr, function(o){
if(_.includes(o, 'con')){
return o;
};
}));
{"order_no":"Order\n生管投料单号","confirmation":"Confirmation\n总数量","plant":"Plant\n厂别","material":"Material\n批号","material_description":"Material Description\n批次说明","posting_date":"Posting Date\n生产时间","confirmation_entry_time":"Confirmation Entry Time\n确认时间","activity":"Activity","work_center":"Work Center\n生产站别","confirmed_yield":"Confirmed Yield (GMEIN)","base_unit":"Base Unit of Measure (=GMEIN)","confirmed_scrap":"Confirmed scrap (MEINH)\n报废数量","reason_for_variance":"Reason for Variance\n报废代码","rework":"Rework\n重工数量","confirmation_text":"Confirmation text","entered_by_user":"Entered by User","ind_final_confirmation":"Ind.Final Confirmation","milestone_confirmed":"Milestone confirmed","ind_delete_doc":"Ind. delete doc."}
\ No newline at end of file
File deleted
var fs = require('fs');
var cl = require('console');
var _ = require('lodash');
var crypto = require('topsin.crypto');
// var filelist = fs.listDir('./', 1);
// print('当前共有文件个数:' + filelist.length);
// //查看当前目录下的文件
// var get_current_files = function(){
// var current_files = [];
// for(var i=0; i<filelist.length; i++){
// current_files.push(_.toString(filelist[i]['baseName']));
// };
// return current_files;
// }
// var current_files = get_current_files();
// cl.log(current_files);
// //查看所有的csv文件
// var get_confirmation_files = function(current_files){
// var confirmation_file = [];
// current_files.forEach(function(item){
// if(item.search('confirmation') != -1){
// confirmation_file.push(item);
// }
// });
// return confirmation_file;
// }
// var confirmation_files = get_confirmation_files(current_files);
// cl.log(confirmation_files);
// //使用md5标记一个文件被查看过多少次
// var file_check = function(current_files){
// //查看当前是否存在csv_data.json文件,如果有就读取,没有就创建
// // if(current_files.indexOf('csv_data') == -1){
// // cl.log('不存在文件csv_data');
// // };
// //cav_data初始化
// if(fs.fileExists('./csv_data.json') == false){
// cl.log('不存在文件csv_data');
// }
// };
// file_check(current_files);
// a = [1, 2, 3, 4];
// b = [1, 2, 3, 4];
// if(
// a[0] == b[0] &&
// a[1] == b[1] &&
// a[2] == b[2] &&
// a[3] == b[3]
// ){
// cl.log('有重复数据');
// }else{
// cl.log('没有重复数据');
// };
// s = {'name':'huzhihui', 'age':23}
// var str = '';
// Object.keys(s).forEach(function(item){
// str += s[item];
// });
// cl.log(str);
// 任务描述:
// 定义两个函数,一个名为stringZip(iStr),一个名为stringUnzip();
// stringZip(iStr),输入为a-z,A-Z的字符串,且字符串不为空,如aaabccccccddeee,输出压缩后的字符串,如3a1b6c2d3e
// stringUnzip(iStr),逻辑与stringZip(iStr)相反
var cl = require('console');
var _ = require('lodash');
var reg = new RegExp('^[a-zA-Z]+$');
function stringZip(iStr){
if((typeof iStr == 'string') && iStr != '' && reg.test(iStr)){
var count = 1; //计数
var str = '';
var index = 1; //被比较字符的索引
var nextChar = iStr[0];
if(iStr.length == index){
return count+iStr;
};
while(index < iStr.length){
if(iStr[index] != nextChar){
str += (count + nextChar);
count = 1;
nextChar = iStr[index];
}else{
count += 1;
};
index += 1;
};
str += (count + nextChar);
return str;
}else{
cl.log('不是字符串 或 字符串为空 或 不是纯英文字母');
};
};
function stringUnzip(iStr){
var str1 = '';
for(var i=0; i<iStr.length; i+=2){
str2 = '';
var n = _.toNumber(iStr[i]);
for(var j=0; j<n; j++){
str1 += iStr[i+1];
};
str2 += str1;
};
return str1;
};
a = 'aaanaaanbbnnnbbbbbbbbbbsssssss';
cl.log(stringZip(a));
b = '3a5b4c7d9f';
cl.log(stringUnzip(b));
//扩展:效率问题
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment