google sheets | | test templates google sheet | Search

This code analyzes a Google Sheet document to identify and categorize its sheets as either templates or data sheets based on their titles. It then returns an organized object containing these categorized sheets.

Run example

npm run import -- "templates google doc"

templates google doc

var importer = require('../Core');
var getInfo = importer.import("get google sheet info");

function getTemplates(doc) {
    return getInfo(doc)
        .then(info => {
            console.log(`loaded doc: ${info.properties.title} ${info.spreadsheetId}`);
            return info.sheets.reduce((obj, worksheet) => {
                var layoutTitle = worksheet.properties.title.toLowerCase()
                    .replace(/\s*layout|\s*data|\s*template/ig, '')
                    .split(/\s+/ig).join('-');

                if( typeof obj[layoutTitle] == 'undefined' ) obj[layoutTitle] = {template: null, data: null};
                if( worksheet.properties.title.toLowerCase().includes('data') ) obj[layoutTitle].data = worksheet;
                else obj[layoutTitle].template = worksheet;
                return obj;
            }, {})
        })
}

module.exports = getTemplates;

What the code could have been:

const importCore = require('../Core');
const { getGoogleSheetInfo } = importCore.import('getGoogleSheetInfo');

/**
 * Retrieves templates from a Google Spreadsheet document.
 * 
 * @param {object} doc The Google Spreadsheet document.
 * @returns {Promise} An object containing templates and their corresponding data.
 */
async function getTemplates(doc) {
  try {
    const info = await getGoogleSheetInfo(doc);
    console.log(`Loaded doc: ${info.properties.title} ${info.spreadsheetId}`);

    // Initialize a map to store templates and their data
    const templates = info.sheets.reduce((map, worksheet) => {
      // Extract the layout title from the worksheet title
      const layoutTitle = worksheet.properties.title.toLowerCase().replace(/layout|\s*data|\s*template/ig, '').split(/\s+/ig).join('-');

      // Initialize the template or data for the layout title if it doesn't exist
      if (!map[layoutTitle]) map[layoutTitle] = { template: null, data: null };
      
      // Assign the worksheet to either template or data based on its title
      if (worksheet.properties.title.toLowerCase().includes('data')) map[layoutTitle].data = worksheet;
      else map[layoutTitle].template = worksheet;

      return map;
    }, {});

    return templates;
  } catch (error) {
    console.error(`Error retrieving templates: ${error.message}`);
    throw error;
  }
}

module.exports = getTemplates;

This code defines a function getTemplates that extracts template and data sheets from a Google Sheet document.

Here's a breakdown:

  1. Imports:

    • Imports a module named getInfo from the Core directory, which presumably handles fetching information about a Google Sheet.
  2. getTemplates Function:

    • Takes a Google Sheet document (doc) as input.
    • Calls the getInfo function to retrieve information about the document.
    • Processes the retrieved information to identify template and data sheets based on their titles.
    • Organizes the extracted sheets into an object where keys are derived from the sheet titles (removing "layout", "data", or "template" from the title) and values are the corresponding sheet objects.
    • Returns the object containing the extracted templates and data sheets.
  3. Export:

    • Exports the getTemplates function for use in other parts of the application.

In essence, this code provides a way to programmatically analyze a Google Sheet and categorize its sheets into templates and data based on their titles.