Last active
December 23, 2019 01:15
-
-
Save prasanthmj/4c97d8cecfe83700f9d7e2050bbd2055 to your computer and use it in GitHub Desktop.
Apps script sample adding contacts from google sheets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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