Note
See the Writing a custom parser section for how to write a custom parser.
A parser that parses public Google Sheets. This parser uses public-google-sheets-parser.
import { generateDtsFile } from 'google-spreadsheet-dts';
import { publicGoogleSheetsParser } from 'google-spreadsheet-dts/parser';
// Define parser
const parser = publicGoogleSheetsParser(
// You can pass an instance of PublicGoogleSheetsParser
{
spreadsheetId: '1j23zhzHcPd_LzDQ7uPrXgMJfPoZYs289boUKoKnAjUo',
},
{
path: ['Key', 'Property'],
typeName: 'Type',
},
);
// Generate d.ts file
generateDtsFile({
name: 'GoogleSheets',
directory: resolve(__dirname, '../src'),
parser,
});
function publicGoogleSheetsParser(
instanceOrOptions: PublicGoogleSheetsParser | PublicGoogleSheetsParserOptions,
params: {
path: string[];
typeName: string;
},
): Parser;
interface PublicGoogleSheetsParserOptions {
spreadsheetId: string;
sheetInfo:
| SheetName
| {
sheetName?: SheetName;
sheetId?: GID;
};
}
type SheetName = string;
type GID = string;
instanceOrOptions
: An instance ofPublicGoogleSheetsParser
or an object with the following properties:spreadsheetId
: The ID of the Google SheetssheetInfo
: The sheet name or an object with the following properties:sheetName
: The sheet namesheetId
: The sheet ID
path
: List of column names where object property names existstypeName
: Column name where the type name exists
For example, given the following Google Sheets:
Key | Property | Type |
---|---|---|
key1 | property1 | "type1" |
key2 | property2 | "type2" |
key3 | property3 | MyType |
If path
is ['Key', 'Property']
and typeName
is Type
, the return will look like this:
({
key1: {
property1: "'type1'",
},
key2: {
property2: "'type2'",
},
key3: {
property3: 'MyType',
},
});
A parser that parses private Google Sheets. This parser uses google-spreadsheet.
import { generateDtsFile } from 'google-spreadsheet-dts';
import { privateGoogleSheetsParser } from 'google-spreadsheet-dts/parser';
import { JWT } from 'google-auth-library';
// Define parser
const parser = privateGoogleSheetsParser(
// You can pass an instance of GoogleSpreadsheetWorksheet (https://theoephraim.github.io/node-google-spreadsheet/#/classes/google-spreadsheet-worksheet)
{
auth: new JWT(/* ... */),
spreadsheetId: '...',
sheetInfo: 0, // Index of the sheet
},
{
path: ['Key', 'Property'],
typeName: 'Type',
},
);
// Generate d.ts file
generateDtsFile({
name: 'GoogleSheets',
directory: resolve(__dirname, '../src'),
parser,
});
Note that you need to pass the sheet instance.
function privateGoogleSheetsParser(
instanceOrOptions:
| GoogleSpreadsheetWorksheet
| PrivateGoogleSheetsParserOptions,
params: {
path: string[];
typeName: string;
},
): Parser;
interface PrivateGoogleSheetsParserOptions {
auth: JWT;
spreadsheetId: string;
sheetInfo: SheetName | SheetIndex;
}
type SheetName = string;
type SheetIndex = number;
instanceOrOptions
: An instance ofGoogleSpreadsheetWorksheet
or an object with the following properties:auth
: An instance ofJWT
fromgoogle-auth-library
spreadsheetId
: The ID of the Google SheetssheetInfo
: The sheet name or the index of the sheet
path
: List of column names where object property names existstypeName
: Column name where the type name exists
See publicGoogleSheetsParser API for a detailed description of the path
and typeName
behavior.
The parser is a function that returns an object. The object structure can be arbitrarily defined and is passed to the createDtsBody
function.
The object property value becomes the type name. This can also be arbitrarily as well, but they will all be converted to string types.
If you want the generated type to point to an actual type, just pass the type name as a string.
({
key: {
property: 'MyType' | 'YourType',
},
});
// generated.d.ts
declare global {
export interface GoogleSheets {
key: {
property: MyType | YourType;
};
}
}
If you want the generated type to be a string type, pass a string wrapped in quotes (''
or ""
).
({
key: {
property: "'MyType' | 'YourType'",
},
});
// generated.d.ts
declare global {
export interface GoogleSheets {
key: {
property: 'MyType' | 'YourType';
};
}
}
These can be mixed and matched.
({
key: {
property: 'MyType' | "'YourType'",
},
});
// generated.d.ts
declare global {
export interface GoogleSheets {
key: {
property: MyType | 'YourType';
};
}
}
This utility function takes FilledData
type data and converts it to an object
type. It can be useful when creating parsers.
import { filledDataToObject } from 'google-spreadsheet-dts/parser';
filledDataToObject(
[
{ Key: 'key1', Property: 'property1', Type: 'type1' },
{ Key: 'key2', Property: 'property2', Type: 'type2', Other: 'other' },
{ Key: 'key3', Property: 'property3', Type: 'MyType' },
],
['Key', 'Property'],
'Type',
);
// Returns
({
key1: {
property1: 'type1',
},
key2: {
property2: 'type2',
},
key3: {
property3: 'MyType',
},
});
Type definition:
type FilledData = Record<string, string>[];
type Result = {
[key: string]: string | Result;
};
function filledDataToObject(
filledData: FilledData,
path: string[],
typeName: string,
): Result;