У меня есть задача в моей компании, в которой мне разрешено использовать JS-код только с Jquery / Ajax или даже SheetJS. Этот код должен читаться из листов Excel, может быть, 30 листов.каждый лист будет отображаться в отдельном HTML-файле.
ниже мой код, который он читает только с одного листа, и я не могу прочитать все столбцы.
-Как я могу прочитатьиз разных листов в одной книге MS Excel?
var xml_file_location = "kpi.xml";
var main_sheet = "Reports";
var Reports = new Array();
function Report() {
this.KPI_Title = "";
this.division_Title = "";
this.Report_Committee = "";
this.committee = "";
this.frequency = "";
this.KPI_Target = "";
this.KPI_Actual = "";
}
function Main() {
$.ajax({
url: xml_file_location,
type: "GET",
dataType: "xml",
data: "",
contentType: "text/xml; charset='utf-8'",
cache: false,
complete: parse_data,
error: function (xml, status, message) {
alert("something " + message + " something");
}
});
}
function get_sheet(data, sheet_name) {
return $(data.responseXML).find("Worksheet[ss\\:Name|='" + sheet_name + "']").contents().find("Row");
}
function parse_data(xData, status) {
var lastKPI = 0;
var rowIndex = 1;
var current_report_row;
var sheet = get_sheet(xData, main_sheet);
// console.log(sheet.text());
sheet.each(function () {
var current_row = $(this).find("Cell");
if ($(this).find("Cell:first[ss\\:Index]").length == 0) {
current_report_row = new Report();
current_report_row.KPI_Title = (current_row[1].firstChild!=null)? current_row[1].firstChild.textContent : "";
current_report_row.division_Title = (current_row[2].firstChild!=null)? current_row[2].firstChild.textContent : "";
current_report_row.Report_Committee = (current_row[3].firstChild!=null)? current_row[3].firstChild.textContent : "";
current_report_row.frequency = (current_row[4].firstChild!=null)? current_row[4].firstChild.textContent : "";
current_report_row.KPI_Target = (current_row[5].firstChild!=null)? current_row[5].firstChild.textContent : "";
current_report_row.KPI_Actual = (current_row[6].firstChild!=null)? current_row[6].firstChild.textContent : "";
}
Reports[rowIndex] = current_report_row;
//console.log(rowIndex);
//console.log(current_report_row);
rowIndex++;
})
var myJSON = JSON.stringify(Reports);
table_head = build_table_header();
table_body = build_table_body();
document.getElementById("report_list").innerHTML = table_head + table_body;
document.getElementById("Recc_list").innerHTML = table_head + table_body;
//document.getElementById("report_list").innerHTML = table_head;
//console.log(Reports);
}
function build_table_header() {
var table_head = "";
table_head += '<thead><tr class="headings">';
var keys = objectValues(Reports[1]);
for (i=0; i<keys.length; i++){
table_head += '<th class="column-title">'+ keys[i] +'</th>'
}
table_head += '</tr></thead>';
return table_head;
}
function build_table_body() {
var table_body = "";
table_body += '<tbody>'
var index = 0;
var i, j;
for (i=0; i<Reports.length; i++){
table_body += '<tr>';
if (i == 0 || i == 1) {continue;}
var keys = objectValues(Reports[i]);
for (j=0;j<keys.length; j++){
table_body += '<td>'+ keys[j] +'</td>';
}
table_body += '</tr>';
}
table_body += '</tbody>';
return table_body
}
function objectValues(obj) {
var res = [];
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
res.push(obj[i]);
}
}
return res;
}
Main();