Skip to content

Instantly share code, notes, and snippets.

@tomjn
Created Aug 22, 2012
Embed
What would you like to do?
GDocs Spreadsheet Check WordPress Sites for Updates
function getWPVersion(url){
var newurl = url + "/readme.html";
var hdoc = null;
try{
hdoc = UrlFetchApp.fetch(newurl);
if(hdoc.getResponseCode() != 200){
return "Cannot retrieve version";
}
}catch(e){
return "Cannot retrieve version";
}
var text = hdoc.getContentText();
var xdoc = Xml.parse(text,true);
var doc = xdoc.getElement();
var logo = doc.getElement("body").getElement("h1");
return logo.getText().replace(/^\s+|\s+$/g,"").replace("Version ","");
}
function checkVersions() {
var latest = latestVersion();
var sheet = SpreadsheetApp.getActiveSheet();
var ncell = sheet.getRange("D1:D1").setValue(latest);
for(var i = 2; i < 100; i++){
var range = sheet.getRange("A"+i+":A"+i);
var site1 = range.getValues();
if(site1 == ""){
break;
}
var vers = getWPVersion(site1);
var ncell = sheet.getRange("B"+i+":B"+i);
ncell.setValue(vers);
if(vers != latest){
ncell.setFontColor("#aa5500");
} else {
ncell.setFontColor("#00aa00");
}
}
}
function onOpen() {
var menu = [
{name: "Check Versions", functionName: "checkVersions"}
];
SpreadsheetApp.getActiveSpreadsheet()
.addMenu("TomJN Scripts", menu);
}
function latestVersion(){
var url = "http://api.wordpress.org/core/version-check/1.5/";
var hdoc = null;
try{
hdoc = UrlFetchApp.fetch(url);
if(hdoc.getResponseCode() != 200){
return "Cannot retrieve version";
}
}catch(e){
return "Cannot retrieve version";
}
var text = hdoc.getContentText();
var lines = text.split("\n");
return lines[3];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment