convert spreadsheet | get sheet purchases | filter data sheet based on url | Search

The code imports necessary functions from the importer module and defines an asynchronous importSheet function that retrieves information from a Google Sheet, including templates, properties, and resources. The function exports the importSheet function as a module, making it available for use in other scripts.

Run example

npm run import -- "sheet marketing import"

sheet marketing import

var importer = require('../Core');
var getSheet = importer.import("get sheet purchases");
var getTemplates = importer.import("templates google sheet");
var wrapTemplate = importer.import("output google sheet template");
var getTemplateProperties = importer.import("google sheet template properties");
var collectTemplateResources = importer.import("collect google sheet resources");
var getTemplateByUrl = importer.import("convert sheet helper functions");

async function importSheet(link, domain) {
    var properties = {}, templates, key
    var match = await getSheet(link, domain)
    console.log(match)
    domain = domain || match.bucket
    templates = await getTemplates(match.sheet)
    key = getTemplateByUrl(templates, '/')
    await getTemplateProperties(key, properties, templates)
    var page = await wrapTemplate('', key, properties[key], properties)
    var resources = (await collectTemplateResources(key, page, properties, templates, domain))
        .filter((cur, i, arr) => arr.indexOf(cur) == i)
        .filter(r => !r.includes(':') && r.includes('.'))
    return resources
}

module.exports = importSheet;

What the code could have been:

const { getSheet, getTemplates, wrapTemplate, getTemplateProperties, collectTemplateResources, getTemplateByUrl } =
  require('../Core').import({
    'get sheet purchases': async () => ({ sheet: '', bucket: '' }),
    templates: 'google sheet',
    'output google sheet template': async () => ({}),
    'google sheet template properties': async () => ({}),
    'collect google sheet resources': async () => ({}),
    'convert sheet helper functions': async () => ({}),
  });

async function importSheet(link, domain = '') {
  if (!link) {
    throw new Error('Link is required');
  }

  const { sheet, bucket } = await getSheet(link, domain);
  domain = domain || bucket;

  const templates = await getTemplates(sheet);
  const key = getTemplateByUrl(templates, '/');

  if (!key) {
    throw new Error('Template not found');
  }

  await getTemplateProperties(key, {}, templates);
  const page = await wrapTemplate('', key, {}, {});

  const resources = await collectTemplateResources(key, page, {}, templates, domain);
  const uniqueResources = [...new Set(resources.map(String))];
  const filteredResources = uniqueResources.filter((resource) => {
    if (!resource.includes(':')) {
      return resource.includes('.');
    }
    return false;
  });

  return filteredResources;
}

module.exports = importSheet;

Code Breakdown

Variables and Imports

importSheet Function

Exports