To start, announcements are submitted via a Google Form, so they end in a spreadsheet. There are three pieces of data: the text of the announcement, the category, and the expiry date.
The script creates a new Google Document (in a public folder), then takes data from the spreadsheet and pastes it into that newly created document. The code for the script follows. (Creative Commons Attribution-ShareAlike).
function createAnnoucementDocument() { // Set up a trigger to run this every weekday, perhaps at 8:00 am // Define a custom paragraph style. var styleHeading = {}; styleHeading[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER; styleHeading[DocumentApp.Attribute.FONT_SIZE] = 18; styleHeading[DocumentApp.Attribute.BOLD] = true; var styleCategory = {}; styleCategory[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.LEFT; styleCategory[DocumentApp.Attribute.FONT_SIZE] = 12; styleCategory[DocumentApp.Attribute.BOLD] = true; var styleText = {}; styleText[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.LEFT; styleText[DocumentApp.Attribute.FONT_SIZE] = 12; styleText[DocumentApp.Attribute.BOLD] = false; // Get the current date var app = UiApp.createApplication(); var dateToday = new Date(); // date formatting here: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html var formattedDateToday = Utilities.formatDate(new Date(), "MST", "yyyy-MM-dd"); // Create the document var documentName = formattedDateToday + " School Announcements"; var doc = DocumentApp.create(documentName); // Move the document to the shared folder entitled "Announcements" var documentFile = DocsList.getFileById(doc.getId()); var folderName = DocsList.getFolder("Announcements"); documentFile.addToFolder(folderName); // the spreadsheet that contains the results of the announcement submission form var sheet = SpreadsheetApp.openById("***INSERT_SPREADSHEET_KEY_HERE***").getSheets()[0]; // Start creating the body of the document var body = doc.getBody(); body.appendParagraph("Name of School Goes Here\r"+formattedDateToday).setAttributes(styleHeading); // Read the whole spreadsheet into a list // if Expiry Date (column D) is > or equal to today's date then process it, else ignore // switch if category match then append to document var data = sheet.getRange(2, 2, sheet.getLastRow(), sheet.getLastColumn()).getValues(); for (var row=0, total=data.length; row < total; row++) { var rowData = data[row]; var announcementText = rowData[0]; var announcementCategory = rowData[1]; var announcementExpiry = rowData[2]; if (announcementExpiry >= dateToday) { switch (announcementCategory) { case "General": body.appendParagraph(announcementText).setAttributes(styleText); break; } } } body.appendParagraph("Events and Meetings").setAttributes(styleCategory); for (var row=0, total=data.length; row < total; row++) { var rowData = data[row]; var announcementText = rowData[0]; var announcementCategory = rowData[1]; var announcementExpiry = rowData[2]; if (announcementExpiry >= dateToday) { switch (announcementCategory) { case "Events and Meetings": body.appendParagraph(announcementText).setAttributes(styleText); break; } } } body.appendParagraph("Athletics").setAttributes(styleCategory); for (var row=0, total=data.length; row < total; row++) { var rowData = data[row]; var announcementText = rowData[0]; var announcementCategory = rowData[1]; var announcementExpiry = rowData[2]; if (announcementExpiry >= dateToday) { switch (announcementCategory) { case "Athletics": body.appendParagraph(announcementText).setAttributes(styleText); break; } } } body.appendParagraph("Cafeteria").setAttributes(styleCategory); for (var row=0, total=data.length; row < total; row++) { var rowData = data[row]; var announcementText = rowData[0]; var announcementCategory = rowData[1]; var announcementExpiry = rowData[2]; if (announcementExpiry >= dateToday) { switch (announcementCategory) { case "Cafeteria": body.appendParagraph(announcementText).setAttributes(styleText); break; } } } return app; }
No comments:
Post a Comment