Skip to content

Library for handling data mapping between Javascript objects and Google Sheets

License

Notifications You must be signed in to change notification settings

gardusig/shit-db

Repository files navigation

ShitDB

ShitDB is a JavaScript library designed to streamline data mapping between JavaScript objects and Google Sheets.

Description

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.

Installation

To use ShitDB, follow these steps:

  1. Install CLASP (Google's Command Line Apps Script Projects tool) if you haven't already.

    npm install -g @google/clasp
  2. Clone this repository to your local machine.

    git clone https://github.com/yourusername/shit-db.git
    cd shit-db
  3. Authenticate with your Google account.

    clasp login
  4. Deploy the code to your Google Apps Script project.

    clasp push

Usage

ObjectToSheetMapper

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

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.

Methods

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.

SheetToObjectMapper

The SheetToObjectMapper class provides functionality to map Google Sheets data to JavaScript objects. It allows you to retrieve objects from a specified sheet.

Constructor

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.

Methods

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.

Types

SheetCellValue

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;

SheetRow

Represents a row of values in a Google Sheets spreadsheet. It is an array of SheetCellValue.

type SheetRow = SheetCellValue[];

GenericObject

Represents a generic JavaScript object, where keys are strings representing column names and values are SheetCellValue.

type GenericObject = Record<string, SheetCellValue>;

HeaderMap

Represents a mapping of column names to their respective indices in the sheet.

type HeaderMap = Record<string, number>;

Examples

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);

Additional Notes

  • 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.

About

Library for handling data mapping between Javascript objects and Google Sheets

Resources

License

Stars

Watchers

Forks

Packages

No packages published