Skip to content

Instantly share code, notes, and snippets.

@prasanthmj
Last active December 23, 2019 01:15
Show Gist options
  • Save prasanthmj/4c97d8cecfe83700f9d7e2050bbd2055 to your computer and use it in GitHub Desktop.
Save prasanthmj/4c97d8cecfe83700f9d7e2050bbd2055 to your computer and use it in GitHub Desktop.
Apps script sample adding contacts from google sheets
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Contacts')
.addItem('add Contacts', 'addContact')
.addToUi();
}
function addContact()
{
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getActiveCell();
var active_row = cell.getRow();
var range = sheet.getDataRange();
var first_name = range.getCell(active_row ,1).getValue();
var last_name = range.getCell(active_row ,2).getValue();
var email = range.getCell(active_row,3).getValue();
var contact = ContactsApp.createContact(first_name, last_name, email);
showOutputBox('first name '+first_name+' \nlast name '+last_name+' \nemail '+email, "added contact");
}
function showOutputBox(str, title)
{
var html = HtmlService.createHtmlOutput('<pre>'+str+'</pre>')
.setWidth(400)
.setHeight(300);
SpreadsheetApp.getUi()
.showModalDialog(html, title);
}
function addContacts()
{
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var last_row = range.getLastRow();
var str='';
for(var r=2;r<=last_row;r++)
{
str+= ' name: '+ range.getCell(r,1).getValue()+' '+range.getCell(r,2).getValue()+"\n";
str+= ' email: '+ range.getCell(r,3).getValue()+"\n";
}
var html = HtmlService.createHtmlOutput('<pre>'+str+'</pre>')
.setWidth(400)
.setHeight(300);
SpreadsheetApp.getUi()
.showModalDialog(html, 'My custom dialog');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment