Last active
November 29, 2022 18:01
-
-
Save ronoaldo/9ca1efa85f083ef5ed2f to your computer and use it in GitHub Desktop.
Google Apps Script RSS Feed Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// O título do Feed | |
var FEED_TITLE = 'Meu Feed RSS'; | |
// Descrição do Feed | |
var FEED_DESC = 'Esse é o meu Feed RSS'; | |
// URL do Feed - O padrão aqui é usar a URL do próprio script | |
var FEED_URL = ScriptApp.getService().getUrl(); | |
// O ID da planilha que vai alimentar o FEED | |
var SHEET_ID = 'ID DA PLANILHA AQUI'; | |
function doGet(e) { | |
// Setup | |
var ss = SpreadsheetApp.openById(SHEET_ID); | |
data = ss.getSheetByName('Feed').getDataRange().getValues(), | |
xml = '<?xml version="1.0" encoding="UTF-8"?>'; | |
xml += '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">'; | |
xml += '<channel><title>' + FEED_TITLE + '</title>'; | |
xml += '<description>' + FEED_DESC + '</description>'; | |
xml += '<link>' + FEED_URL + '</link>'; | |
xml += '<atom:link href="' + FEED_URL + '" rel="self" type="application/rss+xml" />'; | |
// Lookup items from spreadsheet | |
for (var i=1; i < data.length; i++) { | |
var title = data[i][0], | |
link = data[i][1], | |
desc = data[i][2], | |
pubDate = data[i][3]; | |
xml += '<item>'; | |
xml += '<title>' + title + '</title>'; | |
xml += '<link>' + link + '</link>'; | |
xml += '<description>' + desc + '</description>'; | |
xml += '<pubDate>' + new Date(pubDate).toUTCString() + '</pubDate>'; | |
xml += '<guid>' + makeGUID(i) + '</guid>'; | |
xml += '</item>'; | |
} | |
xml += '</channel></rss>'; | |
var response = ContentService.createTextOutput(xml) | |
response.setMimeType(ContentService.MimeType.RSS); | |
return response | |
} | |
function makeGUID(pos) { | |
if (FEED_URL.indexOf('?') > 0) { | |
return FEED_URL += '&guid=' + pos; | |
} | |
return FEED_URL + '?guid=' + pos; | |
} |
@ancmto acabei conseguindo um exemplo. Faz uma cópia desta planilha e aí ao abrir sua cópia vai aparecer o menu XML depois do menu Ajuda no sheets. Ao rodar o script, ele irá gerar um arquivo persons.xml no seu drive. A primeira vez irá pedir permissão.
https://docs.google.com/spreadsheets/d/1p6aGoF1D968qA114vMGvq9sxHfF4adQ2EY8X5A3IH7U/edit?usp=sharing
Se preferir, crie sua própria planilha e nela crie uma aba chamada "Persons". As colunas/linhas serão convertidas em um XML automaticamente. Adicione este script na planilha:
function xmlFromSheet() {
// Setup
var ss = SpreadsheetApp.getActive(),
data = ss.getSheetByName('Persons').getDataRange().getValues(),
xml = '<?xml version="1.0" encoding="UTF-8"?>';
headers = data[0];
xml += "<persons>";
// Lookup items from spreadsheet
for (var i=1; i < data.length; i++) {
// For each column create a tag with the header name
var row = data[i];
xml += "<person>";
for (var j=0; j<row.length; j++) {
var tag = headers[j];
xml += `<${tag}>${row[j]}</${tag}>`;
}
xml += "</person>"
}
xml += "</persons>";
return xml;
}
function sheetToXML() {
var xml = xmlFromSheet();
var file = DriveApp.createFile("persons.xml", xml.toString(), "text/xml");
console.log(`File created at ${file.getDownloadUrl()}`);
}
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu("XML").addItem("Convert to XML", "sheetToXML").addToUi();
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Legal.... Mas já estou conseguindo fazer o que eu preciso, com o código que você mandou... Eu acho que tinha entendido o passa a passo, mas fui com calma, e está sendo bem fácil... Agora só estou organizando as informações da planilha.
Depois vou experimentar essa opção com a geração do arquivo no Drive.
Obrigado!