From 783c6c57ac45b4c08dda19423eb8dafa45a10e53 Mon Sep 17 00:00:00 2001 From: alitskevich Date: Fri, 16 Jan 2015 14:30:53 +0300 Subject: [PATCH] Workbook.definedNames support --- bits/22_xmlutils.js | 49 +++++++++++++++++++++++++++++++++++++++++++++ bits/77_wbxml.js | 4 ++++ 2 files changed, 53 insertions(+) diff --git a/bits/22_xmlutils.js b/bits/22_xmlutils.js index b027fb6..6145528 100644 --- a/bits/22_xmlutils.js +++ b/bits/22_xmlutils.js @@ -20,6 +20,55 @@ function parsexmltag(tag, skip_root) { } function strip_ns(x) { return x.replace(nsregex2, "<$1"); } + +// Parses list of text tags: (text) ... +// @param str input string to be parsed +// @param tag tag name +// @param textAttrName name of atribute for inner text. 'text' by default. +// @return array of objects each contains attributes and text of refered tag: [ {textAttrName: text (, attr1: value1) ...}, ...] +var parseXmlTextTagList = (function() { + + // parse tag attributes + var parseAttrs = function(reAttrs, s, r) { + var e, v; + if (s) { + while ((e = reAttrs.exec(s))) { + v = e[2] || ''; + r[e[1]] = utf8read( v[0] === '"' ? v.slice(1, -1) : v ); + } + } + return r; + }; + + return function(str, tag, textAttrName) { + + // resulting array + var result = []; + + // name of atribute for inner text. 'text' by default. + if (!textAttrName) { + textAttrName = 'text'; + } + + // tag regExp. it will be modified by its .exec(), this why it is instantiated here + var TAG_RE = new RegExp('<'+tag+'((?:\\s+[a-z][a-z0-9\\-]+(?:=(?:(?:[\'"]?[^>\'"]*[\'"]?)))?)*)>([^<]+)<\/'+tag+'>','g'); + + // attributes RegExp. Although this regExp will be modified by its .exec() inside parseAttrs(), it is possible to be reused. + var ATTRS_RE = /\s+([a-z][a-z0-9\-]+)(?:=(['"]?[^>"]*['"]?))?/gi; + + var e = null; + while ((e = TAG_RE.exec(str))) { + var item = {}; + item[textAttrName] = e[2]; + parseAttrs(ATTRS_RE, e[1], item); + result.push(item); + } + + return result; + }; + +})(); + var encodings = { '"': '"', ''': "'", diff --git a/bits/77_wbxml.js b/bits/77_wbxml.js index 84a3ca8..990eca2 100644 --- a/bits/77_wbxml.js +++ b/bits/77_wbxml.js @@ -2,6 +2,10 @@ var wbnsregex = /<\w+:workbook/; function parse_wb_xml(data, opts) { var wb = { AppVersion:{}, WBProps:{}, WBView:[], Sheets:[], CalcPr:{}, xmlns: "" }; + + // parse defined names from tags named 'definedName' and use 'reference' as attribute name + wb.DefinedNames = parseXmlTextTagList(data, 'definedName', 'reference'); + var pass = false, xmlns = "xmlns"; data.match(tagregex).forEach(function xml_wb(x) { var y = parsexmltag(x);