Created
May 29, 2012 16:21
-
-
Save erickoledadevrel/2829354 to your computer and use it in GitHub Desktop.
Apps Script Intro Example Code 2012-05-23
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 publish() { | |
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = spreadsheet.getSheetByName('Announcements'); | |
var data = sheet.getDataRange().getValues(); | |
var site = SitesApp.getSite('erickoledauniversity'); | |
var page = site.getChildByName('announcements'); | |
for (var i = 0; i < data.length; i++) { | |
var row = data[i]; | |
var title = row[1]; | |
var description = row[2]; | |
var approved = row[3]; | |
var published = row[4]; | |
if (approved == 'Yes' && published != 'Yes') { | |
page.createAnnouncement(title, description); | |
data[i][4] = 'Yes'; | |
} | |
} | |
sheet.getDataRange().setValues(data); | |
} | |
function onOpen() { | |
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
spreadsheet.addMenu('Announcements', [ | |
{ name: 'Publish', functionName: 'publish' } | |
]); | |
} | |
function onFormSubmit(event) { | |
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = spreadsheet.getSheetByName('Announcements'); | |
var title = event.namedValues['Title']; | |
var description = event.namedValues['Description']; | |
var url = 'https://docs.google.com/macros/exec?service=AKfycbzohC7b058rfLIVpcxzRO69c0YBCrk2K-4mU-Oc'; | |
url += '&spreadsheetId=' + spreadsheet.getId(); | |
url += '&row=' + sheet.getLastRow(); | |
var message = 'New announcement submitted.\n' + | |
'Title: ' + title + | |
'\nDescription: ' + description + | |
'\nApprove: ' + url; | |
MailApp.sendEmail('api.ekoleda@gmail.com', 'New Announcement', | |
message); | |
} | |
function doGet(event) { | |
var spreadsheetId = event.parameter['spreadsheetId']; | |
var spreadsheet = SpreadsheetApp.openById(spreadsheetId); | |
var sheet = spreadsheet.getSheetByName('Announcements'); | |
var row = event.parameter['row']; | |
sheet.getRange(row, 4).setValue('Yes'); | |
var app = UiApp.createApplication(); | |
var label = app.createLabel(); | |
label.setText('The announcement was approved'); | |
app.add(label); | |
return app; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment