TO USE GOOGLE APPS SCRIPT
Documentation¬ To search documentation, click here and add your key words¬ Help forum¬ Bug tracker¬










NOTES
Gotchas and other ramblings on the new Google Apps Script. All dated since Google's updates may make them out of date

Script editor in Spreadsheets

2011-08-05
After making a copy of a spreadsheet, the spreadsheet displayed is the new copy. Any script window, however contains the script associated with the old spreadsheet.

Script editor in Sites

2011-07-31
The code pane which is executed by a gadget containing the script seems to be the copy, not the original. Therefore...
   if you want to save an old or "reference" copy of your script...
  1. File > Rename. and give the old script a meaningful name
  2. File > Copy
  3. Select the "Copy of ..." tab.
  4. Rename this "Code", so that you know that it is current.

getActiveSite and getActivePage

2011-07-31
The following works in debug, but not when run from a gadget embeded in a site. Do not use.
  http:  www.google.com/support/forum/p/apps-script/thread?tid=5e146e751588339c&hl=en
    var site = SitesApp.getActiveSite()
    Logger.log(site.getName() + " v6")
    var thisPage = SitesApp.getActivePage()
    Logger.log(thisPage.getLastEdited())

Ui Objects not allowing user to enter text

  2011-07-31
   AbsolutePanel1 Does not echo user text
    FlowPanel1 Does not echo user text
    FormPanel1 Does not echo user text
    FocusPanel1 Does not echo user text

  Ui Objects which do allow the user to enter text

  2011-07-31
  "TextBox1" Does not give a new line when the user presses the Enter kety
   "TextArea1" does respond to the Enter key.

Note that it is possibled to write text using "UiApp.createApplication().createTextArea().setText",
    but the only way of reading it back seems to be to ...
    "<text area>.setId('<name of text area>')"
    add the  text area in to a panel,
    set the panel as the callback parameter "<handler>.addCallbackElement(<panel>)",
    wait for a user event to capture the current contents of the panel and trigger the event handler,
    retrieve the contents of the text box with "e.parameter.<name of text area>"

  Ui Change handlers

    2011-07-31
app.createTextArea().addChangeHandler(handler) //  only triggers on loss of focus
    app.createTextArea().addKeyUpHandler(handler)  //  trigers after every character is typed.
the handler.addCallbackElement (e) is copied at the time of the interrupt, and may therefore be out of date by the time it is processed.



There is a limit to the number of events that can be stored on any event que. E.g. when generating an event for each key-up event in a text box, and with an "Utilities.sleep(3000)" in the event handler, earlier events seem to be retained, while later ones seem to be lost. The solution seems to be either not to use a sleep, or generate a "Math.random().toString()" specific to the invokation, store it in "ScriptProperties.setProperty", and keep checking whether it has been overwriten by a susequent event every 100ms or so. If it has been overwritten, then you must exit from the event before thge user generates sufficient further invokations to exceed the size of the que.

Changes to (E.g. label values) instituted by "<label>.setText", are only seen after the event handler returns.

To pass a parameter from a Google Gadget to a Google Apps Script

2011-07-31
Create or open a sites page, and install the gadget as usual
Then in edit mode
  > ensure that the cursor is in the panel containing the gadget.
    > [HTML]
        > Find the code for the gadget.
        > After <img ...
            > After origsrc ="
                > After the end of the google apps script key, and before the "
                  > insert "&amp;<parameter name>=<parameter value>"
      > [Update]
  > [Save]

In the script, access the parameter in "function doGet(e)", with...
  "e.parameter.<parameter name>"

To list all the parameters available to a script, see...
http://www.google.com/support/forum/p/apps-script/thread?tid=04d9d3d4922b8bfb&hl=en¬

ScriptProperties

2011-07-31
A ScriptProperties object is common to all gadgets containing the script, and all events within the script. Its keys and key values are retained across events. If a separate information needs to be stored for different gadgets using the same script, then pass a unique configuration parameter to the script, and use this as a key modifier.

To create an on-sheet button
2011-09-05
 Create a drawing, and save it.
Click on the drawing, and then on the arrow at the top
Select "Assign script"
Enter the name of a function in the script attached to the spreadsheet
[OK]

Then to ensure that focus is restored back to the sheet ready for user input, within the script...
1. Select a different sheet
2. Do the work of the script
3. Re-select the original sheet.
(See http://code.google.com/p/google-apps-script-issues/issues/detail?id=795 for the reasons, and add you comments, or click the yellow star to ask Google for a better solution.




To format a control

<control>
  .setStyleAttribute('fontSize', 'XXpx');
  .setStyleAttribute('fontWeight', 'bold');
  .setStyleAttribute('paddingTop', 'XXpx');
  .setStyleAttribute('fontFamily', 'Georgia');
  .setStyleAttribute('paddingTop', '56px');
  .setStyleAttribute('borderStyle', 'solid');
  .setStyleAttribute('borderColor', 'black');
  .setStyleAttribute('borderWidth', '1px');
  .setStyleAttribute('paddingBottom', '50px') 
  .setStyleAttribute('margin', '50px')   sets space around the control
  .setStyleAttribute('marginTop', '5px')
  .setStyleAttribute('marginBottom', '50px')   sets space below the control
  .setStyleAttribute('backgroundColor','rgb(255,128,128)')
  .setStyleAttribute('backgroundColor','#ff8080')

JAVA (applicable to Google Apps Script

For a random number between 1 and max

Math.floor(Math.random()*max+1)