Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Grabbing full text from urls and creating a .doc/html from a template using Google Apps Script
function doGet(e){
// get some variables passed from the querystring
var project = e.parameter.title;
var range = e.parameter.range;
var sheet = e.parameter.sheet;
// Grab a basic html template to fill in the blanks - see
var t = HtmlService.createTemplateFromFile("reportTemplate");
// some bits of code to grab the source urls
var doc = SpreadsheetApp.openById(ScriptProperties.getProperty('active'));
var sheet = doc.getSheetByName(sheet);
var data = sheet.getRange(range).getValues();
var row = data[0];
t.title = project; // exposing the project title to the template
var options = {"method" : "get"};
var output = [];
// for each url fetch the post content
for (i in row){
if (row[i] !="NONE"){
var url = row[i]+"?feed=rss2&withoutcomments=1";
// cheating by using a Yahoo Pipe to convert xml into json
url = ""+encodeURIComponent(url);
// some caching to improve performance
var cache = CacheService.getPrivateCache(); // using Cache service to prevent too many urlfetch
var cached = cache.get(row[i]);
if (cached != null) { // if value in cache return it
} else {
// if no cache fetch the data
var response = UrlFetchApp.fetch(url, options);
var item = Utilities.jsonParse(response.getContentText());
var res = item.value.items[0]["content:encoded"];
output.push(res || "Error getting content"); // collecting the data ready to pass to the template
cache.put(row[i], res, 3600); // cache the result for next time
Utilities.sleep(5000); // sleep to prevent service invoked too many times
t.content = output; // exposing the project title to the template
// app evaluates template with passed data, sets as XML (not really needed) and forces download with .doc extension
return ContentService.createTextOutput(t.evaluate().getContent())
<html xmlns="">
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<title>JISC final report template</title>
h1 {
font-family:"Arial", "sans-serif";
h2 {
font-family:"Arial", "sans-serif";
h3 {
font-family:"Arial", "sans-serif";
p.MsoHeader, li.MsoHeader, div.MsoHeader {
tab-stops:center 216.0pt right 432.0pt;
font-family:"Arial", "sans-serif";
p.MsoFooter, li.MsoFooter, div.MsoFooter {
tab-stops:center 216.0pt right 432.0pt;
font-family:"Arial", "sans-serif";
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate {
font-family:"Tahoma", "sans-serif";
<body lang=EN-GB>
<div class=WordSection1>
<h1><img width=99 height=57
src="" v:shapes="_x0000_i1025"></h1>
<h1>JISC Final Report </h1>
<p style='line-height:12.0pt'><b><i>Before completing this template
please note:</i></b></p>
<ul type=disc>
<li><i>The <u>Project Management Guidelines</u> explain the purpose of final reports.</i></li>
<li><i>Fill in the information for the header,
e.g. project acronym, version, and date.</i></li>
<li><i>Prepare a cover sheet using the <u>cover
sheet template</u> and attach to final report.</i></li>
<li style='line-height:12.0pt; '><i>This template
is for completion by JISC funded project managers</i></li>
<li style='line-height:12.0pt; '><i>Text in
italics is explanatory and should be deleted in completed documents.</i></li>
<h2>Title Page</h2>
<p><i><?= title || "N/A" ?></i></p>
<h2>Project Plan</h2>
<div><?!= content[0] ?></div>
<div><?!= content[1] ?></div>
<div><?!= content[2] ?></div>
<div><?!= content[3] ?></div>
<div><?!= content[4] ?></div>
<h2>Outputs List</h2>
<div><?!= content[5] ?></div>
<h2>Lessons Learned</h2>
<div><?!= content[6] ?></div>
<div><?!= content[7] ?></div>
<h2>Grand Finale</h2>
<div><?!= content[8] ?></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment