How to have an Apps Script Gadget display results directly in the UI - gwt

On my Google Site I've inserted an Apps Script Gadget (by pasting the URL of an Apps Script that I published as a service). This Apps Script allows the user to enter a value (their 'Blow Number') and view the corresponding data (based on API calls to my Google Fusion tables).
Right now, the script returns 3 hyperlinks:
Click here for a table of Blow Number 1
Click here for a chart of Blow Number 1
Click here for a map of Blow Number 1
This is because my script function getblowdetails has 3 app.createAnchor variables. Instead of having the script return 3 hyperlinks (that the user has to click on and view the resulting URL in a new window), I would like for the script to automatically invoke the 3 URLs and display the table, chart, and map in panels on the same page.
So the user would enter their Blow Number and press enter. They would then view the table, chart, and map directly below the text box on the same web page.
Please see the Code that I've included below and advise...Thanks for the help- I'm an apps-script novice so a thorough and understand-able response is greatly appreciated!
Code
Note: I've removed the URLs from the createAnchor variables because I'm only allowed to include 2 links in the post, but you can see them by going to www.OnSiteBAC.com/ViewMyBlows and entering Blow Number = 1...then click on the hyperlinks.
function doGet() {
var app = UiApp.createApplication();
// Create input boxes, buttons, labels, and links
var textBoxA = app.createTextBox().setId('textBoxA').setName('textBoxA').setFocus(true);
var buttonA = app.createButton('Get Blow Details').setEnabled(false);
var label = app.createLabel('Please enter your Blow Number here');
var link = app.createAnchor('where can I find my Blow Number?', 'http://www.onsitebac.com');
// Create a handler to call the getblowdetails function.
// A validation is added to this handler so that it will only invoke 'getblowdetails' if textBoxA contains a number
var handler = app.createServerClickHandler('getblowdetails').validateNumber(textBoxA).addCallbackElement(textBoxA);
// Create a handler to enable the button if all input is legal
var onValidInput = app.createClientHandler().validateNumber(textBoxA).forTargets(buttonA).setEnabled(true).forTargets(label, link).setVisible(false);
// Create a handler to mark invalid input in textBoxA and disable the button
var onInvalidInput1 = app.createClientHandler().validateNotNumber(textBoxA).forTargets(buttonA).setEnabled(false).forTargets(textBoxA).setStyleAttribute("color", "red").forTargets(label, link).setVisible(true);
// Create a handler to mark the input in textBoxA as valid
var onValidInput1 = app.createClientHandler().validateNumber(textBoxA).forTargets(textBoxA).setStyleAttribute("color", "black");
// only fire ServerHandler for onKeyUp if it passes validation
var textBoxHandler = app.createServerHandler('textBoxHandlerFunction');
// Add all the handlers to be called when the user types in the text boxes
textBoxHandler.addCallbackElement(textBoxA);
textBoxA.addKeyUpHandler(onInvalidInput1);
textBoxA.addKeyUpHandler(onValidInput1);
textBoxA.addKeyUpHandler(onValidInput);
textBoxA.addKeyUpHandler(textBoxHandler);
buttonA.addClickHandler(handler);
app.add(textBoxA);
app.add(buttonA);
app.add(label);
app.add(link);
return app;
}
function textBoxHandlerFunction(e) {
var app = UiApp.getActiveApplication();
if (e.parameter.keyCode == 13)
{
app = getblowdetails(e);
}
return app;
}
function getblowdetails(e) {
var app = UiApp.getActiveApplication();
var panel2 = app.createVerticalPanel();
var link2 = app.createAnchor ().setStyleAttribute("color", "green");
var panel3 = app.createVerticalPanel();
var link3 = app.createAnchor ();
var panel4 = app.createVerticalPanel();
var link4 = app.createAnchor ();
panel3.add(link3);
app.add(panel3);
panel4.add(link4);
app.add(panel4);
return app;
}

I don't think you'll be able to actually download the result and show it. So, there's no easy solution.
But you can build that table and chart on Apps Script easily (assuming you can already fetch the info from tables using its API).
The last issue is the map. On Apps Script you can only create static maps, meaning, an image. You can add custom markers and polygons, set the zoom, etc. But in the end it's a photo. The user will not be able to drag it around or use the map as an embedded google map as one would expect.

Related

Getting values from cells in google scripts

I am trying to make working sheets for my work. In Google scripts, I've created "Custom Menu" for my sheet wich is sending email correctly. But now I want to get value from the specific cell and check if it is below, for example, 2, send an email with that value. For now, I have this:
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Custom Menu')
.addItem('First item', 'menuItem1')
.addSeparator()
.addToUi();
}
function menuItem1() {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.alert('You clicked the first menu item!');
if( 'A1' > 3){
MailApp.sendEmail('luk...#gmail.pl', 'subject', 'message');
}
}
I don't know how to get this value from this cell. This 'If" is just an example of what I am trying to do, I know it is not working. Thank you in advance for any kind of help.
First, You need to find the sheet:
var sheet = SpreadsheetApp.getActiveSheet();
Then, you need to specify a cell range and get the value(s):
var value = sheet.getRange("A1").getValue();
You can browse the API for more functions here: https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app

Script for removing rows in Google sheets

I'm in need of a script that removes, not just deleting, rows in Google sheets.
Having a form that supplies responses to a sheet, and for now I have to manually log in and remove the responses.
So a simple script that triggers every saturday, removing form responses from a sheet is what I need.
Code to delete rows:
function deleteResponses() {
var ss = SpreadsheetApp.openById("File ID");
var sheet = ss.getSheets()[3];
sheet.deleteRows(2, 200);
};
To add code: Under the TOOLS menu, choose SCRIPT EDITOR. Add this code.
Alternate code to clear the sheet:
function deleteResponses() {
var ss = SpreadsheetApp.openById(id);
var sheet = ss.getSheets()[0];
sheet.clear();
};
Get your spreadsheet ID, and replace it in the "id" parameter. Then under the RESOURCES menu, choose CURRENT PROJECTS TRIGGERS.

Hide/Show UI elements by MouseOut/MouseOver client handlers

The goal is to mutually replace two elements with each-other by MouseOut/MouseOver events. Specifically the elements are a label and a listbox. There are some UI arrangement in which the implementation works acceptably in Chrome, however it always fails in IE(9). The problem occurs during a selection from the listbox as per browsers ignor the dropped down area as part of the listbox, it triggers the mouseOut handler and hides the listbox.
Is there any solution forcing browsers to consider the listbox together with its dropped down area?
app.createListBox() .setId('listBox');
app.createLabel('Item1') .setId('label')
.addMouseOverHandler(app.createClientHandler()
.forEventSource().setVisible(false)
.forTargets(app.getElementById('listBox')).setVisible(true));
app.getElementById('listBox')
.addItem('Item1')
.addItem('Item2')
.setVisible(false)
.addMouseOutHandler(app.createClientHandler()
.forEventSource().setVisible(false)
.forTargets(app.getElementById('label')).setVisible(true));
Many Thanks
there is a possible workaround using a server handler to hide the listBox. From my tests it behaves quite similarly (if not better ) - you can test it here
function doGet() {
var app = UiApp.createApplication().setStyleAttribute('padding','100px');
var p = app.createVerticalPanel();
var serverHandler = app.createServerHandler('handler').addCallbackElement(p)
var listBox = app.createListBox() .setId('listBox').setName('listBox').addChangeHandler(serverHandler);
var label = app.createLabel('Item1').setId('label')
.addMouseOverHandler(app.createClientHandler()
.forEventSource().setVisible(false)
.forTargets(listBox).setVisible(true));
listBox.addItem('Item1').addItem('Item2').addItem('Item3').addItem('Item4')
.setVisible(false)
p.add(listBox).add(label)
app.add(p)
return app
}
function handler(e){
var app = UiApp.getActiveApplication();
var listBox = app.getElementById('listBox')
var label = app.getElementById('label')
listBox.setVisible(false)
label.setVisible(true).setText(e.parameter.listBox)
return app
}

Simple popup or dialog box in Google Apps Script

I'm looking for simple code that adds a popup in my Google Apps Script Ui that comes up when I hit a submit button. The popup box would display a message and have a button to close the popup.
I've looked all over the place - everything seems so complicated and does way more than I need it to do.
This is the current code I have for the submit button.
function doGet() {
var app = UiApp.createApplication();
app.setTitle("My Logbook");
var hPanel_01 = app.createHorizontalPanel();
var vPanel_01 = app.createVerticalPanel();
var vPanel_02 = app.createVerticalPanel();
var vPanel_03 = app.createVerticalPanel();
var submitButton = app.createButton("Submit");
//Create click handler
var clickHandler = app.createServerHandler("submitData");
submitButton.addClickHandler(clickHandler);
clickHandler.addCallbackElement(hPanel_01);
////Test PopUp Panel
var app = UiApp.getActiveApplication();
var app = UiApp.createApplication;
var dialog = app.createDialogBox();
var closeHandler = app.createClientHandler().forTargets(dialog).setVisible(false);
submitButton.addClickHandler(closeHandler);
var button= app.createButton('Close').addClickHandler(closeHandler);
dialog.add(button);
app.add(dialog);
//////
return app;
}
Since UiApp is depreciated, HTMLService should be used to create custom user interfaces.
To prompt simple popup to display a message, use alert method of Ui class
var ui = DocumentApp.getUi();
ui.alert('Hello world');
will prompt simple popup with hello world and an ok button.
To display customized html template in Dialog, use HTMLService to create template and then pass it to showModalDialog method of Ui Class
var html = HtmlService.createHtmlOutput("<div>Hello world</div>").setSandboxMode(HtmlService.SandboxMode.IFRAME);
DocumentApp.getUi().showModalDialog(html, "My Dialog");
HtmlService.createHtmlOutputFromFile allows you to display html that is in a separate file. see the documentation
Have you tried using zIndex? It places the panel above all of your other panels...
var popupPanel = app.createVerticalPanel().setId('popupPanel')
.setVisible(false)
.setStyleAttribute('left', x)
.setStyleAttribute('top', y)
.setStyleAttribute('zIndex', '1')
.setStyleAttribute('position', 'fixed');
x = panel position from the left portion of your app
y = panel position from the top portion of your app
zIndex = the 'layer' your panel will appear on. You can stack panels using '1', '2', '3' etc.
position = your panel will be in a fixed position denoted by (x,y)
Visibility is set to false until you click submit, then have a client handler for your submit button make the popupPanel visible. When you click the button on your popupPanel, have the client handler set visibility to false once again and it will disappear.
One more thing, I noticed you get the active app and then create a new app. You do not need to create a new app...just new panels inside your app.
Hope this helps!
You can use a dialogbox to popup.
Add a button to the dialog-box. Add a client handler that sets the dialog box invisible,once you click the button.
var app = UiApp.createApplication;
var dialog = app.createDialogBox();
var closeHandler = app.createClientHandler().forTargets(dialog).setVisible(false);
var button= app.createButton('Close').addClickHandler(closeHandler);
dialog.add(button);
app.add(dialog);
This should help.
EDIT
Added "()" after .createClientHandler. That should remove issues related to TypeError: Cannot find function createDialogBox in object function createApplication() {/* */}
Popup - use something like this:
var table = app.createFlexTable();
table.setStyleAttribute("position", "absolute");
table.setStyleAttribute("background", "white");
add items to the table and hide and show as needed.

How to get row values from a Rounded Rectangle List in Dashcode?

I am new to dashcode and trying to build a simple web app for iphone using it. My primary aim is to have a Rectangular List (I have used the "Rounded rectangle list"). It is a static list and has three rows. What I want is a website to open when user clicks on any of the row, and each row would have a different URL. I was able to add a Rounded rectangle list with three static rows like
The object ID is "list"
Row 1-- Label- "Gift Cards" , Value - "http://www.abcxyz.com/giftcard"
Row 2-- Label- "Toys" , Value - "http://www.abcxyz.com/toys"
Row 3-- Label- "Bikes" , Value - "http://www.abcxyz.com/bikes"
i added onclick even to call a java script function like below
function myButtonPressHandler(event)
{
var websiteURL = "http://www.abcxyz.com/giftcard";
location = websiteURL;
}
the above code opens the same URL "http://www.abcxyz.com/giftcard" when the user clicks on any of the three buttons, but what I want is to fetch the value of each child node (which would be their respective URLs) at runtime and open it using location = WebsiteURL something like below (did'nt work for me :( -
function myButtonPressHandler(event)
{
var websiteURL = document.getElementById("list").children;
var WebURL = websiteURL[???].value;
location = WebURL;
}
Any help would be appreciated.
Thanks
OK ... so figured out my own answer. The Rounded Rectangular list is actually a multidimensional array. so to get the value of each of the rows i.e. the Http URLs and open them on the browser when the rows were touched/tapped/pressed is as below.
function buttonpresshandler(event)
{
// Insert Code Here
var list = document.getElementById("list").object;
var selectedObjects = list.selectedObjects();
//Open webpage with the value of each label
location = selectedObjects[0][1];
}
Hurray!