Last active
August 29, 2015 13:56
-
-
Save mrsonord/9105621 to your computer and use it in GitHub Desktop.
Google Site Script To Generate Pages
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
function doGet(e) { | |
var app = UiApp.createApplication(); | |
// The text label container | |
var mainPanel = app.createVerticalPanel().setHeight("100%").setWidth("100%"); | |
mainPanel.setHorizontalAlignment(UiApp.HorizontalAlignment.CENTER).setVerticalAlignment(UiApp.VerticalAlignment.MIDDLE); | |
mainPanel.add(app.createLabel("Generate a blank board page.")); | |
mainPanel.add(app.createLabel("Refresh the page to show it in the lists")); | |
// The panel that holds our button to click | |
var buttonPanel = app.createVerticalPanel().setHeight("100%").setWidth("100%"); | |
buttonPanel.setHorizontalAlignment(UiApp.HorizontalAlignment.CENTER).setVerticalAlignment(UiApp.VerticalAlignment.MIDDLE); | |
//The Button and it's handler | |
buttonPanel.add(app.createButton("New Board", app.createServerHandler("onClick")).setId("button")); | |
// The full app panel equivalent to the commonly used .wrap | |
var fullPanel = app.createVerticalPanel().setStyleAttributes({background: "#999999"}).setHeight("100%").setWidth("100%"); | |
fullPanel.setHorizontalAlignment(UiApp.HorizontalAlignment.CENTER).setVerticalAlignment(UiApp.VerticalAlignment.MIDDLE); | |
//Add our panels | |
mainPanel.add(buttonPanel); | |
fullPanel.add(mainPanel); | |
app.add(fullPanel); | |
return app; | |
} | |
//Click handler | |
function onClick(e) { | |
var app = UiApp.getActiveApplication(); | |
//get the current site so we can get the templates | |
var site = SitesApp.getActiveSite(); | |
var template = site.getTemplates()[0]; | |
// get the active page so we know where we are. The page I use this script on is called spaces | |
var page = site.getChildByName("spaces"); | |
// get the top item on the list which is filtered by creation time so most recent is at the top | |
var items = page.getListItems({ start:0, max: 1 }); | |
// Must iterate | |
for (var i in items) { | |
// grab the last character | |
var name = items[i].getValueByName("name").substring(6); | |
} | |
// Parse the 1 character string as an integer and ++ it | |
var name = parseInt(name)+1; | |
var name = name++; | |
// Set our name/title vars to keep it clean up ahead | |
var child = "board-" + name; | |
var title = "Board-" + name; | |
//Create the new page | |
page.createPageFromTemplate(title, child, template); | |
// Get creators email to register in the list | |
var user = Session.getActiveUser().getEmail(); | |
//@TODO Get timestamp or date for fourth field | |
page.addListItem([child, title, user, ""]); | |
//Change buttons text to notify the creation is complete | |
app.getElementById("button").setText("New Space Created"); | |
return app; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment