Last active
December 21, 2015 00:59
-
-
Save sebnash/6224259 to your computer and use it in GitHub Desktop.
Google Apps Script that writes to a Spreadsheet a list of your Sites and when they were last edited (and the page that was edited).
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
/** | |
* For more information on using the Sites API, see | |
* https://developers.google.com/apps-script/service_sites | |
* | |
* TODO: "Edited by" column (is this possible?) | |
* Getting inconsistent results via Sites Services, may move to Sites API: | |
* https://developers.google.com/google-apps/sites/ | |
* Can't use yet - GAS helper methods don't support OAuth 2.0 | |
*/ | |
var DOMAIN = "example.com"; // your Google Apps domain | |
// We clear and setup the sheet each time the script runs | |
function setUpSheet(sheet, columnNames) { | |
sheet.clear(); | |
// Set Header Row | |
var headersRange = sheet.getRange(1, 1, 1, columnNames.length); | |
sheet.setFrozenRows(1); | |
headersRange.setBackgroundColor("#DDDDDD"); | |
headersRange.setFontWeight("bold"); | |
headersRange.setHorizontalAlignment("center"); | |
headersRange.setValues([columnNames]); | |
} | |
function recordEdits() { | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = ss.getSheets()[0]; | |
var columnNames = ["Site", "Last Edited", "Page Edited"]; | |
setUpSheet(sheet, columnNames); | |
var sites = SitesApp.getAllSites(DOMAIN); | |
for (var site in sites) { | |
var pages = sites[site].getAllDescendants(); | |
pages.sort(comparePageEdits).reverse(); // sort by pages' last edited date | |
// First array item is now the most recently edited page | |
// Make nice links via Sheet's HYPERLINK function | |
var siteLink = "=hyperlink(\"" + sites[site].getUrl() + "\";\"" + sites[site].getTitle() + "\")"; | |
var latestPageLink = "=hyperlink(\"" + pages[0].getUrl() + "\";\"" + pages[0].getTitle() + "\")"; | |
sheet.appendRow([siteLink, pages[0].getLastEdited(), latestPageLink]); | |
} | |
// Sort by Last Edited column, most recent at top | |
sheet.sort(2, false); | |
} | |
// Custom comparison method | |
function comparePageEdits(a,b) { | |
if (a.getLastEdited() < b.getLastEdited()) | |
return -1; | |
if (a.getLastEdited() > b.getLastEdited()) | |
return 1; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment