G Suite Developers Blog
Information for G Suite Developers
Contact Sharing using Google Apps Script
March 23, 2011
Editor's Note:
This article is written by Steve Webster and Vinay Thakker who work at Dito. Dito has developed applications such as Dito Directory which is available in the
Google Apps Marketplace
.
You just created your own contact group in Google Apps Contact Manager and now you want to share this contact group with a few other coworkers (not the entire company). Over the last couple of years, our team at Dito often got this request from our customers. We decided to leverage Google Spreadsheets & Google Apps Script to allow sharing of user’s “personal contact group” with only a select group of coworkers.
How does it work?
The Apps Script implements a three step wizard. Upon completion of the wizard, the script sends the sharing recipients a link to open the spreadsheet to import the user’s recently shared contact group. The three steps in the wizard are.
Step 1
lists all the current contact groups in user’s account. The user can select the group he/she wants to share.
Step 2
allows user to select the colleagues with whom the user wants to share his/her personal contact group with.
Step 3
lets the user submit the sharing request.
Designing using Apps Script Services
Apps Script has various services which can be used to build the user interface, access the user’s contact list and send emails without the need to compile and deploy any code.
1. Security (
guide
)
Before a script can modify a user’s contacts, it needs to be authorized by that user. The authorization process takes place when a user executes the script for the first time. When a user makes a request to share his/her contacts, our script sends a link to the intended recipients by email. Upon clicking this link and the “Run Shared Contact Groups” button in the spreadsheet, the recipient will first need to grant authorization to execute the script. By clicking the “Run Shared Contacts Groups” button again, the script will proceed with creating the shared contact group.
2. Spreadsheet Service
In developing this script, there was a fair amount of data that needed to be exchanged between different users. We used Apps Script’s
Spreadsheet Service
for temporarily storing this data.
// grab the group titled "Sales Department"
var
group = ContactsApp.getContactGroup(
"Sales Department"
);
// from that group, get all of the contacts
var
contacts = group.getContacts();
// get the sheet that we want to write to
var
ss
= SpreadsheetApp.getActiveSpreadsheet();
var
sheet
=
ss
.getSheetByName(
"Contact Data"
);
// iterate through contacts
for
(
var
i
in
contacts
) {
//save each of the values into their own columns
sheet
.getRange(
i
,
1
,
1
,
1
).setValue(
contacts[i]
.getGivenName());
sheet
.getRange(
i
,
2
,
1
,
1
).setValue(
contacts[i]
.getFamilyName());
...
sheet
.getRange(
i
,
13
,
1
,
1
).setValue(
contacts[i]
.getWorkFax());
sheet
.getRange(
i
,
14
,
1
,
1
).setValue(
contacts[i]
.getPager());
sheet
.getRange(
i
,
15
,
1
,
1
).setValue(
contacts[i]
.getNotes());
}
3. Ui Service
Ui Services
in Google Apps Scripts have an underlying Google Web Toolkit implementation. Using Ui Services in Apps Script, we easily built the user interface consisting of a 3 step wizard. In designing Ui using Ui Services, we used two main types of Ui elements - Layout Panels and Ui widgets. The layout panels, like FlowPanel, DockPanel, VerticalPanel, etc., allow you to organize the Ui widgets. The Ui widgets (TextBoxes, RadioButtons, etc.) are added to layout panels. Ui Services make it very easy to assemble and display a Ui interface very quickly.
We built each of the components on their own, and then nested them by using the “add” method on the desired container. The UI widgets in the screenshot above were constructed by the code below:
// create app container, chaining methods to set properties inline.
var
app = UiApp.createApplication().setWidth(
600
).setTitle(
'Share The Group'
);
// create all of the structural containers
var
tabPanel = app.createTabPanel();
var
overviewContent = app.createFlowPanel();
var
step1Content = app.createFlowPanel();
var
step2Content = app.createFlowPanel();
var
step3Content = app.createFlowPanel();
// create u/i widgets
var
selectLabel = app.createLabel(
"Select one of your Contact Groups you want to share with others."
);
var
contactGroupDropdown = app.createListBox().setName(
'groupChooser'
);
// add all children to their parents
overviewContent.add(selectLabel);
overviewContent.add(contactGroupDropdown);
tabPanel.add(overviewContent,
"Overview"
);
tabPanel.add(step1Content,
"Step 1"
);
tabPanel.add(step2Content,
"Step 2"
);
tabPanel.add(step3Content,
"Step 3"
);
app.add(tabPanel);
// tell the spreadsheet to display the app we've created.
SpreadsheetApp.getActiveSpreadsheet().show(app);
Continuing with this pattern, we created a pretty complex design using the UI Services. The next step in building a useful user interface is actually building in event handlers for the UI Widgets. Event Handlers let Apps Script know which function you want to run when your script needs to respond to a given user interaction. The code below is an example of a DropDownHandler that we used in our script in Step 1 of the wizard.
// create a function to execute when the event occurs. the
// callback element is passed in with the event.
function
changeEventForDrowdown
(
el
) {
Browser.msgBox(
"The dropdown has changed!"
);
}
// create event handler object, indicating the name of the function to run
var
dropdownHandler
=
app
.createServerChangeHandler(
'changeEventForDrowdown'
);
// set the callback element for the handler object.
dropdownHandler
.addCallbackElement(
tabPanel
);
// add the handler to the "on change" event of the dropdown box
contactGroupDropdown.addChangeHandler(
dropdownHandler
);
4. Contacts Service
When a user of the script chooses to share a specific group, the script saves that group contact data into a spreadsheet. When a sharing recipient clicks on the run button to accept the contacts share request, the script fetches the contact group data from the spreadsheet and uses the
Contacts Service
to create contacts for the share recipients.
var
group
= ContactsApp.createContactGroup(
myGroupName
);
for
(
var
i
=
0
;
i
<
sheet
.getLastRow();
i
++) {
var
firstName
=
sheet
.getRange(
i
,
1
,
1
,
1
).getValue();
var
lastName
=
sheet
.getRange(
i
,
2
,
1
,
1
).getValue();
var
email
=
sheet
.getRange(
i
,
3
,
1
,
1
).getValue();
var
myContact
= ContactsApp.createContact(
firstName
,
lastName
,
email
);
// ...
// set other contact details
// ...
myContact
.addToGroup(
group
);
}
As this application shows, Apps Script is very powerful. Apps Script has the ability to create applications which allow you to integrate various Google and non-Google services while building complex user interfaces.
You can find Dito’s Personal Contact Group Sharing Script
here
.
Click here
to view the video demonstration of this application. You can also find
Dito Directory
on the
Google Apps Marketplace
.
Posted by Steve Webster and Vinay Thakker from Dito
Want to weigh in on this topic?
Discuss on Buzz
Free Trial
Labels
.NET
3
#io15
1
#io16
1
Admin SDK
10
Administrative APIs
31
AdSense
1
analytics
5
Android
8
API
3
APIs
3
App Engine
5
Apps
1
Apps Script
118
Audit
2
Auth
5
billing
4
Charts
2
Chrome OS
1
classroom
3
Cloud Storage API
1
Community
1
decks
1
Design
1
Developers
12
Directory API
3
Drive
4
Drive SDK
41
execution API
2
Firebase
1
Forms
1
Freemium
1
Fusion Tables
2
G Suite
24
Gadgets
5
Gmail
7
Gmail APIs
23
Google
3
Google APIs
36
Google Apps
33
Google Apps Marketplace
7
Google Calendar API
25
Google Classroom
4
Google Cloud Directory
1
Google Contacts API
4
Google Data Protocol
8
google docs
5
Google Docs API
22
Google Drive
8
Google Drive SDK
7
Google Forms
8
Google I/O
3
Google Prediction API
3
Google Profiles API
2
Google sheets
11
Google Sheets API
7
Google Sites API
5
Google Slides API
10
Google Spreadsheets API
5
Google Talk
1
Google Tasks API
8
Google+
3
googlenew
1
Groups
2
GSuite
3
Guest Post
43
Hangouts Chat API
1
I
1
Inbox
1
iOS
2
issue tracker
1
ISVs
2
java
1
JavaScript
6
marketing
3
Marketplace
47
Marketplace ISV Guest
21
Migration
2
Mobile
5
mpstaffpick
1
oauth
16
OpenID
8
PHP
1
presentations
1
python
7
realtime API
2
Resellers
2
Ruby
1
SaaS
1
security
5
Sheets API
3
spreadsheets
3
Staff Picks
2
tool
1
tools
2
tutorials
2
video
4
videos
1
webinar
2
Archive
2018
Jul
Jun
May
Mar
Feb
Jan
2017
Dec
Nov
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar
Feb
Jan
2016
Dec
Nov
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar
Jan
2015
Dec
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar
Feb
2014
Dec
Oct
Sep
Aug
Jul
Jun
May
Mar
Feb
Jan
2013
Dec
Nov
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar
Feb
Jan
2012
Dec
Nov
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar
Feb
Jan
2011
Dec
Nov
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar
Feb
Jan
2010
Dec
Nov
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar
Feb
Feed
Google
on
Follow @gsuitedevs