Skip to content

Instantly share code, notes, and snippets.

@roktas
Created June 19, 2013 16:20
Show Gist options
  • Save roktas/5815637 to your computer and use it in GitHub Desktop.
Save roktas/5815637 to your computer and use it in GitHub Desktop.
function showFolderInSite() {
var done = false;
// The while loop and try - catch statement are used to automatically retry if
// there's an issue during the execution (common issue: SitesApp: Internal error).
while (!done) {
try {
var files = DocsList.getFolderById('0B_F4bEJWqt_VRnUzRElES2JoMXM').getFiles();
var page = SitesApp.getPageByUrl('https://sites.google.com/a/bil.omu.edu.tr/belge/a');
var listItems = page.getListItems();
for (i in listItems) {
listItems[i].deleteListItem();
}
for (i in files) {
var name = files[i].getName();
// Skip certain files based on their names.
if (/(^_|taslak|draft)/i.test(name))
continue;
// Title field
var title = '<a href=\'' + files[i].getUrl() + '\'>' + name + '</a>';
// Type field
var type = files[i].getType();
// condition: if the type is "blob_item" replace it with the filename extension
if (type == "blob_item") {
type = name.substring(name.lastIndexOf('.'));
}
// Size field
// To indicate the size properly, we need multiple conditions:
// - If size > 0, we show the size, else (if size = 0)
// it's a Google Drive file and we can hide this zero.
// - Calculate the length of the number (28099 is five
// digits, 2158080 is seven digits):
// If length < 7 digits then size is < 1mb.
// Indicate 'kb' and remove the last digits (the bytes).
// Else,
// indicate 'mb' and remove the last digits (kilobytes and bytes).
var size = files[i].getSize();
if (size > 0) {
var length = size.toString().length;
if (length < 7) {
size = size.toString().substring(0,length - 3) + 'kb';
} else {
size = size.toString().substring(0,length - 6) + 'mb';
}
} else {
size = '';
}
// Owner field
// Take the email address and remove everything after the @.
var owner = files[i].getOwner().toString().substring(0,files[i].getOwner().toString().indexOf('@'));
// Last Updated field
// .formatDate() is a method from the Utilities Services
var lastUpdated = Utilities.formatDate(files[i].getLastUpdated(), 'GMT', 'yyyy-MM-dd');
// Add data according to the order of columns in your list
// page.addListItem([title, owner, lastUpdated, type, size]);
page.addListItem([title, owner, lastUpdated, type]);
}
done = true;
}
catch(e){
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment