我正在使用“ xlsx.full.min.js”从电子表格读取数据,将数据放入表中,然后验证内容。第4列必须是无符号整数,因此我正在使用“ RegExp(” ^ \ d + $“)”对此进行测试。它总是返回false。
另一个问题是我输入的日期是dd / mm / yyyy(例如02/12/2018)。但是,导入日期为m / d / yy(例如12/2/18)。
代码位于:https://jsfiddle.net/Glyndwr/6z1ckv8y/2/
样本输入为: “ Paroid”,“ Marvin”,“ 01/09/1978”,“ 42”,“ 01/12/2018”
代码为(因为此站点需要它):
html:
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.14.0/xlsx.full.min.js"></script>
<style type="text/css">
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #f5f5f5;
}
</style>
</head>
<body>
<div id="wrapper">
<input type="file" id="input-excel" />
</div>
<script src="js/youthMemberBulkImport.js"></script>
</body>
</html>
js:
$(document).ready(function(){
$('#input-excel').change(function(e){
var reader = new FileReader();
reader.readAsArrayBuffer(e.target.files[0]);
reader.onload = function(e) {
var data = new Uint8Array(reader.result);
var wb = XLSX.read(data,{type:'array'});
var htmlstr = XLSX.write(wb,{sheet:"Sheet1", type:'binary',bookType:'html'});
$('#wrapper')[0].innerHTML += htmlstr;
// alert(htmlstr);
//<html><head><meta charset="utf-8"/><title>SheetJS Table Export</title></head><body><table><tr><td t="s" id="sjs-A1">First Name</td><td t="s" id="sjs-B1">Surname</td><td t="s" id="sjs-C1">DOB</td><td t="s" id="sjs-D1">Scout Number</td><td t="s" id="sjs-E1">Joining Date</td></tr><tr><td t="s" id="sjs-A2">R2C1</td><td t="s" id="sjs-B2">R2C2</td><td t="n" id="sjs-C2">4/20/18</td><td t="n" id="sjs-D2">1</td><td t="s" id="sjs-E2">R2C5</td></tr></table></body></html>
//Check number of columns
var colCount = 0;
$('tr:nth-child(1) td').each(function () {
if ($(this).attr('colspan')) {
colCount += +$(this).attr('colspan');
} else {
colCount++;
// alert("colCount: " + colCount);
}
});
//There must be 5 columns, if not return an error.
if (colCount == 5) {
//Return each element
var row = 0;
var column = 0;
var regExNumber = new RegExp("^\d+$");
var regExDate = new RegExp("(^(((0[1-9]|1[0-9]|2[0-8])[\/](0[1-9]|1[012]))|((29|30|31)[\/](0[13578]|1[02]))|((29|30)[\/](0[4,6,9]|11)))[\/](19|[2-9][0-9])\d\d$)|(^29[\/]02[\/](19|[2-9][0-9])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$)");
$("#wrapper tr").each(function () {
row++;
// alert("row: " + row);
column = 0;
$('td', this).each(function () {
column++;
// alert("column: " + column);
if (!$(this).text().replace(/\s/g, '').length) {
alert("Row " + row + " column " + column + " only containes whitespace (i.e., empty, spaces, tabs or line breaks).");
}else{
if (column == 4) {
if (!$(this).text().match(regExNumber)) {
alert("Row " + row + " column 4 must be numeric. " + $(this).text());
}
}
if (column == 3 || column == 5) {
if (!$(this).text().match(regExDate)) {
alert("Row " + row + " column " + column + " does not contain a valid date.");
}
}
}
})
})
}else{
alert("There must be 5 columns. You have " + colCount + " columns.")
}
}
});
}); // end document.ready