Clarify exported XlsxTemplate types in the declaration file (#221)

This commit is contained in:
Yukihiro Hasegawa
2026-03-10 00:13:19 +09:00
committed by GitHub
parent 8757308c9a
commit 7a95c8f417
9 changed files with 228 additions and 56 deletions
+19 -22
View File
@@ -1,24 +1,22 @@
import * as etree from "elementtree";
import * as JSZip from "jszip";
export interface TemplatePlaceholder{
declare namespace XlsxTemplate {
interface TemplatePlaceholder {
type: string;
string?: string;
full: boolean;
name: string;
key: string;
placeholder?: string
placeholder?: string;
subType?: string;
}
export interface NamedTable{
interface NamedTable {
filename: string;
root: etree.Element;
}
namespace XlsxTemplate {
}
interface OutputByType {
base64: string;
uint8array: Uint8Array;
@@ -29,26 +27,25 @@ interface OutputByType {
type GenerateType = keyof OutputByType;
export type GenerateOptions<T extends GenerateType = GenerateType> = {
type GenerateOptions<T extends GenerateType = GenerateType> = {
type: T;
};
interface RangeSplit
{
interface RangeSplit {
start: string;
end: string;
}
interface ReferenceAddress
{
interface ReferenceAddress {
table?: string;
colAbsolute?: boolean;
col: string;
rowAbsolute?: boolean;
row: number;
}
}
class XlsxTemplate
declare class XlsxTemplate
{
public readonly sharedStrings: string[];
protected readonly workbook: etree.ElementTree;
@@ -62,16 +59,16 @@ class XlsxTemplate
public copySheet(sheetName : string, copyName : string, binary? : boolean) : this;
public loadTemplate(data : Buffer) : 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 replaceString(oldString : string, newString : string) : number; // returns idx
public stringIndex(s : string) : number; // returns idx
public writeSharedStrings() : void;
public splitRef(ref : string) : ReferenceAddress;
public joinRef(ref : ReferenceAddress) : string;
public splitRef(ref : string) : XlsxTemplate.ReferenceAddress;
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 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 numToChar(num : number) : string;
public nextCol(ref : string) : string;
@@ -79,9 +76,9 @@ class XlsxTemplate
public stringify(value : Date | number | boolean | string) : string;
public isWithin(ref : string, startRef : string, endRef : string) : boolean;
public isRange(ref : string) : boolean;
public splitRange(range : string) : RangeSplit;
public joinRange(range : RangeSplit) : string;
public extractPlaceholders(templateString : string) : TemplatePlaceholder[];
public splitRange(range : string) : XlsxTemplate.RangeSplit;
public joinRange(range : XlsxTemplate.RangeSplit) : string;
public extractPlaceholders(templateString : string) : XlsxTemplate.TemplatePlaceholder[];
// need typing properly
protected _rebuild() : void;
@@ -100,8 +97,8 @@ class XlsxTemplate
protected substituteTableColumnHeaders(tables : any, substitutions : any) : void;
protected insertCellValue(cell : any, substitution : any) : string;
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 substituteImage(cell : any, string : string, placeholder: TemplatePlaceholder, substitution : any, drawing : etree.ElementTree) : boolean
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: XlsxTemplate.TemplatePlaceholder, substitution : any, drawing : etree.ElementTree) : boolean
protected substituteImageInCell(cell : any, substitution : any) : boolean;
protected cloneElement(element : any, deep? : any) : any;
protected replaceChildren(parent : any, children : any) : void;
+2 -1
View File
@@ -69,7 +69,8 @@
},
"scripts": {
"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"
},
"readme": "",
+13
View File
@@ -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() {
var t = new XlsxTemplate();
+33
View File
@@ -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,
};
+16
View File
@@ -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:../../../.."
}
}
+11
View File
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"esModuleInterop": true,
"noEmit": true,
"strict": false,
"skipLibCheck": true,
"types": ["node"]
},
}
+74
View File
@@ -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,
};
+16
View File
@@ -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:../../../.."
}
}
+11
View File
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"esModuleInterop": true,
"noEmit": true,
"strict": false,
"skipLibCheck": true,
"types": ["node"]
},
}