2. Mashup Google Apps Script Základní zdroje http://code.google.com/googleapps/appsscript/ [http://code.google.com/googleapps/appsscript/] http://code.google.com/intl/cs/googleapps/appsscript/articles.html [http://code.google.com/intl/cs/googleapps/appsscript/articles.html] http://code.google.com/intl/cs/googleapps/appsscript/templates.html [http://code.google.com/intl/cs/googleapps/appsscript/templates.html] http://code.google.com/intl/cs/googleapps/appsscript/allservices.html [http://code.google.com/intl/cs/googleapps/appsscript/allservices.html] Vytvoření dokumentu/skriptu 1. V Google Docs [http://docs.google.com/] vytvořte novou tabulku (spreadsheet) I. Pro práci se skripty je dokument potřeba uložit a tedy i pojmenovat 2. Otevření editoru skriptů I. V menu dokumentu: Nástroje Skripty Editor skriptů II. Pro uložení a následné spuštění skriptu je potřeba skript pojmenovat (V rámci jednoho dokumentu může být více sktiptů) 3. Lze importovat již hotové skripty nebo použít celý dokument včetně skriptů jako šablonu I. V menu dokumentu: Nástroje Skripty Vložit Stock Price nebo: Vložit Skript II. Šablony [https://docs.google.com/templates] Základy Pro psaní skriptů je použit JavaScript. Jednoduchý skript zobrazující dialogové okno function helloworld() { Browser.msgBox("Hello, World"); Spuštění skriptu: Spustit helloworld tlačítko spustit vytvořením objektu a přiřazením skriptu V menu dokumentu: Vložit Kresba nakreslit tlačítko Menu hotové kresby: Kresba Přiřadit skript. Vložit název fce ke spuštění pomocí událostí: viz dále Debugger tlačítko Ladit breakpoint, krokování Rozšíření Jednoduchý dialog pro získání údaje function showdialog(){ var name = Browser.inputBox("ID Check", "Enter your name", Browser.Buttons.OK_CANCEL); Posílání mailu odesílatem je email Vašeho účtu. existují omezení na počet odeslaných mailů. MailApp.getRemainingDailyQuota() function sendmail(){ MailApp.sendEmail("you@email.com", "Hello world!", "Sent from Apps Script"); Log function logmessage(){ Logger.log("Hello world!"); Zobrazení logu: Editor skriptů Zobrazit protokoly Logger.getLog() Události 1 z 5 22.11.11 12:42
onopen() onedit(event) oninstall() onformsubmit(e) Časové spouštěče Editor skriptů Spouštěče Spouštěče aktuálního skriptu function onopen() { Browser.msgBox(Session.getActiveUser().getEmail()); function onedit(event) { var ss = event.source.getactivesheet(); var r = event.source.getactiverange(); r.setcomment("last modified: " + (new Date())); // Assume the form has 2 fields (name and age) function onformsubmit(e) { var timestamp = e.values[0]; var name = e.values[1]; var age = e.values[2]; // namedvalues are only available for submitters who are Apps users and // for forms that require users to signin before submitting the form. var submitter = e.namedvalues["username"]; MailApp.sendEmail("email_username", "form submitted", name + ", age " + age + " submitted by " + submitter + " on " + timestamp + ". "); Vytvoření menu // po otevření dokumentu jsou přidány položky menu do aktuálního Spreadsheet function onopen() { // položky menu a funkce, které se zavolají po kliknutí na položku menu var menuentries = [ {name: "Surprise 1", functionname: "menuitem1", {name: "Surprise 2", functionname: "menuitem2" ]; ss.addmenu("surprises", menuentries); function menuitem1() { Browser.msgBox("You clicked Surprise 1 on the menu!"); function menuitem2() { Browser.msgBox("Surprise! Surprise!") Uživatelské rozhraní tlačítka přepínače textová pole popisky seznamy dialogová okna panely function gui() { var doc = SpreadsheetApp.getActiveSpreadsheet(); var app = UiApp.createApplication(); var textbox = app.createtextbox().setname('textbox'); var button = app.createbutton('submit'); app.add(textbox); app.add(button); var handler = app.createserverclickhandler('process'); handler.addcallbackelement(textbox); button.addclickhandler(handler); doc.show(app); function process(e) { var doc = SpreadsheetApp.getActiveSpreadsheet(); var cell = doc.getrange('a1'); cell.setvalue(e.parameter.textbox); var app = UiApp.getActiveApplication(); app.close(); // The following line is REQUIRED for the widget to actually close. return app; Zápis do buňky function example() { var sheet = ss.getsheets()[0]; var myvalue = Browser.inputBox("Enter a number"); sheet.getrange("a1").setvalue("number entered:"); var b1range = sheet.getrange("b1"); b1range.setvalue(myvalue); var valuetoshow = b1range.getvalue() + 1; Browser.msgBox("The value you entered plus one is: " + valuetoshow); Práce s SpreadSheet function getsheetbyname(name){ 2 z 5 22.11.11 12:42
var data = ss.getsheetbyname(name); // if not exists create if(data==null){ ss.insertsheet(name, 1); return ss.getsheetbyname(name); // Function that records the values in the Spreadsheet function savedata(e) { var d = SpreadsheetApp.getActiveSpreadsheet(); var doc = d.getsheetbyname("data"); var lastrow = doc.getlastrow(); // Determine the last row in the Spreadsheet that contains any values var cell = doc.getrange('a1').offset(lastrow, 0); // determine the next free cell in column A cell.setvalue(e.parameter.date); cell.offset(0, 1).setValue(e.parameter.name); // Set the value of the adjacent cell cell.offset(0, 2).setValue(e.parameter.price); // set the value of the next cell cell.offset(0, 3).setValue(e.parameter.category); // set the value of the next // Clean up - get the UiApp object, close it, and return var app = UiApp.getActiveApplication(); app.close(); // The following line is REQUIRED for the widget to actually close. return app; function getdatabysheet(name){ // get shhet by name var sheet = ss.getsheetbyname(name); // get last row var lastrow = sheet.getlastrow(); var i=0; // start position var cell = sheet.getrange('a1'); // associative array to store data var data=new Array(); // iterate all data for (i=0;i<lastrow;i++){ // if doesn't exist, create 0 if(!data[cell.offset(i, 3).getValue()]){ data[cell.offset(i, 3).getValue()] = 0; // add count data[cell.offset(i, 3).getValue()]+=cell.offset(i, 2).getValue(); // return array return data; Url Fetch function requesturl(url) { var response = UrlFetchApp.fetch(url); return response.getcontenttext() XML parser // This script makes use of the Oracle of Bacon, // a very cool site at http://oracleofbacon.org. // Takes two arguments: names of actors. The second // is optional, and defaults to "Kevin Bacon" if unspecified. // // Example usage in a spreadsheet: // =kb("miley Cyrus", "Arnold Schwarzenegger") // function kb(from, to) { if (!to) { to = "Kevin Bacon"; var parameters = { method : "post", payload : "a=" + encodeuricomponent(from) + "&b=" + encodeuricomponent(to) + "&u=1" + // movies only. Use "3" to include TV "&p=" + encodeuricomponent('google-apps') ; var text = UrlFetchApp.fetch("http://oracleofbacon.org/cgi-bin/xml", parameters).getcontenttext(); return parse(text); function testbacon() { var result = kb("miley Cyrus", "arnold schwarzenegger"); Logger.log(result); function parse(txt) { var doc = Xml.parse(txt, true); var attr = doc.spellcheck; if (attr) { return "Cannot find actor: " + attr.name; var actors = doc.html.head.getelements("actor"); var movies = doc.html.head.getelements("movie"); if (!actors actors.length ==0) { return "no match found"; var movieindex = 0; var r = ''; var firstperson = true; for (var i in actors) { r = r + actors[i].gettext(); if (movies[movieindex]) { r = r + (firstperson? "" : " who") + " was in " + movies[movieindex].gettext() + " with "; movieindex++; firstperson = false; return r; Soap service 3 z 5 22.11.11 12:42
function determinecountryfromip(ipaddress) { var wsdl = SoapService.wsdl("http://www.webservicex.net/geoipservice.asmx?wsdl"); var geoservice = wsdl.getgeoipservice(); var param = Xml.element("GetGeoIP", [ Xml.attribute("xmlns", "http://www.webservicex.net"), Xml.element("IPAddress", [ ipaddress ]) ]); var result = geoservice.getgeoip(param); return result.envelope.body.getgeoipresponse.getgeoipresult.countrycode.text; Map Zobrazení bodů na statické mapě function testmap(){ var imageurl = getmap("prague","vienna"); var sheet = SpreadsheetApp.getActiveSheet(); sheet.insertimage(imageurl, 3, 3); function getmap (place1, place2) { var map = Maps.newStaticMap().setSize(500, 350); map.setmarkerstyle(maps.staticmap.markersize.mid, "red", null); map.addmarker(place1); map.addmarker(place2); return map.getmapurl(); Úkoly 1. Vytvořte nový dokument (tabulku) 2. Vytvořte menu s názvem Ukázka menu bude mít 2 položky: Zadat číselný údaj, Zadat text, Zadat město, Proveď úkoly 3. Po kliknutí na Zadat číselný údaj se zobrazí dialogové okno pro zadání údaje Tento údaj se zapíše do buňky A1 a A4 4. Po kliknutí na Zadat text se zobrazí dialogové okno pro zadání textu Tento údaj se zapíše do buňky A2 5. Po kliknutí na Zadat město se zobrazí dialogové okno pro zadání města Tento údaj se zapíše do buňky A3 6. Po kliknutí na Proveď úkoly se provedou úkoly dle následujícího zadání: I. Převeďte číslo v buňce A1 z CZK na EUR (ručně zapište vzorec a kurz) a zapište do buňky B1 II. Přeložte text v buňce A2 z češtiny do angličtiny III. Zobrazte město z buňky A3 na mapě IV. Zjistěte aktuální kurz online a převeďte číslo z A4 z CZK na EUR, výsledek zapište do B4 a. můžete použít http://www.google.com/ig/calculator?hl=en&q=1czk=?eur [http://www.google.com/ig/calculator?hl=en&q=1czk=?eur] b. výsledkem není JSON, ale pole. Lze ho použít např. takto: // získaný text var text =... var dat; eval("dat = " + text + ";"); Bonus úkol: Vytvořte v novém dokumentu/listu GUI obsahující 2 textové pole do prvního textového pole zadejte seznam požadovaných ingrediencí pro recept oddělených čárkou, např. onions,garlic druhé textového pole bude obsahovat název jídla, např. pizza po odeslání formuláře zpracujte údaje z API Recipe Puppy [http://www.recipepuppy.com/about/api/] zapište do tabulky název receptu a url Pipes Základní zdroje http://pipes.yahoo.com/pipes/ [http://pipes.yahoo.com/pipes/] http://pipes.yahoo.com/pipes/pipes.popular [http://pipes.yahoo.com/pipes/pipes.popular] Základy Dostupné moduly Sources - Most Pipes begin with a data source. These modules grab data from somewhere on the internet and bring it into your Pipe for processing. User Inputs - These modules let you define parameters for your Pipe. These inputs can be fed into other modules in your Pipe. Operators - These modules transform and filter data flowing through your Pipe. URL - This module manipulates URLs. String - These modules help manipulate and combine text strings. Date - These modules define and format dates. Location - These modules help convert text strings to geographic locations. Number - This module provides basic arithmetic operations. 4 z 5 22.11.11 12:42
Deprecated - These modules will continue to work however, we're discouraging their use because we've introduced new modules with improved functionality. Jednoduchá agregace feedů Kombinace zdrojů Úkoly 1. Přihlašte se do Pipes 2. Vytvořte pipe, která bude kombinovat několik zdrojů (např. BBC, CNN) a bude filtrovat záznamy na základě uživatelem zadaného textu. Tento text může být obsažen jak v popisu tak v názvu. 3. Vytvoře pipe, která načte automobily nebo produkty (např. Google Base) dle zadaného klíčového slova a ke každému z nich dodá obrázek z flickr /mnt/www/courses/mi-w20/data/pages/tutorials/02/start.txt Poslední úprava: 2011/03/10 20:49 autor: kuchajar 5 z 5 22.11.11 12:42