xlsx-template
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -0,4 +1,28 @@ | ||
import * as etree from "elementtree"; | ||
import * as JSZip from "jszip"; | ||
export interface TemplatePlaceholder{ | ||
type: string; | ||
string?: string; | ||
full: boolean; | ||
name: string; | ||
key: string; | ||
placeholder?: string | ||
} | ||
export interface NamedTable{ | ||
filename: string; | ||
root: etree.Element; | ||
} | ||
export default class Workbook | ||
{ | ||
constructor(data? : Buffer); | ||
protected readonly sharedStrings: string[]; | ||
protected readonly workbook: etree.ElementTree; | ||
protected readonly archive: JSZip; | ||
protected readonly workbookPath: string; | ||
protected readonly calcChainPath?: string; | ||
constructor(data? : Buffer, option? : object); | ||
public deleteSheet(sheetName : string) : this; | ||
@@ -16,4 +40,8 @@ public copySheet(sheetName : string, copyName : string) : this; | ||
protected replaceString(oldString : string, newString : string) : any; // returns idx | ||
protected loadSheets(prefix : any, workbook : any, workbookRels : any) : any[]; | ||
protected loadSheets(prefix : any, workbook : etree.ElementTree, workbookRels : any) : any[]; | ||
protected loadSheet(sheet : any) : { filename : any, name : any, id : any, root : any }; // this could definitely return a "Sheet" interface/class | ||
protected loadSheetRels(sheetFilename : string) : { rels : any}; | ||
protected loadDrawing(sheet : any, sheetFilename : string, rels : any) : { drawing : any}; | ||
protected writeDrawing(drawing : any); | ||
protected moveAllImages(drawing : any, fromRow : int, nbRow : int); | ||
protected loadTables(sheet : any, sheetFilename : any) : any; | ||
@@ -34,6 +62,6 @@ protected writeTables(tables : any) : void; | ||
protected insertCellValue(cell : any, substitution : any) : string; | ||
protected substituteScalar(cell : any); | ||
protected substituteScalar(cell : any, string: string, placeholder: TemplatePlaceholder, substitution: any); | ||
protected substituteArray(cells : any[], cell : any, substitution : any); | ||
protected substituteTable(row : any, newTableRows : any, cells : any[], cell : any, namedTables : any, substitution : any, key : any) : any; | ||
protected cloneElement(element : any, deep : any) : any; | ||
protected cloneElement(element : any, deep? : any) : any; | ||
protected replaceChildren(parent : any, children : any) : void; | ||
@@ -45,4 +73,4 @@ protected getCurrentRow(row : any, rowsInserted : any) : number; | ||
protected joinRange(range : any) : string | ||
protected pushRight(workbook : any, sheet : any, currentCell : any, numCols : any) : any; | ||
protected pushDown(workbook : any, sheets : any, tables : any, currentRow : any, numRows : any) : any; | ||
protected pushRight(workbook : etree.ElementTree, sheet : any, currentCell : any, numCols : any) : any; | ||
protected pushDown(workbook : etree.ElementTree, sheets : any, tables : any, currentRow : any, numRows : any) : any; | ||
} | ||
@@ -53,2 +81,2 @@ | ||
type : "uint8array" | "arraybuffer" | "blob" | "nodebuffer" | "base64"; | ||
} | ||
} |
@@ -21,3 +21,3 @@ /*jshint globalstrict:true, devel:true */ | ||
*/ | ||
var Workbook = function(data) { | ||
var Workbook = function(data, option = {}) { | ||
var self = this; | ||
@@ -28,2 +28,3 @@ | ||
self.sharedStringsLookup = {}; | ||
self.option = option; | ||
@@ -201,3 +202,4 @@ if(data) { | ||
namedTables = self.loadTables(sheet.root, sheet.filename), | ||
rows = []; | ||
rows = [], | ||
drawing = null; | ||
@@ -300,2 +302,12 @@ sheetData.findall("row").forEach(function(row) { | ||
if(newTableRows.length > 0) { | ||
//Move images for each subsitute array if option is active | ||
if(self.option["moveImages"]){ | ||
var rels = self.loadSheetRels(sheet.filename); | ||
if (rels != null && drawing == null){ | ||
drawing = self.loadDrawing(sheet.root, sheet.filename, rels.root); | ||
} | ||
if(drawing != null){ | ||
self.moveAllImages(drawing, row.attrib.r, newTableRows.length) | ||
} | ||
} | ||
newTableRows.forEach(function(row) { | ||
@@ -357,2 +369,3 @@ rows.push(row); | ||
self.writeTables(namedTables); | ||
self.writeDrawing(drawing); | ||
}; | ||
@@ -490,2 +503,77 @@ | ||
//Load rels for a sheetName | ||
Workbook.prototype.loadSheetRels = function (sheetFilename) { | ||
var self = this; | ||
var sheetDirectory = path.dirname(sheetFilename), | ||
sheetName = path.basename(sheetFilename), | ||
relsFilename = path.join(sheetDirectory, '_rels', sheetName + '.rels').replace(/\\/g, '/'), | ||
relsFile = self.archive.file(relsFilename); | ||
if (relsFile === null) { | ||
return null; | ||
} | ||
var rels = {filename: relsFilename, root: etree.parse(relsFile.asText()).getroot()} | ||
return rels; | ||
} | ||
//Load Drawing file | ||
Workbook.prototype.loadDrawing = function (sheet, sheetFilename, rels) { | ||
var self = this; | ||
var sheetDirectory = path.dirname(sheetFilename), | ||
sheetName = path.basename(sheetFilename), | ||
drawing = {filename: '', root: null}; | ||
var drawingPart = sheet.find("drawing"); | ||
if (drawingPart === null) { | ||
return null; | ||
} | ||
var relationshipId = drawingPart.attrib['r:id'], | ||
target = rels.find("Relationship[@Id='" + relationshipId + "']").attrib.Target, | ||
drawingFilename = path.join(sheetDirectory, target).replace(/\\/g, '/'), | ||
drawingTree = etree.parse(self.archive.file(drawingFilename).asText()); | ||
drawing.filename = drawingFilename; | ||
drawing.root = drawingTree.getroot(); | ||
drawing.relFilename = path.dirname(drawingFilename) + '/_rels/' + path.basename(drawingFilename) + '.rels'; | ||
drawing.relRoot = etree.parse(self.archive.file(drawing.relFilename).asText()).getroot(); | ||
return drawing; | ||
}; | ||
//Write Drawing file | ||
Workbook.prototype.writeDrawing = function (drawing) { | ||
var self = this; | ||
if (drawing!==null){ | ||
self.archive.file(drawing.filename, etree.tostring(drawing.root)); | ||
self.archive.file(drawing.relFilename, etree.tostring(drawing.relRoot)); | ||
} | ||
}; | ||
//Move all images after fromRow of nbRow row | ||
Workbook.prototype.moveAllImages = function(drawing, fromRow, nbRow){ | ||
var self = this; | ||
drawing.root.getchildren().forEach(function(drawElement){ | ||
if(drawElement.tag == "xdr:twoCellAnchor"){ | ||
self._moveTwoCellAnchor(drawElement, fromRow, nbRow) | ||
} | ||
//TODO : make the other tags image | ||
}) | ||
}; | ||
//Move TwoCellAnchor tag images after fromRow of nbRow row | ||
Workbook.prototype._moveTwoCellAnchor = function(drawingElement, fromRow, nbRow){ | ||
var self = this; | ||
var _moveImage = function(drawingElement, fromRow, nbRow){ | ||
var from = Number.parseInt(drawingElement.find('xdr:from').find('xdr:row').text, 10) + Number.parseInt(nbRow, 10) | ||
drawingElement.find('xdr:from').find('xdr:row').text = from | ||
var to = Number.parseInt(drawingElement.find('xdr:to').find('xdr:row').text, 10) + Number.parseInt(nbRow, 10) | ||
drawingElement.find('xdr:to').find('xdr:row').text = to | ||
} | ||
if(self.option["moveSameLineImages"]){ | ||
if(parseInt(drawingElement.find('xdr:from').find('xdr:row').text) + 1 >= fromRow){ | ||
_moveImage(drawingElement, fromRow, nbRow) | ||
} | ||
}else{ | ||
if(parseInt(drawingElement.find('xdr:from').find('xdr:row').text) + 1 > fromRow){ | ||
_moveImage(drawingElement, fromRow, nbRow) | ||
} | ||
} | ||
}; | ||
// Load tables for a given sheet | ||
@@ -492,0 +580,0 @@ Workbook.prototype.loadTables = function(sheet, sheetFilename) { |
{ | ||
"name": "xlsx-template", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Generate .xlsx (Excel) files from templates built in Excel", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index", |
@@ -164,2 +164,22 @@ # XLSX Template | ||
### Version 1.3.0 | ||
* Added support for optional moving of the images together with table. (#109) | ||
### Version 1.2.0 | ||
* Specify license field in addition to licenses field in the package.json (#102) | ||
### Version 1.1.0 | ||
* Added TypeScript definitions. #101 | ||
* NodeJS 12, 14 support | ||
### Version 1.0.0 | ||
Nothing to see here. Just I'm being brave and make version 1.0.0 | ||
### Version 0.5.0 | ||
* Placeholder in hyperlinks. #87 | ||
* NodeJS 10 support | ||
### Version 0.4.0 | ||
* Fix wrongly replacing text in shared strings #81 | ||
### Version 0.2.0 | ||
@@ -166,0 +186,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
63008
1105
225