mirror of
https://github.com/optilude/xlsx-template.git
synced 2026-07-02 00:17:39 +08:00
Clarify exported XlsxTemplate types in the declaration file (#221)
This commit is contained in:
committed by
GitHub
parent
8757308c9a
commit
7a95c8f417
Vendored
+52
-55
@@ -1,54 +1,51 @@
|
|||||||
import * as etree from "elementtree";
|
import * as etree from "elementtree";
|
||||||
import * as JSZip from "jszip";
|
import * as JSZip from "jszip";
|
||||||
|
|
||||||
export interface TemplatePlaceholder{
|
declare namespace XlsxTemplate {
|
||||||
type: string;
|
interface TemplatePlaceholder {
|
||||||
string?: string;
|
type: string;
|
||||||
full: boolean;
|
string?: string;
|
||||||
name: string;
|
full: boolean;
|
||||||
key: string;
|
name: string;
|
||||||
placeholder?: string
|
key: string;
|
||||||
|
placeholder?: string;
|
||||||
|
subType?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface NamedTable {
|
||||||
|
filename: string;
|
||||||
|
root: etree.Element;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OutputByType {
|
||||||
|
base64: string;
|
||||||
|
uint8array: Uint8Array;
|
||||||
|
arraybuffer: ArrayBuffer;
|
||||||
|
blob: Blob;
|
||||||
|
nodebuffer: Buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
type GenerateType = keyof OutputByType;
|
||||||
|
|
||||||
|
type GenerateOptions<T extends GenerateType = GenerateType> = {
|
||||||
|
type: T;
|
||||||
|
};
|
||||||
|
|
||||||
|
interface RangeSplit {
|
||||||
|
start: string;
|
||||||
|
end: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ReferenceAddress {
|
||||||
|
table?: string;
|
||||||
|
colAbsolute?: boolean;
|
||||||
|
col: string;
|
||||||
|
rowAbsolute?: boolean;
|
||||||
|
row: number;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface NamedTable{
|
declare class XlsxTemplate
|
||||||
filename: string;
|
|
||||||
root: etree.Element;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace XlsxTemplate {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
interface OutputByType {
|
|
||||||
base64: string;
|
|
||||||
uint8array: Uint8Array;
|
|
||||||
arraybuffer: ArrayBuffer;
|
|
||||||
blob: Blob;
|
|
||||||
nodebuffer: Buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
type GenerateType = keyof OutputByType;
|
|
||||||
|
|
||||||
export type GenerateOptions<T extends GenerateType = GenerateType> = {
|
|
||||||
type: T;
|
|
||||||
};
|
|
||||||
|
|
||||||
interface RangeSplit
|
|
||||||
{
|
|
||||||
start: string;
|
|
||||||
end: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ReferenceAddress
|
|
||||||
{
|
|
||||||
table?: string;
|
|
||||||
colAbsolute?: boolean;
|
|
||||||
col : string;
|
|
||||||
rowAbsolute?: boolean;
|
|
||||||
row : number;
|
|
||||||
}
|
|
||||||
|
|
||||||
class XlsxTemplate
|
|
||||||
{
|
{
|
||||||
public readonly sharedStrings: string[];
|
public readonly sharedStrings: string[];
|
||||||
protected readonly workbook: etree.ElementTree;
|
protected readonly workbook: etree.ElementTree;
|
||||||
@@ -62,16 +59,16 @@ class XlsxTemplate
|
|||||||
public copySheet(sheetName : string, copyName : string, binary? : boolean) : this;
|
public copySheet(sheetName : string, copyName : string, binary? : boolean) : this;
|
||||||
public loadTemplate(data : Buffer) : void;
|
public loadTemplate(data : Buffer) : void;
|
||||||
public substitute(sheetName : string | number, substitutions : Object) : void;
|
public substitute(sheetName : string | number, substitutions : Object) : void;
|
||||||
public generate<T extends GenerateType>(options: GenerateOptions<T>): OutputByType[T];
|
public generate<T extends XlsxTemplate.GenerateType>(options: XlsxTemplate.GenerateOptions<T>): XlsxTemplate.OutputByType[T];
|
||||||
public generate() : any;
|
public generate() : any;
|
||||||
|
|
||||||
public replaceString(oldString : string, newString : string) : number; // returns idx
|
public replaceString(oldString : string, newString : string) : number; // returns idx
|
||||||
public stringIndex(s : string) : number; // returns idx
|
public stringIndex(s : string) : number; // returns idx
|
||||||
public writeSharedStrings() : void;
|
public writeSharedStrings() : void;
|
||||||
public splitRef(ref : string) : ReferenceAddress;
|
public splitRef(ref : string) : XlsxTemplate.ReferenceAddress;
|
||||||
public joinRef(ref : ReferenceAddress) : string;
|
public joinRef(ref : XlsxTemplate.ReferenceAddress) : string;
|
||||||
public addSharedString(s : string) : any; // I think s is a string? Not sure what its return "idx" is though, I think it's a number? Is "idx" short for "index"?
|
public addSharedString(s : string) : any; // I think s is a string? Not sure what its return "idx" is though, I think it's a number? Is "idx" short for "index"?
|
||||||
public substituteScalar(cell : any, string: string, placeholder: TemplatePlaceholder, substitution: any): string;
|
public substituteScalar(cell : any, string: string, placeholder: XlsxTemplate.TemplatePlaceholder, substitution: any): string;
|
||||||
public charToNum(str : string) : number;
|
public charToNum(str : string) : number;
|
||||||
public numToChar(num : number) : string;
|
public numToChar(num : number) : string;
|
||||||
public nextCol(ref : string) : string;
|
public nextCol(ref : string) : string;
|
||||||
@@ -79,9 +76,9 @@ class XlsxTemplate
|
|||||||
public stringify(value : Date | number | boolean | string) : string;
|
public stringify(value : Date | number | boolean | string) : string;
|
||||||
public isWithin(ref : string, startRef : string, endRef : string) : boolean;
|
public isWithin(ref : string, startRef : string, endRef : string) : boolean;
|
||||||
public isRange(ref : string) : boolean;
|
public isRange(ref : string) : boolean;
|
||||||
public splitRange(range : string) : RangeSplit;
|
public splitRange(range : string) : XlsxTemplate.RangeSplit;
|
||||||
public joinRange(range : RangeSplit) : string;
|
public joinRange(range : XlsxTemplate.RangeSplit) : string;
|
||||||
public extractPlaceholders(templateString : string) : TemplatePlaceholder[];
|
public extractPlaceholders(templateString : string) : XlsxTemplate.TemplatePlaceholder[];
|
||||||
|
|
||||||
// need typing properly
|
// need typing properly
|
||||||
protected _rebuild() : void;
|
protected _rebuild() : void;
|
||||||
@@ -100,8 +97,8 @@ class XlsxTemplate
|
|||||||
protected substituteTableColumnHeaders(tables : any, substitutions : any) : void;
|
protected substituteTableColumnHeaders(tables : any, substitutions : any) : void;
|
||||||
protected insertCellValue(cell : any, substitution : any) : string;
|
protected insertCellValue(cell : any, substitution : any) : string;
|
||||||
protected substituteArray(cells : any[], cell : any, 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, placeholder : TemplatePlaceholder, drawing : etree.ElementTree) : any;
|
protected substituteTable(row : any, newTableRows : any, cells : any[], cell : any, namedTables : any, substitution : any, key : any, placeholder : XlsxTemplate.TemplatePlaceholder, drawing : etree.ElementTree) : any;
|
||||||
protected substituteImage(cell : any, string : string, placeholder: TemplatePlaceholder, substitution : any, drawing : etree.ElementTree) : boolean
|
protected substituteImage(cell : any, string : string, placeholder: XlsxTemplate.TemplatePlaceholder, substitution : any, drawing : etree.ElementTree) : boolean
|
||||||
protected substituteImageInCell(cell : any, substitution : any) : boolean;
|
protected substituteImageInCell(cell : any, substitution : any) : boolean;
|
||||||
protected cloneElement(element : any, deep? : any) : any;
|
protected cloneElement(element : any, deep? : any) : any;
|
||||||
protected replaceChildren(parent : any, children : any) : void;
|
protected replaceChildren(parent : any, children : any) : void;
|
||||||
|
|||||||
+2
-1
@@ -69,7 +69,8 @@
|
|||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"compile": "tsc",
|
"compile": "tsc",
|
||||||
"test": "npm run compile && jest",
|
"test:module": "find test/module -name package.json -execdir npm test \\;",
|
||||||
|
"test": "npm run compile && npm run test:module && jest",
|
||||||
"prepublishOnly": "npm run compile"
|
"prepublishOnly": "npm run compile"
|
||||||
},
|
},
|
||||||
"readme": "",
|
"readme": "",
|
||||||
|
|||||||
@@ -128,6 +128,19 @@ describe("Helpers", function() {
|
|||||||
}]);
|
}]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("can extract placeholders with subtypes", function() {
|
||||||
|
var t = new XlsxTemplate();
|
||||||
|
|
||||||
|
expect(t.extractPlaceholders("${table:foo.bar:image}")).toEqual([{
|
||||||
|
full: true,
|
||||||
|
key: "bar",
|
||||||
|
name: "foo",
|
||||||
|
placeholder: "${table:foo.bar:image}",
|
||||||
|
subType: "image",
|
||||||
|
type: "table"
|
||||||
|
}]);
|
||||||
|
});
|
||||||
|
|
||||||
it("can handle strings with no placeholders", function() {
|
it("can handle strings with no placeholders", function() {
|
||||||
var t = new XlsxTemplate();
|
var t = new XlsxTemplate();
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import XlsxTemplate = require("xlsx-template");
|
||||||
|
|
||||||
|
const generateType: XlsxTemplate.GenerateType = "base64";
|
||||||
|
|
||||||
|
const placeholder: XlsxTemplate.TemplatePlaceholder = {
|
||||||
|
type: "normal",
|
||||||
|
full: true,
|
||||||
|
name: "foo",
|
||||||
|
key: "bar",
|
||||||
|
};
|
||||||
|
|
||||||
|
const namedTable: XlsxTemplate.NamedTable = {
|
||||||
|
filename: "table1.xml",
|
||||||
|
root: {} as any,
|
||||||
|
};
|
||||||
|
|
||||||
|
const outputByType: XlsxTemplate.OutputByType = {
|
||||||
|
base64: "base64string",
|
||||||
|
uint8array: new Uint8Array(),
|
||||||
|
arraybuffer: new ArrayBuffer(0),
|
||||||
|
blob: new Blob(),
|
||||||
|
nodebuffer: Buffer.from([]),
|
||||||
|
};
|
||||||
|
|
||||||
|
const rangeSplit: XlsxTemplate.RangeSplit = {
|
||||||
|
start: "A1",
|
||||||
|
end: "C3",
|
||||||
|
};
|
||||||
|
|
||||||
|
const referenceAddress: XlsxTemplate.ReferenceAddress = {
|
||||||
|
col: "A",
|
||||||
|
row: 1,
|
||||||
|
};
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"name": "commonjs-typings-test",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"type": "commonjs",
|
||||||
|
"scripts": {
|
||||||
|
"test:types": "tsc --noEmit",
|
||||||
|
"test": "npm i --no-save --no-package-lock && npm run test:types"
|
||||||
|
},
|
||||||
|
"keywords": [],
|
||||||
|
"author": "",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"xlsx-template": "file:../../../.."
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "es2015",
|
||||||
|
"module": "commonjs",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"noEmit": true,
|
||||||
|
"strict": false,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"types": ["node"]
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
import XlsxTemplate, {
|
||||||
|
GenerateOptions,
|
||||||
|
GenerateType,
|
||||||
|
TemplatePlaceholder,
|
||||||
|
NamedTable,
|
||||||
|
OutputByType,
|
||||||
|
RangeSplit,
|
||||||
|
ReferenceAddress,
|
||||||
|
} from "xlsx-template";
|
||||||
|
|
||||||
|
const generateType1: XlsxTemplate.GenerateType = "base64";
|
||||||
|
const generateType2: GenerateType = "base64";
|
||||||
|
|
||||||
|
const generateOptions1: XlsxTemplate.GenerateOptions = {
|
||||||
|
type: "base64",
|
||||||
|
};
|
||||||
|
const generateOptions2: GenerateOptions = {
|
||||||
|
type: "base64",
|
||||||
|
};
|
||||||
|
|
||||||
|
const placeholder1: XlsxTemplate.TemplatePlaceholder = {
|
||||||
|
type: "normal",
|
||||||
|
full: true,
|
||||||
|
name: "foo",
|
||||||
|
key: "bar",
|
||||||
|
};
|
||||||
|
const placeholder2: TemplatePlaceholder = {
|
||||||
|
type: "normal",
|
||||||
|
full: true,
|
||||||
|
name: "foo",
|
||||||
|
key: "bar",
|
||||||
|
};
|
||||||
|
|
||||||
|
const namedTable1: XlsxTemplate.NamedTable = {
|
||||||
|
filename: "table1.xml",
|
||||||
|
root: {} as any,
|
||||||
|
};
|
||||||
|
const namedTable2: NamedTable = {
|
||||||
|
filename: "table1.xml",
|
||||||
|
root: {} as any,
|
||||||
|
};
|
||||||
|
|
||||||
|
const outputByType1: XlsxTemplate.OutputByType = {
|
||||||
|
base64: "base64string",
|
||||||
|
uint8array: new Uint8Array(),
|
||||||
|
arraybuffer: new ArrayBuffer(0),
|
||||||
|
blob: new Blob(),
|
||||||
|
nodebuffer: Buffer.from([]),
|
||||||
|
};
|
||||||
|
const outputByType2: OutputByType = {
|
||||||
|
base64: "base64string",
|
||||||
|
uint8array: new Uint8Array(),
|
||||||
|
arraybuffer: new ArrayBuffer(0),
|
||||||
|
blob: new Blob(),
|
||||||
|
nodebuffer: Buffer.from([]),
|
||||||
|
};
|
||||||
|
|
||||||
|
const rangeSplit1: XlsxTemplate.RangeSplit = {
|
||||||
|
start: "A1",
|
||||||
|
end: "C3",
|
||||||
|
};
|
||||||
|
const rangeSplit2: RangeSplit = {
|
||||||
|
start: "A1",
|
||||||
|
end: "C3",
|
||||||
|
};
|
||||||
|
|
||||||
|
const referenceAddress1: XlsxTemplate.ReferenceAddress = {
|
||||||
|
col: "A",
|
||||||
|
row: 1,
|
||||||
|
};
|
||||||
|
const referenceAddress2: ReferenceAddress = {
|
||||||
|
col: "A",
|
||||||
|
row: 1,
|
||||||
|
};
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"name": "esm-typings-test",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"test:types": "tsc --noEmit",
|
||||||
|
"test": "npm i --no-save --no-package-lock && npm run test:types"
|
||||||
|
},
|
||||||
|
"keywords": [],
|
||||||
|
"author": "",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"xlsx-template": "file:../../../.."
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "es2015",
|
||||||
|
"module": "commonjs",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"noEmit": true,
|
||||||
|
"strict": false,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"types": ["node"]
|
||||||
|
},
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user