google sheets | get worksheet rows | add row data google sheet | Search

This code snippet is a unit test that verifies the ability to retrieve and process data from a specific Google Sheet using functions from a local module. It asserts the existence of a data sheet and checks if the processed data array contains at least one element.

Run example

npm run import -- "test google data sheet"

test google data sheet

var assert = require('assert');
var importer = require('../Core');
var getTemplates = importer.import("templates google sheet");
var getDataSheet = importer.import("google sheet array objects");

var docsId = '113mVIumItArQ_oXpfDRXP-2Kw2ms4t48oPJ68_p5P8k';

describe('get data from data sheet', () => {
    
    it('should process at least one data sheet', () => {
        return getTemplates(docsId)
            .then(t => {
                var key = Object.keys(t).filter(k => t[k].data)[0];
                assert(typeof key != 'undefined', 'should have at least one data sheet');
                return getDataSheet(t[key].data);
            })
            .then(data => {
                console.log(data.length);
                console.log(data);
                assert(data.length > 0, 'should have data');
            })
    })
    
    
})

What the code could have been:

// Import required modules
const assert = require('assert');
const Core = require('../Core');

// Import functions from Core module
const { getTemplates, getDataSheet } = Core.import({
  'templates google sheet': true,
  'google sheet array objects': true,
});

// Function to process data from Google Sheets
const processGoogleSheetData = async (docsId) => {
  // Get templates from Google Sheet
  const templates = await getTemplates(docsId);

  // Check if there are any data sheets
  if (!templates || Object.keys(templates).length === 0) {
    throw new Error('No data sheets found');
  }

  // Get the first data sheet with data
  const [key] = Object.keys(templates).filter((k) => templates[k].data);

  // Get data from the data sheet
  const data = await getDataSheet(templates[key].data);

  // Check if data was retrieved successfully
  assert(data.length > 0,'should have data');

  // Return retrieved data
  return data;
};

// Test suite for processing Google Sheet data
describe('get data from data sheet', () => {
  it('should process at least one data sheet', async () => {
    // Test ID for Google Sheet
    const docsId = '113mVIumItArQ_oXpfDRXP-2Kw2ms4t48oPJ68_p5P8k';

    // Process data from Google Sheet
    const data = await processGoogleSheetData(docsId);

    // Log retrieved data length and content
    globalThis.console.log(data.length);
    globalThis.console.log(data);
  });
});

// TODO: Implement logging and error handling for getTemplates and getDataSheet functions

This code snippet tests the functionality of retrieving and processing data from a specific Google Sheet.

Here's a breakdown:

  1. Dependencies:

  2. Test Setup:

  3. Test Case:

  4. Execution:

Let me know if you'd like a deeper dive into any specific part of the code!