Vendure Google Sheet Loader plugin
npm install @pinelab/vendure-plugin-google-sheet-loaderVendure Google Sheet Loader plugin
Section titled “Vendure Google Sheet Loader plugin”This plugin allows you to load data from a Google Sheet and handle the data in Vendure. For example, to load products and prices into Vendure. You can define a single action per channel. This means each channel can have it’s own sheet, and it’s own way of handling the data from that sheet.
Getting started
Section titled “Getting started”You need to have a Google API key to use this plugin. You can get one here.
- Add this to your plugin in
vendure-config.ts:
import { GoogleSheetLoaderPlugin } from '@pinelab/vendure-plugin-google-sheet-loader';
plugins: [ GoogleSheetLoaderPlugin.init({ strategies: [new MyDataLoadingStrategy()], googleApiKey: 'test-api-key', }), ...]- The “Load Google Sheet data” action is added as a dropdown item in the product list page’s action bar via the React Dashboard extension API — no Admin UI compilation step is needed.
Before you can use this plugin, you need to implement your own data loading strategy. This strategy determines what will be done with the data from the sheet.
import { RequestContext, Injector } from '@vendure/core';import { GoogleSheetDataStrategy, SheetContent,} from '@pinelab/vendure-plugin-google-sheet-loader';
export class MyDataLoadingStrategy implements GoogleSheetDataStrategy { code = 'MyDataLoadingStrategy';
getSheetMetadata() { return { sheets: ['My tab'], spreadSheetId: '1gqZpM-Ksxc-xxKJAKNALAFNLLkksdalakml', }; }
validateSheetData( ctx: RequestContext, sheets: SheetContent[] ): boolean | string { // Single sheet example const sheet = sheets[0]; const headerRow = sheet.data[0]; if (headerRow[0] !== 'My Header Column') { return 'Expected "My Header Column" for column 1'; } return true; }
async handleSheetData( ctx: RequestContext, injector: Injector, sheets: SheetContent[] ): Promise<string> { // Do whatever you want with the data here // This part is executed in the worker return `Successfully processed ${sheets[0].data.length} rows`; }}How it works
Section titled “How it works”When the data loading is triggered from the product overview page in the admin UI, the plugin will:
- Get the sheet metadata from the strategy
- Fetch the content of the sheets
- Validate the data, and give feedback to the admin user
- If valid, a job will be created to handle the data
