npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fumigoro/gas-spreadsheet-object-mapper

v0.1.5

Published

Typed object mapper for Spreadsheet on Google Apps Script

Downloads

16

Readme

gas-spreadsheet-object-mapper

English | 日本語

npm version

Google Apps Scriptにて、スプレッドシートに対する型付きのCRUDを実現するパッケージです。

型定義にはTypeScriptのtypeではなくclassを使用するため、JavaScriptでも恩恵を受けることができます。

Usage

前提

  • claspを導入済みでローカルでGoogleAppsScriptの開発を行う環境がある。
  • esbuild等のバンドラーを導入済みでnpmパッケージを使用したコードをビルドできる環境がある。

スプレッドシートにシートを追加

スプレッドシートに読み書き対象となる表を作成します。

  • 1行目に必ずカラム名を記入してください。
  • プライマリキーとなるカラム(ユニークなstringまたはnumber)を1つ用意する必要があります。

image

Install

npm install @fumigoro/gas-spreadsheet-object-mapper

データのclassを定義

@SpreadSheetColumnデコレーターを使用して、各メンバとスプレッドシート上のカラム名をマッピングします。

  • メンバー変数idは必ず定義し、プライマリキーとなるスプレッドシートのカラム名とマッピングしてください。idの型はstringまたはnumberのみ利用できます。
  • カラム名には日本語も使用できます。
  • スプレッドシート側のカラムの順序とメンバーの定義順序を一致させる必要はありません。
import { SpreadSheetColumn } from "@fumigoro/gas-spreadsheet-object-mapper";

class User {
  @SpreadSheetColumn('ID')
  id: number;

  @SpreadSheetColumn('Name')
  name: string;

  @SpreadSheetColumn('Age')
  age: number;

  // このようにして、スプレッドシートから値を読み込む際のパース関数や書き込む際のシリアライズ関数を追加することも可能です。
  @SpreadSheetColumn('CreatedAt', {
    parser: (value: any) => new Date(value),
    serializer: (value: any) => value.toISOString()
  })
  createdAt: Date;
}

SpreadSheetMapperインスタンスの作成

定義したデータクラスを用いてSpreadSheetMapperインスタンスを作成します。

SpreadSheetMapperはそのコンストラクタ内で全ての行のデータを読み出してメモリ上に持ちます。

import { SpreadSheetMapper } from "@fumigoro/gas-spreadsheet-object-mapper";

const spreadSheetId = 'YOUR_SPREADSHEET_ID';
const sheetName = 'Users';
const userSheetMapper = new SpreadSheetMapper(spreadSheetId, sheetName, User);

データのCRUD

list()

SpreadSheetMapperインスタンスがメモリ上に持つ全てのデータを返します。

const users = userSheetMapper.list();

get()

SpreadSheetMapperインスタンスがメモリ上に持つ全てのデータから、idが一致するデータを返します。

const user = userSheetMapper.get('target_user_id');

updateAndSave()

idが一致する行を更新し、スプレッドシートに書き込みます。 SpreadSheetMapperインスタンスがメモリ上にもつデータも更新されます。

userSheetMapper.updateAndSave({ id: 1, name: 'Jone', age: 36, createdAt: new Date() });

createAndSave()

新しいデータを挿入し、スプレッドシートに書き込みます。

userSheetMapper.createAndSave({ id: 4, name: 'Bob', age: 10, createdAt: new Date() });

deleteAndSave()

idが一致するデータを削除します。 SpreadSheetMapperインスタンスが持つデータとスプレッドシート両方から削除されます。 スプレッドシートにおいても行ごと削除され上に詰められます。

userSheetMapper.deleteAndSave(4);