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');
\ No newline at end of file
//版本2残存的作用是为了生成版本3的配置文件
\ No newline at end of file
This diff is collapsed.
{"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