Pular para o conteúdo

Como adicionar dados extras a um feed ​

Os dados extras permitem enriquecer seus produtos com informações complementares que não estão no seu catálogo de origem. Você importa um conjunto de dados adicional, associa esses dados aos seus produtos e os campos extras ficam disponíveis em todo o Feedr, prontos para serem mapeados na saída do feed, usados em filtros ou referenciados em templates.

Isso é útil quando os dados de que você precisa estão fora da sua fonte principal de produtos. Por exemplo, margens ou preços de custo de uma exportação financeira, níveis de estoque de um sistema de armazém, ou etiquetas personalizadas e metadados mantidos em uma planilha separada.

Importar dados extras para um feed do Feedr

Código do Apps Script para o Google Sheets ​

Use e adapte este código no "Apps Script"

js
// ========= CONFIG: edit this block per feed =========
var CONFIG = {
	feedUrl: 'https://example.com/your-feed.xml', // the feed URL
	sheetName: 'Feed', // tab to write into (created if missing)
	itemElement: 'item', // "item" for RSS feeds, "entry" for Atom feeds
	fields: [
		// tag names you want, in column order
		'id',
		'item_group_id',
		'title',
		'custom_label_1',
		'custom_label_2',
		'custom_label_3',
		'custom_label_4',
	],
};
// ====================================================

function importFeed() {
	// Fetch
	var response = UrlFetchApp.fetch(CONFIG.feedUrl, {
		muteHttpExceptions: true,
	});
	if (response.getResponseCode() !== 200) {
		throw new Error('Fetch failed with status ' + response.getResponseCode());
	}

	// Parse and find all item/entry elements anywhere in the tree
	var doc = XmlService.parse(response.getContentText());
	var items = [];
	collectElements(doc.getRootElement(), CONFIG.itemElement, items);

	if (items.length === 0) {
		throw new Error(
			'No <' +
				CONFIG.itemElement +
				'> elements found. Check the "itemElement" setting.',
		);
	}

	// Build rows: header + data
	var rows = [CONFIG.fields.slice()]; // header row = field names
	for (var i = 0; i < items.length; i++) {
		var row = [];
		for (var f = 0; f < CONFIG.fields.length; f++) {
			row.push(getFieldValue(items[i], CONFIG.fields[f]));
		}
		rows.push(row);
	}

	// Write in one batch
	var ss = SpreadsheetApp.getActiveSpreadsheet();
	var sheet =
		ss.getSheetByName(CONFIG.sheetName) || ss.insertSheet(CONFIG.sheetName);
	sheet.clearContents();
	sheet.getRange(1, 1, rows.length, rows[0].length).setValues(rows);

	ss.toast(items.length + " items imported into '" + CONFIG.sheetName + "'.");
}

// Returns the text of the first child whose tag name matches, ignoring namespace.
function getFieldValue(item, fieldName) {
	var children = item.getChildren();
	for (var i = 0; i < children.length; i++) {
		if (children[i].getName() === fieldName) {
			return children[i].getText();
		}
	}
	return ''; // field missing on this item
}

// Recursively collects all elements with the given tag name (namespace ignored).
function collectElements(element, name, result) {
	var children = element.getChildren();
	for (var i = 0; i < children.length; i++) {
		var child = children[i];
		if (child.getName() === name) {
			result.push(child);
		} else {
			collectElements(child, name, result);
		}
	}
}