ShitDB is a JavaScript library designed to streamline data mapping between JavaScript objects and Google Sheets.
ShitDB facilitates easy and efficient mapping of data between JavaScript objects and Google Sheets. It provides utilities to serialize and deserialize data, allowing for seamless integration with Google Sheets for data storage and retrieval.
To use ShitDB, follow these steps:
-
Install CLASP (Google's Command Line Apps Script Projects tool) if you haven't already.
npm install -g @google/clasp
-
Clone this repository to your local machine.
git clone https://github.com/yourusername/shit-db.git cd shit-db
-
Authenticate with your Google account.
clasp login
-
Deploy the code to your Google Apps Script project.
clasp push
The ObjectToSheetMapper
class provides functionality to map JavaScript objects to Google Sheets. It allows you to define a sheet with a specific header and append objects to it.
constructor(sheetName: string, header: string[], spreadsheetIdOrURL?: string)
sheetName
: The name of the sheet to which objects will be appended.header
: An array containing the column headers for the sheet.spreadsheetIdOrURL
: (Optional) The ID or URL of the Google Spreadsheet. If not provided, it defaults to the active spreadsheet.
appendObject(obj: GenericObject): void
Appends a single object to the sheet.
appendObjects(objs: GenericObject[]): void
Appends an array of objects to the sheet.
trimRows(): void
Removes empty rows from the bottom of the sheet.
trimColumns(): void
Removes empty columns from the right of the sheet.
The SheetToObjectMapper
class provides functionality to map Google Sheets data to JavaScript objects. It allows you to retrieve objects from a specified sheet.
constructor(sheetName: string, spreadsheetIdOrURL?: string)
sheetName
: The name of the sheet from which objects will be retrieved.spreadsheetIdOrURL
: (Optional) The ID or URL of the Google Spreadsheet. If not provided, it defaults to the active spreadsheet.
getAllObjects(): GenericObject[]
Retrieves all objects from the sheet.
getObject(rowIndex: number): GenericObject | null
Retrieves a single object from the specified row index.
getObjectBatch(startRowIndex: number, finishRowIndex: number): GenericObject[]
Retrieves a batch of objects from the specified range of row indices.
getHeaderMap(): HeaderMap | null
Retrieves the header map, which maps column names to their respective indices in the sheet.
Represents a value that can be stored in a Google Sheets cell. It can be a string, number, boolean, or Date.
type SheetCellValue = string | number | boolean | Date;
Represents a row of values in a Google Sheets spreadsheet. It is an array of SheetCellValue.
type SheetRow = SheetCellValue[];
Represents a generic JavaScript object, where keys are strings representing column names and values are SheetCellValue.
type GenericObject = Record<string, SheetCellValue>;
Represents a mapping of column names to their respective indices in the sheet.
type HeaderMap = Record<string, number>;
Define a function to get a list of objects from a sheet
export function getObjectList<T>(sheetName: string): T[] {
const sheetMapper = new ShitDb.SheetToObjectMapper.SheetToObjectMapper(sheetName);
const objectList = sheetMapper.getAllObjects() as T[];
return objectList;
}
// Example usage:
interface StockTransaction {
ticker: string;
side: string;
quantity: number;
total: string;
price: string;
}
const stockTransactions = getObjectList<StockTransaction>('StockTransactions');
console.log(stockTransactions);
- Ensure that the necessary permissions are granted to the Google Sheets API for the script to access the spreadsheet.
- This library assumes familiarity with Google Apps Script and basic JavaScript concepts.