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.
npm run import -- "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;
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
This code defines a function getTemplates
that extracts template and data sheets from a Google Sheet document.
Here's a breakdown:
Imports:
getInfo
from the Core
directory, which presumably handles fetching information about a Google Sheet.getTemplates
Function:
doc
) as input.getInfo
function to retrieve information about the document.Export:
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.