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