今天突然接到一个工作,要把两个存储在.xls的主体信息表,除重后合并成一个主体信息表,并且补充主体类型和所在县区这两列信息。
完成这项工作的方法有很多,如果信息表中的信息量不大的话,手工处理一下也行,如果信息量大的话,还是由电脑来处理比较好,比如用vba写代码来合并,不过我更喜欢用JavaScript来完成。
用HTML设计页面,放置两个textarea
其中一个textarea(ID=ta1)用来存放要处理的原始企业信息
另一个textarea(ID=ta2)用来存放处理好的企业信息。
代码如下:
- html>
- <html>
-
- <head>
- <title>文本筛选合并title>
- <style type="text/css">
- * {
- padding: 0px;
- margin: 0px;
- }
-
- html,
- body {
- overflow-y: hidden;
- }
-
- .ta_div {
- border: 1px solid #CCCCCC;
- overflow: auto;
- position: relative;
- }
-
- .ta_div textarea {
- resize: none;
- background: none repeat scroll 0 0 transparent;
- border: 1 solid black;
- width: 99%;
- height: 200px;
- overflow-y: scroll;
- //position: absolute;
- left: 0px;
- top: 0px;
- z-index: 2;
- font-size: 18px;
- white-space: pre-wrap;
- word-wrap: break-word;
- word-break: break-all;
- }
-
- p {text-align:center; color:purple}
- style>
- head>
-
- <body>
- <table style="width:100%; borderspacing:0; cellpadding:0;">
- <tr>
- <td style="width:49%">
- <div class="ta_div">
- <p>原始数据p>
- <textarea id="ta1" onscroll="ta1_scroll()" oninput="t()" onpropertychange="t()">请输入原始数据textarea>
- div>
- td>
- <td style="width:49%">
- <div class="ta_div">
- <p>整理后的数据p>
- <textarea id="ta2" onscroll="ta2_scroll()" oninput="textchange()" onpropertychange="textchange()">合并后的数据textarea>
- div>
- td>
- tr>
- table>
-
- body>
- html>
- var v = ta1.value.trim();//去除首尾空格
- v = v.replace(/\"/g, ""); //去除半角双引号
- v = v.replace(/\n[\s]*\n/g, "\n"); //去除多余的\n,避免空行
var a = v.split('\n');
var b = Array.from(new Set([...a]));
- function addType(n)
- {
- //补充类型
- var a = ['合作社','家庭农场','公司'];
- var j;
- for (j=0; j < a.length-1; j++ )
- {
- if (-1 != n.indexOf(a[j]))
- {
- break;
- }
- }
- return '\t' + a[j];
- } //addType()
-
-
-
- function addArea(n)
- {
- //补充所在县(区)
- var a = ['金城江','宜州','罗城','环江','南丹','天峨','东兰','巴马','凤山','都安','大化','*未知'];
- var j;
- for (j=0; j < a.length-1; j++ )
- {
- if (-1 != n.indexOf(a[j]))
- {
- break;
- }
- }
- return '\t' + a[j];
- } //addArea()
-
-
-
- function addData(a)
- {
- //追加信息
- for (var i=0; i < a.length; i++)
- {
- a[i] += addType(a[i]); //补充类型
- a[i] += addArea(a[i]); //补充所在县(区)
- }
- } //addData(a)
document.getElementById("ta2").value = b.join('\n');
- <html>
-
- <head>
- <title>文本筛选合并title>
- <style type="text/css">
- * {
- padding: 0px;
- margin: 0px;
- }
-
- html,
- body {
- overflow-y: hidden;
- }
-
- .ta_div {
- border: 1px solid #CCCCCC;
- overflow: auto;
- position: relative;
- }
-
- .ta_div textarea {
- resize: none;
- background: none repeat scroll 0 0 transparent;
- border: 0 none;
- width: 100%;
- height: 200px;
- overflow-y: scroll;
- //position: absolute;
- left: 0px;
- top: 0px;
- z-index: 2;
- font-size: 18px;
- white-space: pre-wrap;
- word-wrap: break-word;
- word-break: break-all;
- }
-
- p {text-align:center; color:purple}
- style>
- head>
-
- <body>
- <table style="width:100%">
- <tr>
- <td style="width:50%">
- <div class="ta_div">
- <p>原始数据p>
- <textarea id="ta1" onscroll="ta1_scroll()" oninput="t()" onpropertychange="t()">请输入原始数据textarea>
- div>
- td>
- <td style="width:50%">
- <div class="ta_div">
- <p>整理后的数据p>
- <textarea id="ta2" onscroll="ta2_scroll()" oninput="textchange()" onpropertychange="textchange()">合并后的数据textarea>
- div>
- td>
- tr>
- table>
- <script>
-
- String.prototype.trim = function()
- {
- //功能:去除首尾空格
- return this.replace(/(^\s*)|(\s*$)/g, "");
- /*var t = this.replace(/(^\s*)|(\s*$)/g, "");
- return t =t.replace(/(^ *)|( *$)/g, ""); */
- }
-
- var ta1 = document.getElementById("ta1");
-
- function t()
- {
- var v = ta1.value.trim();
- if ('' == v)
- {
- alert('请先输入原始数据');
- }
- v = v.replacereplace(/\"/g, ""); //去除半角双引号
- //v = v.replace('','\n'
- a = v.split('\n');
- b = Array.from(new Set([...a]));
- document.getElementById("ta2").value = b.join('\n');
- }
- script>
- body>
- html>