Modify some code and add .env config.

这个提交包含在:
maisui 2023-05-20 00:20:37 +08:00
父节点 17e1574809
当前提交 abac204a5a
共有 6 个文件被更改,包括 155 次插入27 次删除

0
.env.example 普通文件
查看文件

167
app.js
查看文件

@ -1,38 +1,155 @@
const config = require('dotenv').config();
if (config.error) {
throw config.error
}
//console.log(config.parsed);
//console.log(process.env);
//console.log(process.env.TOKEN);
const ExcelJS = require('exceljs');
const workbook = new ExcelJS.Workbook();
//https://github.com/exceljs/exceljs/blob/master/README_zh.md
var axios = require("axios").default;
var token= 'xc-token'
var options = {};
var pool = {};
var options = {
options.nocodb = {
method: 'GET',
url: 'https://nocodb.example.com',
url: process.env.REQUEST_URL,
params: { offset: '0', limit: '1000', where: '' , 'nested[人口信息 List][fields]':'*'},
headers: {
'xc-token': token
}
headers: {
'xc-token' : process.env.API_TOKEN
}
};
options.seatable = {
method: 'GET',
url: 'https://cloud.seatable.cn/api/v2.1/dtable/app-access-token/',
headers: {
'Authorization' : 'Token '+process.env.API_TOKEN
}
}
function send(options, callback) {
sendRequest(options[process.env.TABLE_APP],function(response){
if(process.env.TABLE_APP == 'nocodb'){
//将数据存放于数据池内
pool=response
//nocodb数据整理输出
for(var i=0 ; i < response.pageInfo.totalRows ; i++){
now=response.list[i];
replaceExcel(now);
//console.log(pool);
}
}
console.log(response);
});
//替换Excel
async function replaceExcel(data){
//console.log(data);
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.readFile('./pop_template.xlsx');
// 按 id 提取工作表
const worksheet = workbook.getWorksheet(1);
//填充表格内容
//填充表头以及产权信息
worksheet.getCell('G2').value=data['栋']+" 栋 "+data['单元']+" 单元 "+data['楼层']+" 楼 "+data['房号']+" 号";
worksheet.getCell('A6').value=data['面积'];
worksheet.getCell('C6').value=data['产权人姓名'];
worksheet.getCell('D6').value=data['产权人电话'];
switch(data['房屋类型'])
{
case '自用':
worksheet.getCell('E6').value='√';
break;
case '租用':
worksheet.getCell('F6').value='√';
break;
case '借用':
worksheet.getCell('G6').value='√';
break;
case '闲置':
worksheet.getCell('H6').value='√';
break;
default:
}
switch(data['房屋用途'])
{
case '居住':
worksheet.getCell('I6').value='√';
break;
case '办公':
worksheet.getCell('J6').value='√';
break;
case '经营':
worksheet.getCell('K6').value='√';
break;
case '仓储':
worksheet.getCell('L6').value='√';
break;
default:
}
worksheet.getCell('N6').value=data['独居老人紧急联系人姓名'];
worksheet.getCell('O6').value=data['独居老人紧急联系人电话'];
//填充人口信息
var registered_population=9;
var transient_population=22;
for(people=0;people<data['人口信息 List'].length;people++){
//console.log(data['人口信息 List'][people]);
single=data['人口信息 List'][people]
if(single['流动人口']=='是'){
//流动人口
worksheet.getCell('B'+transient_population).value=single['姓名'];
worksheet.getCell('D'+transient_population).value=single['身份证'];
worksheet.getCell('E'+transient_population).value=single['手机号'];
worksheet.getCell('H'+transient_population).value=single['民族'];
//性别
gender='';
if(single['身份证']!=null){
gender=single['身份证'].substr(16,1)%2 ? '男': '女'
}
worksheet.getCell('I'+transient_population).value=gender;
worksheet.getCell('J'+transient_population).value=single['政治面貌'];
worksheet.getCell('K'+transient_population).value=single['婚姻关系'];
worksheet.getCell('L'+transient_population).value=single['学历'];
worksheet.getCell('M'+transient_population).value=single['职业类型'];
worksheet.getCell('O'+transient_population).value=single['城乡标识']=='农村' ? '农业':'非农业';
worksheet.getCell('Q'+transient_population).value=single['重点人群']=='是' ? '是':'否'; //重点人群
transient_population ++;
}else{
//户籍人口
worksheet.getCell('B'+registered_population).value=single['姓名'];
worksheet.getCell('D'+registered_population).value=single['身份证'];
worksheet.getCell('E'+registered_population).value=single['手机号'];
worksheet.getCell('G'+registered_population).value=single['民族'];
//性别
gender='';
if(single['身份证']!=null){
gender=single['身份证'].substr(16,1)%2 ? '男': '女'
}
worksheet.getCell('H'+registered_population).value=gender;
worksheet.getCell('I'+registered_population).value=single['政治面貌'];
worksheet.getCell('J'+registered_population).value=single['婚姻关系'];
worksheet.getCell('K'+registered_population).value=single['学历'];
worksheet.getCell('L'+registered_population).value=single['职业类型'];
worksheet.getCell('N'+registered_population).value=single['城乡标识']=='农村' ? '农业':'非农业';
worksheet.getCell('O'+registered_population).value=single['重点人群']=='是' ? '是':'否'; //特殊医疗需求
worksheet.getCell('Q'+registered_population).value=single['人户一致'];
registered_population ++;
}
//填充表头居住人口
worksheet.getCell('M6').value=people+1;
}
//保存表格
await workbook.xlsx.writeFile(process.env.EXPORT_DIR+"/"+data['栋']+"-"+data['单元']+"-"+data['楼层']+"-"+data['房号']+".xlsx");
}
//请求方法
function sendRequest(options, callback) {
axios.request(options).then(function (response) {
//console.log(response.data);
return callback(response.data);
callback(response.data);
}).catch(function (error) {
console.error(error);
});
}
send(options,function(response){
//console.log(response.pageInfo.totalRows)
//每间房屋
for(var i=0 ; i < response.pageInfo.totalRows ; i++){
let now=response.list[i];
//console.log(now);
//console.log(now['人口信息 List'].length)
//房屋里的每个人
for(a=0;a<now['人口信息 List'].length;a++){
console.log(now['人口信息 List'][a]);
}
}
});
}

1
export/ReadMe.txt 普通文件
查看文件

@ -0,0 +1 @@
这是默认输出文件夹,如需要修改为其他文件夹,请先创建好相关路径再修改 .env 中的 EXPORT_DIR 配置。

13
package-lock.json 自动生成的
查看文件

@ -1,15 +1,16 @@
{
"name": "api2excel",
"name": "dld2excel",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "api2excel",
"name": "dld2excel",
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"axios": "^1.4.0",
"dotenv": "^16.0.3",
"exceljs": "^4.3.0",
"request": "^2.88.2"
}
@ -395,6 +396,14 @@
"node": ">=0.4.0"
}
},
"node_modules/dotenv": {
"version": "16.0.3",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz",
"integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==",
"engines": {
"node": ">=12"
}
},
"node_modules/duplexer2": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/duplexer2/-/duplexer2-0.1.4.tgz",

查看文件

@ -15,6 +15,7 @@
"license": "MIT",
"dependencies": {
"axios": "^1.4.0",
"dotenv": "^16.0.3",
"exceljs": "^4.3.0",
"request": "^2.88.2"
}

二进制
pop_template.xlsx 普通文件

二进制文件未显示。