Murlogue

GAS: Data From GUI To Spreadsheet

Feb 20th, 2013
7,997
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*global SpreadsheetApp: false, UiApp: false */
  2.  
  3. // Close the current UI
  4. function exit() {
  5.     "use strict";
  6.     var ui = UiApp.getActiveApplication();
  7.     return ui.close();
  8. }
  9.  
  10. // Callback action for button labelled "Add Row"
  11. // Take the values from two text boxes in the active GUI and
  12. //   add them as rows to the active sheet.
  13. // Prepare for next input by:
  14. //   re-setting the text boxes to empty strings
  15. //   Set the focus to the first text box
  16. function addRow(e) {
  17.     "use strict";
  18.     var ui = UiApp.getActiveApplication(),
  19.         sheet = SpreadsheetApp.getActiveSheet(),
  20.         name = e.parameter.txtName_Name,
  21.         email = e.parameter.txtEmail_Name,
  22.         txtName = ui.getElementById('txtName_Id'),
  23.         txtEmail = ui.getElementById('txtEmail_Id');
  24.     sheet.appendRow([name, email]);
  25.     txtName.setValue('');
  26.     txtEmail.setValue('');
  27.     txtName.setFocus(true);
  28.     return ui;
  29. }
  30.  
  31. // Build a GUI with two labels, two text boxes and two buttons.
  32. function guiDemo() {
  33.     "use strict";
  34.     var ui = UiApp.createApplication(),
  35.         ss = SpreadsheetApp.getActiveSpreadsheet(),
  36.         uiTitle = 'Add Row To Spreadsheet',
  37.         panelInput = ui.createVerticalPanel(),
  38.         panelName = ui.createHorizontalPanel(),
  39.         panelEmail = ui.createHorizontalPanel(),
  40.         panelButtons = ui.createHorizontalPanel(),
  41.         lblName = ui.createLabel('Name:'),
  42.         lblEmail = ui.createLabel('Email:'),
  43.         txtName = ui.createTextBox(),
  44.         txtEmail = ui.createTextBox(),
  45.         btnAddRow = ui.createButton('Add Row'),
  46.         btnExit = ui.createButton('Exit'),
  47.         exitHandler = ui.createServerHandler('exit'),
  48.         addRowHandler = ui.createServerHandler('addRow');
  49.     panelName.add(lblName);
  50.     panelName.add(txtName);
  51.     panelEmail.add(lblEmail);
  52.     panelEmail.add(txtEmail);
  53.     panelInput.add(panelName);
  54.     panelInput.add(panelEmail);
  55.     panelButtons.add(btnAddRow);
  56.     panelButtons.add(btnExit);
  57.     panelInput.add(panelButtons);
  58.     ui.add(panelInput);
  59.     ui.setWidth(200);
  60.     ui.setHeight(100);
  61.     btnExit.setWidth(80);
  62.     btnExit.addClickHandler(exitHandler);
  63.     btnAddRow.setWidth(80);
  64.     btnAddRow.addClickHandler(addRowHandler);
  65.     addRowHandler.addCallbackElement(txtName);
  66.     addRowHandler.addCallbackElement(txtEmail);
  67.     txtName.setName('txtName_Name');
  68.     txtEmail.setName('txtEmail_Name');
  69.     txtName.setId('txtName_Id');
  70.     txtEmail.setId('txtEmail_Id');
  71.     ui.setTitle(uiTitle);
  72.     txtName.setFocus(true);
  73.     ss.show(ui);
  74. }
RAW Paste Data