Skip to content

Instantly share code, notes, and snippets.

@smelendez
Created March 4, 2013 17:25
Show Gist options
  • Select an option

  • Save smelendez/5083897 to your computer and use it in GitHub Desktop.

Select an option

Save smelendez/5083897 to your computer and use it in GitHub Desktop.
/* Global Constants */
var cID = 0, cURL = 1, cName = 2, cContact = 3, cChanged=4, cChecksum=5, cMirrorPage=6; // column indices
var baseSiteUrl = 'https://sites.google.com/site/smelendeznicarmirror/'; // site to store mirrored content
var site = SitesApp.getSiteByUrl(baseSiteUrl);
var today = Date();
function test() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
GmailApp.sendEmail("smelendez@gmail.com","", sheet.getUrl());
}
/**
* Downloads the content and checks whether it's changed,
* sending an email if it has
* and updating the row and, optionally, mirror page
*/
function processRow(rowValues) {
var url = rowValues[cURL];
var name = rowValues[cName];
var rowID = rowValues[cID];
if (!url)
{
// Not much we can do here
return rowValues;
}
var response = UrlFetchApp.fetch(url);
var oldChecksum = rowValues[cChecksum];
var contentText = response.getContentText(), content = response.getContent();
var newChecksum = String(Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, contentText));
if (newChecksum != oldChecksum)
{
// File has changed
var mirrorPageUrl = rowValues[cMirrorPage];
var page;
var contacts = rowValues[cContact];
if (!mirrorPageUrl) {
// Need to make a mirror page
page = site.createFileCabinetPage(name, rowID, "");
rowValues[cMirrorPage] = mirrorPageUrl = page.getUrl();
}
else page = SitesApp.getPageByUrl(mirrorPageUrl);
page.addHostedAttachment(Utilities.newBlob(content, "application/octet-stream", url + ' ' + today), today); // Add it to the site as an attachment
rowValues[cChanged] = today;
rowValues[cChecksum] = newChecksum;
if (contacts)
{
// Email the appropriate people
GmailApp.sendEmail(contacts, "Change notification " + url, "See the latest version at " + mirrorPageUrl);
}
}
return rowValues;
}
function processRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var colnames = values[0];
newvalues = [colnames];
for (var i = 1; i <= numRows - 1; i++) {
var row = values[i];
row = processRow(row);
newvalues.push(row);
}
// Update with new values
rows.setValues(newvalues);
};
/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the readRows() function specified above.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Process All Rows",
functionName : "readRows"
}, ];
sheet.addMenu("Mirror Tool", entries);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment