From 369f5bb19f5b4b5cd0a54d56b28b04e1adf2894d Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 21 Jul 2015 10:45:10 +0800 Subject: [PATCH] change the codes to comply with the official rules of locating xlsx sheet --- lib/xlsx/xlsx.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/xlsx/xlsx.js b/lib/xlsx/xlsx.js index 23c7386..3b18a23 100644 --- a/lib/xlsx/xlsx.js +++ b/lib/xlsx/xlsx.js @@ -48,8 +48,28 @@ function xlsx(file) { return (s || '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, '\''); } + function findSheetPath(sheetId, wb, rel) { + var key, start, end, rid, target; + + //sheetId -> r:id + key = 'sheetId="' + sheetId + '"'; + + start = wb.indexOf('r:id="', wb.indexOf(key)) + 6; + end = wb.indexOf('"', start); + rid = wb.substring(start, end); + + //r:id -> Target + key = 'Relationship Id="' + rid + '"'; + start = rel.indexOf('Target="', rel.indexOf(key)) + 8; + end = rel.indexOf('"', start); + target = rel.substring(start, end); + + return 'xl/' + target; + } + if (typeof file === 'string') { // Load + var wbxml, relxml; zipTime = Date.now(); zip = zip.load(file, { base64: true }); result = { worksheets: [], zipTime: Date.now() - zipTime }; @@ -75,7 +95,8 @@ function xlsx(file) { result.modified = new Date(s.substring(0, s.indexOf(''))); // Get workbook info from "xl/workbook.xml" - Worksheet names exist in other places, but "activeTab" attribute must be gathered from this file anyway - s = zip.file('xl/workbook.xml').asText(); index = s.indexOf('activeTab="'); + wbxml = s = zip.file('xl/workbook.xml').asText(); index = s.indexOf('activeTab="'); + relxml = zip.file('xl/_rels/workbook.xml.rels').asText(); if (index > 0) { s = s.substr(index + 11); // Must eliminate first 11 characters before finding the index of " on the next line. Otherwise, it finds the " before the value. @@ -113,7 +134,7 @@ function xlsx(file) { // Get worksheet info from "xl/worksheets/sheetX.xml" i = result.worksheets.length; while (i--) { - s = zip.file('xl/worksheets/sheet' + (i + 1) + '.xml' ).asText().split(' 0; t = getAttr(s[0].substr(s[0].indexOf('