Skip to content

Commit

Permalink
updated sheet.coffee, templates.coffee to match manually-edited lib u…
Browse files Browse the repository at this point in the history
…pdates
  • Loading branch information
jaydanielsencision committed Aug 29, 2024
1 parent dfd61bf commit 4ecbc82
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 51 deletions.
34 changes: 27 additions & 7 deletions lib/sheet.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
(function() {
var _, sheetStream, template, through, utils;
var _, sheetStream, template, through, utils, worksheetTemplates;

_ = require("lodash");

through = require('through');

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) {
Expand All @@ -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;
};
Expand All @@ -48,7 +68,7 @@
store: opts.store
});
// ヘッダ部分を追加
converter.queue(template.header);
converter.queue(worksheetTemplates.header);
return converter;
};

Expand Down
63 changes: 48 additions & 15 deletions lib/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,29 @@
esc = utils.escapeXML;

module.exports = {
// worksheet
worksheet: {
header: xml(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
<sheetViews>
<sheetView workbookViewId="0"/>
</sheetViews>
<sheetFormatPr defaultRowHeight="15" x14ac:dyDescent="0.25"/>
<sheetData>`),
footer: xml(` </sheetData>
</worksheet>`)
footer: xml(`</sheetData>`),
hyperLinkStart: xml(`<hyperlinks>`),
hyperLink: function(link, rId) {
var escapedLink, parts, xmlString;
parts = link.split('-');
escapedLink = parts[1].replace(/&/g, '&amp;');
xmlString = `<hyperlink display="${escapedLink}" r:id="rId${rId}" ref="${parts[0]}" />`;
return xml(xmlString);
},
hyperLinkEnd: xml(`</hyperlinks>`),
endSheet: xml(`</worksheet>`)
},
// Static files
sheet_related: {
"[Content_Types].xml": {
header: xml(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
Expand Down Expand Up @@ -49,11 +59,12 @@
},
"xl/workbook.xml": {
header: xml(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<fileVersion appName="xl" lastEdited="5" lowestEdited="5" rupBuild="9303"/>
<workbookPr defaultThemeVersion="124226"/>
<bookViews>
<workbookView xWindow="480" yWindow="60" windowWidth="18195" windowHeight="8505"/>
<workbookView xWindow="480" yWindow="60" windowWidth="18195" windowHeight="8505"/>
</bookViews>
<sheets>`),
sheet: function(sheet) {
Expand All @@ -64,28 +75,30 @@
</workbook>`)
}
},
// 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 += ` <numFmt numFmtId=\"${item.numFmtId}\" formatCode=\"${esc(item.formatCode)}\" />\n`;
numFmtItems += `<numFmt numFmtId="${item.numFmtId}" formatCode="${esc(item.formatCode)}" />`;
}
numFmts = numFmtItems ? `<numFmts count="${styl.numFmts.length}">
${numFmtItems}</numFmts>` : "";
${numFmtItems}
</numFmts>` : "";
cellXfItems = "";
ref1 = styl.cellStyleXfs;
for (j = 0, len1 = ref1.length; j < len1; j++) {
item = ref1[j];
cellXfItems += ` <xf xfId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"0\" numFmtId=\"${item.numFmtId}\" applyNumberFormat=\"1\"/>\n`;
cellXfItems += `<xf xfId="0" fontId="0" fillId="0" borderId="0" numFmtId="${item.numFmtId}" applyNumberFormat="1"/>`;
}
cellXfs = cellXfItems ? `<cellXfs count="${Object.keys(styl.cellStyleXfs).length}">
${cellXfItems}
</cellXfs>` : "";
return xml(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
${numFmts}
<fonts count="1" x14ac:knownFonts="1">
<font>
Expand Down Expand Up @@ -126,10 +139,9 @@
</extLst>
</styleSheet>`);
},
// Static files
statics: {
"_rels/.rels": xml(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
Expand All @@ -153,13 +165,34 @@
var today;
today = new Date().toISOString();
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:dcmitype="http://purl.org/dc/dcmitype/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<dc:creator>node-xlsx-stream</dc:creator>
<cp:lastModifiedBy>node-xlsx-stream</cp:lastModifiedBy>
<dcterms:created xsi:type="dcterms:W3CDTF">${today}</dcterms:created>
<dcterms:modified xsi:type="dcterms:W3CDTF">${today}</dcterms:modified>
</cp:coreProperties>`;
}
},
rels: {
"xl/worksheets/_rels/sheet1.xml.rels": function(links) {
var escapedLink, i, len, link, linksCounter, parts, xmlString;
xmlString = xml(`<?xml version="1.0" encoding="UTF-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">`);
linksCounter = 0;
for (i = 0, len = links.length; i < len; i++) {
link = links[i];
linksCounter++;
parts = link.split('-');
escapedLink = parts[1].replace(/&/g, '&amp;');
xmlString += xml(`<Relationship Id="rId${linksCounter}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="${escapedLink}" TargetMode="External"/>`);
}
xmlString += xml(`</Relationships>`);
return xmlString;
}
}
};

Expand Down
24 changes: 20 additions & 4 deletions src/sheet.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,16 +22,30 @@ module.exports = sheetStream = (zip, sheet, opts={})->
buf += utils.buildCell("#{colChar(i)}#{nRow}", val, sheet.styles) for val, i in row
buf += '</row>'
@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

converter = through(onData, onEnd)
zip.append converter, name: sheet.path, store: opts.store

# ヘッダ部分を追加
converter.queue template.header
converter.queue worksheetTemplates.header

return converter
Loading

0 comments on commit 4ecbc82

Please sign in to comment.