2
votes

I'm trying to export google spreadsheet to RSS using Google Apps script. Here is my google sheet:

https://docs.google.com/spreadsheets/d/15fwOeR6Jo4UadzOTlryTucgI3ZFZ5IVM16GDSwA0XE0/edit?usp=sharing

and here is my google apps script code:

function doGet() {
 var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/15fwOeR6Jo4UadzOTlryTucgI3ZFZ5IVM16GDSwA0XE0/edit#gid=0');
 SpreadsheetApp.setActiveSpreadsheet(ss);
 SpreadsheetApp.setActiveSheet(ss.getSheets()[0]);
 var title = ss.getSheets()[0].getRange("A1:A3").getValues();

var rss='<?xml version="1.0" encoding="utf-8"?>';
  rss+='<feed xmlns="http://www.w3.org/2005/Atom">';
  rss+='<title>testtile </title>';
  rss+='<link href="www.google.com"/>';
  rss+='<id>http://nexcono.appspot.com/frase/</id>';   
  for(var i=1;i<title.length;i++){
    rss+='<entry>';
    rss+='<title>sometitle '+title[i][0]+'</title>';
    rss+='<link rel="alternate" href="http://apple.com"/>';
    rss+='<id>http://nexcono.appspot.com/frase/'+'</id>';
    rss+='</entry>';
  }
   rss+='</feed>';
   return ContentService.createTextOutput(rss).setMimeType(ContentService.MimeType.RSS);
}

However, when I verify using https://validator.w3.org/feed/, it says it's not a vlid RSS :(

Thanks a lot,

2

2 Answers

4
votes

Templated HTML can be used to your script. Reference site is https://developers.google.com/apps-script/guides/html/templates

var title;
function doGet() {
  var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/15fwOeR6Jo4UadzOTlryTucgI3ZFZ5IVM16GDSwA0XE0/edit#gid=0');
  SpreadsheetApp.setActiveSpreadsheet(ss);
  SpreadsheetApp.setActiveSheet(ss.getSheets()[0]);
  title = ss.getSheets()[0].getRange("A1:A2").getValues();
  var template = HtmlService.createTemplateFromFile('template');
  var rss = template.evaluate();
  return ContentService.createTextOutput(rss.getContent()).setMimeType(ContentService.MimeType.XML);
}

Template html is as follows. The file name is "template.html". Please make this in a project with above script.

<rss version="2.0"
  xmlns:rss="http://purl.org/rss/1.0/"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>testtile </title>
<link href="www.google.com"/>
<id>http://nexcono.appspot.com/frase/</id>
<? for(var i=0;i<title.length;i++){ ?>
<entry>
<title>sometitle '+ <?= title[i][0] ?> +'</title>
<link rel="alternate" href="http://apple.com"/>
<id>http://nexcono.appspot.com/frase/'+'</id>
</entry>
<? } ?>
</channel>
</rss>
1
votes

The problem, as far as I can tell is this:

For security reasons, content returned by the Content service isn't served from script.google.com, but instead redirected to a one-time URL at script.googleusercontent.com. This means that if you use the Content service to return data to another application, you must ensure that the HTTP client is configured to follow redirects.1

The script works on browsers (Firefox, Opera, etc), but no RSS reader recognises it. So, there, not sure how to get around it, but you're half way there.