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

astro-sheet-loader

v1.0.4

Published

This package provides a Google Sheets loader for Astro. It allows you to load and parse publicly viewable Sheets, and use the data in your Astro site.

Downloads

234

Readme

Astro Sheet loader

This package provides a Google Sheets loader for Astro. It allows you to load and parse publicly viewable Sheets, and use the data in your Astro site.

Data is fetched using the Google Visualization API (gviz).

Sheet preparation

In Google Sheets, create a document, give it a name and write some tabular data.

Recommended: Click Format > Convert to Table, choose the appropriate column type and make sure that there are no different data types within the same column.

Edit the Share settings of the document, so that "Anyone on the internet with the link can view".

From the URL bar, copy the document ID and optionally the gid (grid ID) of the sheet you want to fetch data from.

Installation

npm install astro-sheet-loader

Usage

This package requires Astro 4.14.0 or later. You must enable the experimental content layer in Astro unless you are using version 5.0.0-beta or later. You can do this by adding the following to your astro.config.mjs:

export default defineConfig({
  // ...
  experimental: {
    contentLayer: true,
  },
});

You can then use the feed loader in your content configuration:

// src/content/config.ts
import { defineCollection } from "astro:content";
import { sheetLoader } from "astro-sheet-loader";
// optionally import a transform function
import { camelCase, snake_case } from "astro-sheet-loader";

const crm = defineCollection({
  loader: sheetLoader({
    document: "1wb2TbwRE-McOA663PGgf0InTsXC6b07ThEy_j6_MCDw",
  }),
  // if you don't define a schema yourself, it will be automatically generated
  // schema: z.object({
  //
  //})
});

export const collections = { crm };

You can then use these like any other content collection in Astro:

---
import type { GetStaticPaths } from "astro";
import { getCollection, type CollectionEntry } from "astro:content";

const entries: CollectionEntry<"crm">[] = await getCollection("crm");
---

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Astro Sheet Loader</title>
  </head>
  <body>
    <h1>Table generated from Astro Sheet Loader</h1>
    <table>
      <tr id="columns">
        {Object.keys(entries[0].data).map((column) => (
          <th>{column}</th>
        ))}
      </tr>
      {entries.map((row) => (
        <tr id={row.id}>
          {Object.values(row.data).map((value) => (
            <td>{value}</td>
          ))}
        </tr>
      ))}
    </table>
  </body>
</html>

Options

The sheetLoader function takes an options object with the following properties:

  • document (mandatory): The Google Sheet document ID, found in the URL
  • gid (optional, default: first sheet): Number identifying the sheet, found in the URL
  • sheet (optional, default: first sheet): The name of the sheet, found at the bottom of the page
  • range (optional, default: select all): Range of cells to delimit the table
  • query (optional, default: select all): SQL-like query to filter results in the provided range, using the original column names. Query documentation
  • allowBlanks (optional, default: all fields are mandatory): Allow for blank cells in your table
  • transformHeader (optional, default: false): Pass a function like camelCase or snake_case or a custom defined one to transform column names

Caveat

Types

The schema will be generated automatically if not user-specified when defining the collection in Astro.

However, you should be aware of the following:

  • numbers, booleans and strings are imported with their original type
  • currencies, percentages and monetary values are imported as raw numbers
  • dates, times, datetimes and durations are imported as strings as they appear in the sheet
  • checkboxes are imported as booleans
  • smart chips are imported as strings
  • functions are imported as their return value

Warnings and errors

  • if the provided sheet or gid do not exist, Google will silently return data for the default sheet (the one with gid 0)
  • if the range or query do not return any entries, the loader will throw a warning
  • if the document is not publicly viewable or does not exist, the loader will throw an error
  • if an entry does not respect the user-provided or auto-generated schema, the loader will throw an error

Inaccurate API response

Despite doing everything correctly, the Sheets API may return inaccurate data:

  • empty column names Example
  • all entries are part of the column name Example
  • unnecessary, completely blank columns Example

If you have an idea on how to tackle these cases, feel free to submit a PR!

Special Thanks

Kudos to Matt Kane for his collection of Astro Loaders