html 此javascript文件将解析csv文件并将其添加到SharePoint列表。这是javascript开关的一个很好的例子,也是一个例子
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html 此javascript文件将解析csv文件并将其添加到SharePoint列表。这是javascript开关的一个很好的例子,也是一个例子相关的知识,希望对你有一定的参考价值。
<script type="text/javascript">
var renderArea = document.getElementById("renderArea");
function execFile(fileName,typeName) {
// main function to open, parse, and then render
var myfile = document.getElementById("F1");
var arr = readCSV(fileName);
for (var i = 0; i < arr.length; i++) {
arr[i] = parseLineCSV(arr[i]);
}
// render the result into html table
s = '<table border=1>';
for (var i = 0; i < arr.length; i++) {
s = s + '<tr>';
for (var j = 0; j < arr[i].length; j++) {
s = s + '<td><font size="1" face="Verdana">' + arr[i][j] + '</font></td>';
}
s = s + '</tr>';
}
s = s + '</table>';
var renderArea = document.getElementById("renderArea");
renderArea.innerHTML = s;
if(typeName == 'Processing')
sendProcessingToSharePoint(arr);
else
sendToSharePoint(arr);
}
if (typeof XMLHttpRequest == "undefined" && window.ActiveXObject) {
function XMLHttpRequest() {
var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
"Microsoft.XMLHTTP"];
for (var i = 0; i < arrSignatures.length; i++) {
try {
var oRequest = new ActiveXObject(arrSignatures[i]);
return oRequest;
} catch (oError) {
//ignore
}
}
throw new Error("MSXML is not installed on your system.");
}
}
function readCSV(locFile) {
// load a whole csv file, and then split it line by line
var req = new XMLHttpRequest();
req.open("GET", locFile, false);
req.send("");
return req.responseText.split(/\n/g);
}
function parseLineCSV(lineCSV) {
// parse csv line by line into array
var CSV = new Array();
lineCSV = lineCSV.replace(/,/g, " ,");
lineCSV = lineCSV.split(/,/g);
for (var i = 0; i < lineCSV.length; i++) {
lineCSV[i] = lineCSV[i].replace(/\s*$/g, "");
}
lineCSV[lineCSV.length - 1] = lineCSV[lineCSV.length - 1].replace(/^\s*|\s*$/g, "");
var fstart = -1;
var j = 0;
for (var i = 0; i< lineCSV.length; i++) {
if (lineCSV[i] != "-DELETED-") {
CSV[j] = lineCSV[i];
CSV[j] = CSV[j].replace(/^\s*|\s*$/g, ""); // remove leading & trailing space
CSV[j] = CSV[j].replace(/^"|"$/g, ""); // remove " on the beginning and end
CSV[j] = CSV[j].replace(/""/g, '"'); // replace "" with "
j++;
}
}
return CSV;
}
var fleetAssignment = {
Batch:'',
UIC:'',
Command:1,
AssignedBy:'',
Site:'',
NumberofTLDs:0,
DateRequested:0,
DateExpected:0,
AssignmentPeriod:'',
DateAnnealed:0,
DateTLDsAssigned:0,
DateSigReview:0,
DatePickedUp:0,
Comments:''
}
var fleetProcessing = {
Batch:'',
UIC:'',
Command:'',
ReceivedBy:'',
SiteReceived:'',
DateTLDsReceived:'',
LocationReceived:'',
DateTLDsProcessed:'',
DateUploaded:'',
DateNVLAPReviewProcessingRecord:'',
DateResultsReleased:'',
DateResultsPickedup:'',
DateTLDsReleased:'',
Comments:'',
QtyShipped:'',
QtyUnused:''
}
function sendToSharePoint(arr) {
for (var i = 1; i < arr.length; i++) {//for each line
fleetAssignment.Batch = arr[i][0];
fleetAssignment.UIC = arr[i][1];
fleetAssignment.Command = arr[i][2];
fleetAssignment.AssignedBy = getAssignedByID(arr[i][3]);
fleetAssignment.Site = getSite(arr[i][4]);
fleetAssignment.NumberofTLDs = arr[i][5];
fleetAssignment.DateRequested = arr[i][6];
fleetAssignment.DateExpected = arr[i][7];
fleetAssignment.AssignmentPeriod= arr[i][8];
fleetAssignment.DateAnnealed = arr[i][10];
fleetAssignment.DateTLDsAssigned = arr[i][11];
fleetAssignment.DateSigReview = arr[i][12];
fleetAssignment.DatePickedUp = arr[i][13];
fleetAssignment.Comments = arr[i][14];
writeToMaterList(fleetAssignment);
}
}
function sendProcessingToSharePoint(arr) {
for (var i = 1; i < arr.length; i++) {//for each line
fleetProcessing.Batch = arr[i][0];
fleetProcessing.UIC = arr[i][1];
fleetProcessing.Command = arr[i][2];
fleetProcessing.ReceivedBy = getAssignedByID(arr[i][3]);
fleetProcessing.SiteReceived = getSite(arr[i][4]);
fleetProcessing.DateTLDsReceived = arr[i][5];
fleetProcessing.LocationReceived = arr[i][6];
fleetProcessing.DateTLDsProcessed = arr[i][7];
fleetProcessing.DateUploaded= arr[i][8];
fleetProcessing.DateNVLAPReviewProcessingRecord = arr[i][9];
fleetProcessing.DateResultsReleased = arr[i][10];
fleetProcessing.DateResultsPickedup = arr[i][11];
fleetProcessing.DateTLDsReleased = arr[i][12];
fleetProcessing.Comments = arr[i][13];
fleetProcessing.QtyShipped = arr[i][14];
fleetProcessing.QtyUnused = arr[i][15];
writeToProcessingList(fleetProcessing);
}
}
function writeToProcessingList(fleetProcessing)
{
var datePat = /^\d{2}\/\d{2}\/\d{4}$/;
var nonNumberPat = /\D+/;
var clientContext= new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('Fleet Processing');
var itemCreateInfo = new SP.ListItemCreationInformation();
var oListItem = oList.addItem(itemCreateInfo);
nonNumberPat.test(fleetProcessing.Batch) ? '' : oListItem.set_item('Batch_x0020_Number', fleetProcessing.Batch);
nonNumberPat.test(fleetProcessing.UIC) ? '' : oListItem.set_item('UIC', fleetProcessing.UIC);
oListItem.set_item('Command_x0020_Name', fleetProcessing.Command);
oListItem.set_item('TLD_x0020_Received_x0020_By', fleetProcessing.ReceivedBy);
oListItem.set_item('Site', fleetProcessing.SiteReceived);
oListItem.set_item('Location_x0020_Received', fleetProcessing.LocationReceived);
datePat.test(fleetProcessing.DateTLDsReceived) ? oListItem.set_item('Date_x0020_Received', dateConvert(fleetProcessing.DateTLDsReceived)):'';
datePat.test(fleetProcessing.DateTLDsReceived) ? oListItem.set_item('Date_x0020_TLD_x0020_Received', dateConvert(fleetProcessing.DateTLDsReceived)):'';
datePat.test(fleetProcessing.DateTLDsProcessed) ? oListItem.set_item('Date_x0020_TLD_x0020_Processed', dateConvert(fleetProcessing.DateTLDsProcessed)):'';
datePat.test(fleetProcessing.DateUploaded) ? oListItem.set_item('Date_x0020_Upload_x0020_to_x0020', dateConvert(fleetProcessing.DateUploaded)):'';
datePat.test(fleetProcessing.DateNVLAPReviewProcessingRecord) ? oListItem.set_item('Date_x0020_Processing_x0020_Reco', dateConvert(fleetProcessing.DateNVLAPReviewProcessingRecord)):'';
datePat.test(fleetProcessing.DateResultsReleased) ? oListItem.set_item('Date_x0020_Release_x0020_of_x002', dateConvert(fleetProcessing.DateResultsReleased)):'';
datePat.test(fleetProcessing.DateResultsPickedup) ? oListItem.set_item('Date_x0020_Results_x0020_Picked_', dateConvert(fleetProcessing.DateResultsPickedup)):'';
oListItem.set_item('Notes_x0020_Added_x0020_to_x0020', fleetProcessing.Comments);
nonNumberPat.test(fleetProcessing.QtyShipped) ? '' : oListItem.set_item('Number_x0020_of_x0020_Shipped_x0', fleetProcessing.QtyShipped);
nonNumberPat.test(fleetProcessing.QtyUnused) ? '' : oListItem.set_item('Number_x0020_of_x0020_Unused_x00', fleetProcessing.QtyUnused);
oListItem.set_item('Percent_x0020_Complete', 1);
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQueryMasterSucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function writeToMaterList(fleetAssignment)
{
var datePat = /^\d{2}\/\d{2}\/\d{4}$/;
var clientContext= new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('Fleet Assignment');
var itemCreateInfo = new SP.ListItemCreationInformation();
var oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Batch_x0020_Number', fleetAssignment.Batch);
oListItem.set_item('UIC', fleetAssignment.UIC);
oListItem.set_item('Command_x0020_Name', fleetAssignment.Command);
oListItem.set_item('TLDAssignedBy', fleetAssignment.AssignedBy);
oListItem.set_item('Site', fleetAssignment.Site);
oListItem.set_item('Number_x0020_of_x0020_TLDs_x0020', fleetAssignment.NumberofTLDs);
datePat.test(fleetAssignment.DateRequested) ? oListItem.set_item('Date_x0020_Requested', dateConvert(fleetAssignment.DateRequested)):'';
datePat.test(fleetAssignment.DateExpected) ? oListItem.set_item('Date_x0020_Expected_x0020_Delive', dateConvert(fleetAssignment.DateExpected)):'';
oListItem.set_item('Assignment_x0020_Period', fleetAssignment.AssignmentPeriod);
datePat.test(fleetAssignment.DateAnnealed) ? oListItem.set_item('Date_x0020_Annealed', dateConvert(fleetAssignment.DateAnnealed)):'';
datePat.test(fleetAssignment.DateTLDsAssigned) ? oListItem.set_item('Date_x0020_TLDs_x0020_Assigned', dateConvert(fleetAssignment.DateTLDsAssigned)):'';
datePat.test(fleetAssignment.DateSigReview) ? oListItem.set_item('Date_x0020_Signatory_x0020_Revie', dateConvert(fleetAssignment.DateSigReview)):'';
//datePat.test(fleetAssignment.DatePickedUp) ? console.log(fleetAssignment.DatePickedUp) : 'Blank OK';
datePat.test(fleetAssignment.DatePickedUp) ? oListItem.set_item('Date_x0020_Picked_x0020_Up', dateConvert(fleetAssignment.DatePickedUp)):'';
oListItem.set_item('Comments', fleetAssignment.Comments);
oListItem.set_item('Percent_x0020_Complete', 1);
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQueryMasterSucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQueryMasterSucceeded() {
console.log('Master Item created');
//alert('Master Item created');
}
//var failedCounter = 0;
function onQueryFailed(sender, args) {
//console.log('Master Item failed');
console.log(args.get_message());
// console.log(dd);
//console.log(args.get_stackTrace());
//window.location.href = 'http://scushp01/Teams/compliance/Pages/RFC.aspx';
}
/*function isValidDate(d) {
if ( Object.prototype.toString.call(d) !== "[object Date]" )
return false;
return !isNaN(d.getTime());
}*/
function dateConvert(dateToConvert)
{
var pattern = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
var arrayDate = dateToConvert.match(pattern);
var dt = new Date(arrayDate[3], arrayDate[1] - 1, arrayDate[2]);
return dt;
}
function getSite(site)
{
var siteName;
switch(site){
case 'BR':
siteName='Bremerton';
break;
case 'SD':
siteName='San Diego';
break;
case 'JP':
siteName='Japan';
break;
default:
siteName = '';
}
return siteName;
}
function getAssignedByID(name)
{
var ID;
switch(name) {
case 'Edward':
ID = 6;
break;
case 'Julia':
ID = 7;
break;
case 'KS':
ID = 8;
break;
case 'Kevin':
ID = 9;
break;
case 'Terry':
ID = 10;
break;
case 'Joe':
ID = 11;
break;
case 'Jasmin':
ID = 12;
break;
case 'Andy':
ID = 13;
break;
case 'Bret':
ID = 14;
break;
case 'TS':
ID = 15;
break;
case 'KH':
ID = 16;
break;
case 'Nick':
ID = 17;
break;
case 'Zach':
ID = 18;
break;
case 'Boze':
ID = 19;
break;
case 'KR':
ID = 20;
break;
case 'Kari':
ID = 21;
break;
default:
ID = 22;
}
//console.log(ID);
return ID;
}
</script>
<input type="button" value="Read Assignments" name="B1" id="B1" onclick="execFile('FleetAssignment.csv','Assignments')"/>
<input type="button" value="Read Processing" name="B1" id="B1" onclick="execFile('FleetProcessing.csv','Processing')"/>
<input type="reset" value="Reset" name="B2" id="B2" onclick="renderArea.innerHTML=''"/>
<div id="renderArea"></div>
CSV Upload.txtOpen
2 of 3 items
Upload to Sharepoint list.txtCSV Upload.txtsmime.p7sDisplaying Upload to Sharepoint list.txt.
以上是关于html 此javascript文件将解析csv文件并将其添加到SharePoint列表。这是javascript开关的一个很好的例子,也是一个例子的主要内容,如果未能解决你的问题,请参考以下文章
如何将JavaScript数组信息导出到csv(在客户端)?
html 问Ben:使用Javascript Exec()正则表达式命令解析CSV字符串