From 4ecbc82310e4f1308750fd39a3fd7fc8b66b4334 Mon Sep 17 00:00:00 2001 From: Jay Danielsen Date: Thu, 29 Aug 2024 10:08:44 -0400 Subject: [PATCH] updated sheet.coffee, templates.coffee to match manually-edited lib updates --- lib/sheet.js | 34 +++++++++++---- lib/templates.js | 63 +++++++++++++++++++++------- src/sheet.coffee | 24 +++++++++-- src/templates.coffee | 98 +++++++++++++++++++++++++++++++++----------- 4 files changed, 168 insertions(+), 51 deletions(-) diff --git a/lib/sheet.js b/lib/sheet.js index aa4edd0..725f9dc 100644 --- a/lib/sheet.js +++ b/lib/sheet.js @@ -1,5 +1,5 @@ (function() { - var _, sheetStream, template, through, utils; + var _, sheetStream, template, through, utils, worksheetTemplates; _ = require("lodash"); @@ -7,13 +7,16 @@ utils = require('./utils'); - template = require('./templates').worksheet; + template = require('./templates'); + + worksheetTemplates = template.worksheet; module.exports = sheetStream = function(zip, sheet, opts = {}) { - var colChar, converter, nRow, onData, onEnd; + var colChar, converter, links, nRow, onData, onEnd; // 列番号の26進表記(A, B, .., Z, AA, AB, ..) // 一度計算したらキャッシュしておく。 colChar = _.memoize(utils.colChar); + links = []; // 行ごとに変換してxl/worksheets/sheet1.xml に追加 nRow = 0; onData = function(row) { @@ -36,9 +39,26 @@ return this.queue(buf); }; onEnd = function() { - var converter; - // フッタ部分を追加 - this.queue(template.footer); + var converter, func, j, len, link, linkCounter, name, rel; + this.queue(worksheetTemplates.footer); + if (links.length > 0) { + rel = template.rels; + for (name in rel) { + func = rel[name]; + zip.append(func(links), { + name: name + }); + } + this.queue(worksheetTemplates.hyperLinkStart); + linkCounter = 0; + for (j = 0, len = links.length; j < len; j++) { + link = links[j]; + linkCounter++; + this.queue(worksheetTemplates.hyperLink(link, linkCounter)); + } + this.queue(worksheetTemplates.hyperLinkEnd); + } + this.queue(worksheetTemplates.endSheet); this.queue(null); return converter = colChar = zip = null; }; @@ -48,7 +68,7 @@ store: opts.store }); // ヘッダ部分を追加 - converter.queue(template.header); + converter.queue(worksheetTemplates.header); return converter; }; diff --git a/lib/templates.js b/lib/templates.js index a685a90..a670b30 100644 --- a/lib/templates.js +++ b/lib/templates.js @@ -8,19 +8,29 @@ esc = utils.escapeXML; module.exports = { - // worksheet worksheet: { header: xml(` - + `), - footer: xml(` -`) + footer: xml(``), + hyperLinkStart: xml(``), + hyperLink: function(link, rId) { + var escapedLink, parts, xmlString; + parts = link.split('-'); + escapedLink = parts[1].replace(/&/g, '&'); + xmlString = ``; + return xml(xmlString); + }, + hyperLinkEnd: xml(``), + endSheet: xml(``) }, - // Static files sheet_related: { "[Content_Types].xml": { header: xml(` @@ -49,11 +59,12 @@ }, "xl/workbook.xml": { header: xml(` - + - + `), sheet: function(sheet) { @@ -64,28 +75,30 @@ `) } }, - // Styles file styles: function(styl) { var cellXfItems, cellXfs, i, item, j, len, len1, numFmtItems, numFmts, ref, ref1; numFmtItems = ""; ref = styl.numFmts; for (i = 0, len = ref.length; i < len; i++) { item = ref[i]; - numFmtItems += ` \n`; + numFmtItems += ``; } numFmts = numFmtItems ? ` - ${numFmtItems}` : ""; + ${numFmtItems} +` : ""; cellXfItems = ""; ref1 = styl.cellStyleXfs; for (j = 0, len1 = ref1.length; j < len1; j++) { item = ref1[j]; - cellXfItems += ` \n`; + cellXfItems += ``; } cellXfs = cellXfItems ? ` ${cellXfItems} ` : ""; return xml(` - + ${numFmts} @@ -126,10 +139,9 @@ `); }, - // Static files statics: { "_rels/.rels": xml(` - + @@ -153,13 +165,34 @@ var today; today = new Date().toISOString(); return ` - + node-xlsx-stream node-xlsx-stream ${today} ${today} `; } + }, + rels: { + "xl/worksheets/_rels/sheet1.xml.rels": function(links) { + var escapedLink, i, len, link, linksCounter, parts, xmlString; + xmlString = xml(` +`); + linksCounter = 0; + for (i = 0, len = links.length; i < len; i++) { + link = links[i]; + linksCounter++; + parts = link.split('-'); + escapedLink = parts[1].replace(/&/g, '&'); + xmlString += xml(``); + } + xmlString += xml(``); + return xmlString; + } } }; diff --git a/src/sheet.coffee b/src/sheet.coffee index 4155e9c..3059399 100644 --- a/src/sheet.coffee +++ b/src/sheet.coffee @@ -2,12 +2,14 @@ _ = require "lodash" through = require('through') utils = require('./utils') -template = require('./templates').worksheet +template = require('./templates') +worksheetTemplates = template.worksheet module.exports = sheetStream = (zip, sheet, opts={})-> # 列番号の26進表記(A, B, .., Z, AA, AB, ..) # 一度計算したらキャッシュしておく。 colChar = _.memoize utils.colChar + links = [] # 行ごとに変換してxl/worksheets/sheet1.xml に追加 nRow = 0 @@ -20,9 +22,23 @@ module.exports = sheetStream = (zip, sheet, opts={})-> buf += utils.buildCell("#{colChar(i)}#{nRow}", val, sheet.styles) for val, i in row buf += '' @queue buf + onEnd = -> - # フッタ部分を追加 - @queue template.footer + @queue worksheetTemplates.footer + + if links.length > 0 + rel = template.rels + for name, func of rel + zip.append func(links), name: name + + @queue worksheetTemplates.hyperLinkStart + linkCounter = 0 + for link in links + linkCounter++ + @queue worksheetTemplates.hyperLink(link, linkCounter) + @queue worksheetTemplates.hyperLinkEnd + + @queue worksheetTemplates.endSheet @queue null converter = colChar = zip = null @@ -30,6 +46,6 @@ module.exports = sheetStream = (zip, sheet, opts={})-> zip.append converter, name: sheet.path, store: opts.store # ヘッダ部分を追加 - converter.queue template.header + converter.queue worksheetTemplates.header return converter diff --git a/src/templates.coffee b/src/templates.coffee index 1db321c..80ec3c0 100644 --- a/src/templates.coffee +++ b/src/templates.coffee @@ -4,11 +4,13 @@ xml = utils.compress esc = utils.escapeXML module.exports = - # worksheet worksheet: header: xml """ - + @@ -17,10 +19,24 @@ module.exports = """ footer: xml """ + """ + hyperLinkStart: xml """ + + """ + hyperLink: (link, rId) -> + parts = link.split('-') + escapedLink = parts[1].replace(/&/g, '&') + xmlString = """ + + """ + xml xmlString + hyperLinkEnd: xml """ + + """ + endSheet: xml """ """ - # Static files sheet_related: "[Content_Types].xml": header: xml """ @@ -34,9 +50,10 @@ module.exports = """ - sheet: (sheet)-> """ - - """ + sheet: (sheet) -> + """ + + """ footer: xml """ """ @@ -46,9 +63,10 @@ module.exports = """ - sheet: (sheet)-> """ - - """ + sheet: (sheet) -> + """ + + """ footer: xml """ @@ -58,36 +76,42 @@ module.exports = "xl/workbook.xml": header: xml """ - + - + """ - sheet: (sheet)-> xml """ - - """ + sheet: (sheet) -> + xml """ + + """ footer: xml """ """ - # Styles file - styles: (styl)-> + styles: (styl) -> numFmtItems = "" for item in styl.numFmts - numFmtItems += " \n" + numFmtItems += """ + + """ numFmts = if numFmtItems then """ - #{numFmtItems} + #{numFmtItems} + """ else "" cellXfItems = "" for item in styl.cellStyleXfs - cellXfItems += " \n" + cellXfItems += """ + + """ cellXfs = if cellXfItems then """ #{cellXfItems} @@ -96,7 +120,9 @@ module.exports = xml """ - + #{numFmts} @@ -138,11 +164,10 @@ module.exports = """ - # Static files statics: "_rels/.rels": xml """ - + @@ -166,17 +191,40 @@ module.exports = false #{require('../package.json').version} - """ + """ semiStatics: - "docProps/core.xml": (opts)-> + "docProps/core.xml": (opts) -> today = new Date().toISOString() """ - + node-xlsx-stream node-xlsx-stream #{today} #{today} """ + + rels: + "xl/worksheets/_rels/sheet1.xml.rels": (links) -> + xmlString = xml """ + + + """ + linksCounter = 0 + for link in links + linksCounter++ + parts = link.split('-') + escapedLink = parts[1].replace(/&/g, '&') + xmlString += xml """ + + """ + xmlString += xml """ + + """ + xmlString