mirror of
https://github.com/optilude/xlsx-template.git
synced 2026-07-02 00:17:39 +08:00
Introduce ability to check source code using TSC (#111)
This does not convert code to TypeScript yet. Based on initial testing, buster should be abandoned or typings for it should be written.
This commit is contained in:
+42
-33
@@ -4,10 +4,10 @@
|
||||
"use strict";
|
||||
|
||||
var path = require('path'),
|
||||
zip = require('jszip'),
|
||||
sizeOf = require('image-size'),
|
||||
fs = require('fs'),
|
||||
etree = require('elementtree');
|
||||
import zip from "jszip";
|
||||
|
||||
module.exports = (function() {
|
||||
|
||||
@@ -20,16 +20,24 @@ module.exports = (function() {
|
||||
* Create a new workbook. Either pass the raw data of a .xlsx file,
|
||||
* or call `loadTemplate()` later.
|
||||
*/
|
||||
var Workbook = function(data, option = {}) {
|
||||
var self = this;
|
||||
|
||||
self.archive = null;
|
||||
self.sharedStrings = [];
|
||||
self.sharedStringsLookup = {};
|
||||
self.option = option;
|
||||
var Workbook = function(data, option = { imageRootPath: undefined }) {
|
||||
this.archive = null;
|
||||
this.sharedStrings = [];
|
||||
this.sharedStringsLookup = {};
|
||||
this.option = option;
|
||||
this.sharedStringsPath = "";
|
||||
this.sheets = [];
|
||||
this.sheet = null;
|
||||
this.workbook = null;
|
||||
this.workbookPath = null;
|
||||
this.contentTypes = null;
|
||||
this.prefix = null;
|
||||
this.workbookRels = null;
|
||||
this.calChainRel = null;
|
||||
this.calcChainPath = "";
|
||||
|
||||
if(data) {
|
||||
self.loadTemplate(data);
|
||||
this.loadTemplate(data);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1137,11 +1145,11 @@ module.exports = (function() {
|
||||
var widthRate = imageWidth / mergeWidthEmus;
|
||||
var heightRate = imageHeight / mergeHeightEmus;
|
||||
if(widthRate > heightRate){
|
||||
imageWidth = Number.parseInt(imageWidth / widthRate);
|
||||
imageHeight = Number.parseInt(imageHeight / widthRate);
|
||||
imageWidth = Math.floor(imageWidth / widthRate);
|
||||
imageHeight = Math.floor(imageHeight / widthRate);
|
||||
}else{
|
||||
imageWidth = Number.parseInt(imageWidth / heightRate);
|
||||
imageHeight = Number.parseInt(imageHeight / heightRate);
|
||||
imageWidth = Math.floor(imageWidth / heightRate);
|
||||
imageHeight = Math.floor(imageHeight / heightRate);
|
||||
}
|
||||
imageInMergeCell = true;
|
||||
}
|
||||
@@ -1151,17 +1159,17 @@ module.exports = (function() {
|
||||
if(self.option && self.option.imageRatio){
|
||||
ratio = self.option.imageRatio;
|
||||
}
|
||||
imageWidth = Number.parseInt(imageWidth / ratio);
|
||||
imageHeight = Number.parseInt(imageHeight / ratio);
|
||||
imageWidth = Math.floor(imageWidth / ratio);
|
||||
imageHeight = Math.floor(imageHeight / ratio);
|
||||
}
|
||||
var imagePart = etree.SubElement(drawing.root, 'xdr:oneCellAnchor');
|
||||
var fromPart = etree.SubElement(imagePart, 'xdr:from');
|
||||
var fromCol = etree.SubElement(fromPart, 'xdr:col');
|
||||
fromCol.text = (Number.parseInt(self.charToNum(self.splitRef(cell.attrib.r).col))-1).toString();
|
||||
fromCol.text = (self.charToNum(self.splitRef(cell.attrib.r).col)-1).toString();
|
||||
var fromColOff = etree.SubElement(fromPart, 'xdr:colOff');
|
||||
fromColOff.text = '0';
|
||||
var fromRow = etree.SubElement(fromPart, 'xdr:row');
|
||||
fromRow.text = (Number.parseInt(self.splitRef(cell.attrib.r).row)-1).toString();
|
||||
fromRow.text = (self.splitRef(cell.attrib.r).row-1).toString();
|
||||
var fromRowOff = etree.SubElement(fromPart, 'xdr:rowOff');
|
||||
fromRowOff.text = '0';
|
||||
var extImagePart = etree.SubElement(imagePart, 'xdr:ext', { cx:imageWidth, cy:imageHeight });
|
||||
@@ -1436,8 +1444,8 @@ module.exports = (function() {
|
||||
var self = this;
|
||||
var mergeWidth = 0;
|
||||
var mergeRange = self.splitRange(mergeCell.attrib.ref),
|
||||
mergeStartCol = Number.parseInt(self.charToNum(self.splitRef(mergeRange.start).col)),
|
||||
mergeEndCol = Number.parseInt(self.charToNum(self.splitRef(mergeRange.end).col));
|
||||
mergeStartCol = self.charToNum(self.splitRef(mergeRange.start).col),
|
||||
mergeEndCol = self.charToNum(self.splitRef(mergeRange.end).col);
|
||||
for(let i = mergeStartCol; i < mergeEndCol + 1; i++){
|
||||
mergeWidth += self.getWidthCell(i, sheet);
|
||||
}
|
||||
@@ -1459,8 +1467,8 @@ module.exports = (function() {
|
||||
var self = this;
|
||||
var mergeHeight = 0;
|
||||
var mergeRange = self.splitRange(mergeCell.attrib.ref),
|
||||
mergeStartRow = Number.parseInt(self.splitRef(mergeRange.start).row),
|
||||
mergeEndRow = Number.parseInt(self.splitRef(mergeRange.end).row);
|
||||
mergeStartRow = self.splitRef(mergeRange.start).row,
|
||||
mergeEndRow = self.splitRef(mergeRange.end).row;
|
||||
for(let i = mergeStartRow; i < mergeEndRow + 1; i++){
|
||||
mergeHeight += self.getHeightCell(i, sheet);
|
||||
}
|
||||
@@ -1470,8 +1478,8 @@ module.exports = (function() {
|
||||
Workbook.prototype.getNbRowOfMergeCell = function(mergeCell){
|
||||
var self = this;
|
||||
var mergeRange = self.splitRange(mergeCell.attrib.ref),
|
||||
mergeStartRow = Number.parseInt(self.splitRef(mergeRange.start).row),
|
||||
mergeEndRow = Number.parseInt(self.splitRef(mergeRange.end).row);
|
||||
mergeStartRow = self.splitRef(mergeRange.start).row,
|
||||
mergeEndRow = self.splitRef(mergeRange.end).row;
|
||||
return mergeEndRow - mergeStartRow +1 ;
|
||||
}
|
||||
|
||||
@@ -1488,7 +1496,7 @@ module.exports = (function() {
|
||||
// https://poi.apache.org/apidocs/dev/org/apache/poi/util/Units.html
|
||||
// https://startbigthinksmall.wordpress.com/2010/01/04/points-inches-and-emus-measuring-units-in-office-open-xml/
|
||||
// http://lcorneliussen.de/raw/dashboards/ooxml/
|
||||
return this.pixelsToEMUs(width * 7,625579987895905);
|
||||
return this.pixelsToEMUs(width * 7.625579987895905);
|
||||
}
|
||||
|
||||
Workbook.prototype.rowHeightToEMUs = function (height) {
|
||||
@@ -1515,13 +1523,13 @@ module.exports = (function() {
|
||||
|
||||
Workbook.prototype.cellInMergeCells = function(cell, mergeCell){
|
||||
var self = this;
|
||||
var cellCol = Number.parseInt(self.charToNum(self.splitRef(cell.attrib.r).col));
|
||||
var cellRow = Number.parseInt(self.splitRef(cell.attrib.r).row);
|
||||
var cellCol = self.charToNum(self.splitRef(cell.attrib.r).col);
|
||||
var cellRow = self.splitRef(cell.attrib.r).row;
|
||||
var mergeRange = self.splitRange(mergeCell.attrib.ref),
|
||||
mergeStartCol = Number.parseInt(self.charToNum(self.splitRef(mergeRange.start).col)),
|
||||
mergeEndCol = Number.parseInt(self.charToNum(self.splitRef(mergeRange.end).col)),
|
||||
mergeStartRow = Number.parseInt(self.splitRef(mergeRange.start).row),
|
||||
mergeEndRow = Number.parseInt(self.splitRef(mergeRange.end).row);
|
||||
mergeStartCol = self.charToNum(self.splitRef(mergeRange.start).col),
|
||||
mergeEndCol = self.charToNum(self.splitRef(mergeRange.end).col),
|
||||
mergeStartRow = self.splitRef(mergeRange.start).row,
|
||||
mergeEndRow = self.splitRef(mergeRange.end).row;
|
||||
if(cellCol >= mergeStartCol && cellCol <= mergeEndCol ){
|
||||
if(cellRow >= mergeStartRow && cellRow <= mergeEndRow){
|
||||
return true;
|
||||
@@ -1559,21 +1567,22 @@ module.exports = (function() {
|
||||
}
|
||||
else{
|
||||
if(typeof(imageObj) === 'string' || imageObj instanceof String){
|
||||
imageObj = imageObj.toString();
|
||||
//if(this.isUrl(imageObj)){
|
||||
// TODO
|
||||
//}else{
|
||||
if("imageRootPath" in this.option && fs.existsSync(this.option.imageRootPath + "/" + imageObj)){
|
||||
//get the Absolute path file
|
||||
return new Buffer.from(fs.readFileSync(this.option.imageRootPath + "/" + imageObj, { encoding: 'base64' }), 'base64');
|
||||
return Buffer.from(fs.readFileSync(this.option.imageRootPath + "/" + imageObj, { encoding: 'base64' }), 'base64');
|
||||
}else{
|
||||
if(fs.existsSync(imageObj)){
|
||||
//get the relatif path file
|
||||
return new Buffer.from(fs.readFileSync(imageObj, { encoding: 'base64' }), 'base64');
|
||||
return Buffer.from(fs.readFileSync(imageObj, { encoding: 'base64' }), 'base64');
|
||||
}
|
||||
}
|
||||
//}
|
||||
try {
|
||||
var buff = new Buffer.from(imageObj, 'base64')
|
||||
var buff = Buffer.from(imageObj, 'base64')
|
||||
return buff;
|
||||
} catch (error) {
|
||||
console.log("this is NOT a base64 string")
|
||||
|
||||
Generated
+3031
File diff suppressed because it is too large
Load Diff
@@ -34,6 +34,8 @@
|
||||
}
|
||||
],
|
||||
"dependencies": {
|
||||
"@types/jszip": "^3.1.7",
|
||||
"@types/node": "^14.0.27",
|
||||
"elementtree": "0.1.6",
|
||||
"image-size": "^0.3.5",
|
||||
"jszip": "^2.6.1"
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
"module": "commonjs",
|
||||
"allowJs": true,
|
||||
"checkJs": true,
|
||||
"noEmit": true,
|
||||
"strict": false,
|
||||
"noImplicitAny": false,
|
||||
"esModuleInterop": true
|
||||
},
|
||||
"files": [
|
||||
"lib/index.js"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user