Google Apps Script: Export all Google contacts as CSV - google-apps-script

I have just started to learn Google Apps Script to solve one practical task. I'd like to write a script that periodically exports all contacts in my Google account into a CSV file and sends it to a predefined email address. This should be a kind of automatic versioned backup if data loss will occur in my contact list.
Actually what I am trying to do is to use the native export functionality available in the web interface of the Google contacts (More > Export) in my script. I skimmed through the Google API, but I could not find a service or object in the Google API that does what I need. Is it possible at all?

Here's something I threw together to get a few of the basic fields in your contacts into something that could be a csv output. You may want to add more fields and perhaps use different delimiters.
function getAllContacts() {
var contactsA=ContactsApp.getContacts();
var s='';
var br='<br />';//line delimiter change to linefeed when not using html dialog
var dlm=' ~~~ ';//field delimiter
for(var i=0;i<contactsA.length;i++)
{
s+=Utilities.formatString('<br />\'%s\',\'%s\',\'%s\',\'%s\',\'%s\'%s',
(typeof(contactsA[i].getFullName())!='undefined')?contactsA[i].getFullName():'',
(typeof(contactsA[i].getAddresses().map(function (v,i,A) { return A[i].getAddress();}))!='undefined')?contactsA[i].getAddresses().map(function (v,i,A) { return A[i].getAddress();}).join(dlm):'',
(typeof(contactsA[i].getEmails().map(function(v,i,A) {return A[i].getAddress();}))!='undefined')?contactsA[i].getEmails().map(function(cV,i,A) {return A[i].getAddress();}).join(dlm):'',
(typeof(contactsA[i].getPhones().map(function(v,i,A){return A[i].getPhoneNumber();}))!='undefined')?contactsA[i].getPhones().map(function(v,i,A){return A[i].getPhoneNumber();}).join(dlm):'',
(typeof(contactsA[i].getCompanies().map(function(v,i,A){return A[i].getCompanyName();}))!='undefined')?contactsA[i].getCompanies().map(function(v,i,A){return A[i].getCompanyName();}).join(dlm):'',br);
}
var ui=HtmlService.createHtmlOutput(s).setWidth(800) ;
SpreadsheetApp.getUi().showModelessDialog(ui, 'Contacts')
}

Related

Access Spreadsheet Cell values from library script

Is there a way I can access data, stored within a spreadsheet-file from the library script?
I want to use 1 Google Apps Script from multiple Google Spreadsheet files within my Google Drive.
I followed this answer: "you could use Libraries. The idea is that you create one script that you use as a library" and could successfully import the library to my project.
In order to work, the scripts within the library need some of the cell-values stored in the google sheet files. I know how to access the script via a helper function in my sheet-bound script file. For example:
function loc_my_credits()
{
SISTRIXAbfrageFreigabe.my_credits();
}
Whilst "SISTRIXAbfrageFreigabe" is the library name, and my_credits a function within the library.
When I call loc_my_credits from sheets, nothing happens. My best guess: the script cant read data from the spreadsheet file it needs to execute.
The my_credits script from the library file looks like this:
function my_credits(){
// Base URL to access customsearch
var urlTemplate = "https://api.sistrix.com/credits?api_key=%KEY%";
// initialize sheets: 1. Get the spreadsheet, 2. Get the first and the second sheets in this spreadsheet
var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
var inputSheet = spreadSheet.getSheets()[0];
// Script-specific credentials & search engine
var sistrix_Apikey = inputSheet.getRange('A2').getValue();
var url = urlTemplate.replace("%KEY%", encodeURIComponent(sistrix_Apikey));
var params = {
muteHttpExceptions: true
};
var xml = UrlFetchApp.fetch(url).getContentText();
var document = XmlService.parse(xml);
var root = document.getRootElement();
var items = document.getRootElement().getChildren();
for (var i = 0; i < items.length; i++) {
if(items[i].getName() == 'answer'){
var answer = items[i].getChildren();
return answer[0].getAttribute('value').getValue();
}
}
return 0;
}
Is there a way I can access the data stored in the spreadsheet file from the library script?
I'm writing this answer as a community wiki, since the issue was resolved from the comments section, in order to provide a proper response to the question.
The problem was related to the usage of methods that need scopes that require authorization, therefore it would be expected that simple triggers would show the error:
You do not have permission to call SpreadsheetApp.openById
Google's documentation states that installable triggers would solve the problem:
Installable triggers, however, offer more flexibility than simple triggers: they can call services that require authorization
According to s.Panse, the usage of installable triggers has resolved the issue in this case.
References:
google script openById : You do not have permission to perform that action
Installable Triggers

Google Apps Script - call function deployed as API executable

I have created a Google apps script attached to a google sheet (where I have various methods manipulating the spreadsheet), and I have deployed it as API executable (enabling OAuth etc). Target is to call those methods via REST from an external location not part of Google cloud (like an independent React client, or a standalone server, or my local machine)
Question is: How can I call this from a standalone javascript (like a node.js script executed on my local machine? I do have the script URL (script id) , the secret and the key, but don;t know how to use them all.
Could you help with some sample code, pointers, etc. It looks like my google searches hit only unrelated topics...
You can check this example on how to call the script as an API executable. You will see that the way to call the script from different languages is similar for example using JavaScript, you need to also take note on some important information like:
The basic types in Apps Script are similar to the basic types in JavaScript: strings, arrays, objects, numbers and booleans. The Execution API can only take and return values corresponding to these basic types -- more complex Apps Script objects (like a Document or Sheet) cannot be passed by the API.
An example to make a call the way that you currently want using Apps script would be like:
Target Script
/** This is the Apps Script method these API examples will be calling.
*
* It requires the following scope list, which must be used when authorizing
* the API:
* https://www.googleapis.com/auth/spreadsheets
*/
/**
* Return a list of sheet names in the Spreadsheet with the given ID.
* #param {String} a Spreadsheet ID.
* #return {Array} A list of sheet names.
*/
function getSheetNames(sheetId) {
var ss = SpreadsheetApp.openById(sheetId);
var sheets = ss.getSheets();
return sheets.map(function(sheet) {
return sheet.getName();
});
}
This is the script that you have setup as an API executable and you can call this script using JavaScript like this:
// ID of the script to call. Acquire this from the Apps Script editor,
// under Publish > Deploy as API executable.
var scriptId = "<ENTER_YOUR_SCRIPT_ID_HERE>";
// Initialize parameters for function call.
var sheetId = "<ENTER_ID_OF_SPREADSHEET_TO_EXAMINE_HERE>";
// Create execution request.
var request = {
'function': 'getSheetNames',
'parameters': [sheetId],
'devMode': true // Optional.
};
// Make the request.
var op = gapi.client.request({
'root': 'https://script.googleapis.com',
'path': 'v1/scripts/' + scriptId + ':run',
'method': 'POST',
'body': request
});
// Log the results of the request.
op.execute(function(resp) {
if (resp.error && resp.error.status) {
// The API encountered a problem before the script started executing.
console.log('Error calling API: ' + JSON.stringify(resp, null, 2));
} else if (resp.error) {
// The API executed, but the script returned an error.
var error = resp.error.details[0];
console.log('Script error! Message: ' + error.errorMessage);
} else {
// Here, the function returns an array of strings.
var sheetNames = resp.response.result;
console.log('Sheet names in spreadsheet:');
sheetNames.forEach(function(name){
console.log(name);
});
}
});
Please note as well that there are some limitations that you may want to check before further perform tests.

Google Docs Apps Script get number of columns

On a Google Doc you can set columns from Format > Columns. Now, though, I want to access those columns from Apps Script to confirm the number of columns. I don't even need to modify them, just access. I haven't found anything that jumps out at me from the documentation, so I didn't know if there's an extension of the Document service that would allow for such.
I'm sorry not to include any code, but I have no code obvious to show. I did create a document with 2 columns to see exactly what I'm talking about. https://docs.google.com/document/d/1MyttroeN4kPUm9PfYZnTe_gJqstM3Gb5q3vS3c84dNw/edit
Answer
It is possible using the get method of the Google Docs API
How to do it
In Apps Script, enable the Advanced Docs Service.
Use the method get.
Check the array called content.
Search an object called sectionBreak in each element of content.
Check that the object has the following data: sectionBreak>sectionStyle>columnProperties.
Take the length of the array columnProperties.
(keep in mind that the first occurrence of columnProperties is in the first element of content, skip it and start the loop from the second one.
Code
function myFunction() {
var id = DocumentApp.getActiveDocument().getId()
var result = Docs.Documents.get(id)
var content = result["body"]["content"]
for (var i = 1; i < 20; i++) {
try {
var cols = content[i]["sectionBreak"]["sectionStyle"]["columnProperties"]
console.log('number of columns: ' + cols.length)
} catch (error) {
}
}
}
Reference
Google Docs API
Method: documents.get
Advanced Google services
Advanced Docs Service

Google Apps Script - Contacts - Query on multiple strings

I'm working on a script to remove and re-add about 100 contacts. I have 12 different search criteria for ContactsApp.getContactsByEmailAddress, which the initiated know takes 30+ seconds to run. Is there some way I can only run it once and search all of my criteria? I've looked for others trying to do this same thing and was unsuccessful.
Below is one of the searches from my function (repeats 12 times with various search terms being passed to ContactsApp.getContactsByEmailAddress). I added the try-catch block because the script kept throwing errors out for seemingly no reason during various delete loops.
Would appreciate any and all advice.
var apa = ContactsApp.getContactsByEmailAddress('apa.')
try{
for (var i in apa) {
apa[i].deleteContact()
}
} catch(e){
Logger.log(e)
}
With the Contacts Service, you are limited to a single search criteria. Thus, the only way to search multiple patterns is to call the method once with each search parameter. You can, thankfully, use standard programming practices to minimize the amount of repeated code:
function getContactsWithEmails(emailSearchCriteria) {
if (!emailSearchCriteria || !emailSearchCriteria.length)
throw new Error("No search inputs given");
// Collect the results of each search into a single Array.
const matches = [];
emailSearchCriteria.forEach(function (email) {
var results = ContactsApp.getContactsByEmailAddress(email);
if (results.length)
Array.prototype.push.apply(matches, results);
else
console.log({message: "No results for search query '" + email + "'", query: email, resultsSoFar: matches});
});
return matches;
}
function deleteContacts(arrayOfContacts) {
if (!arrayOfContacts || !arrayOfContacts.length)
throw new Error("No contacts to delete");
arrayOfContacts.forEach(function (contact) {
ContactsApp.deleteContact(contact);
});
}
// Our function that uses the above helper methods to do what we want.
function doSomething() {
// Define all email searches to be performed.
const emailFragmentsToSearchWith = [
"email1",
...
"emailN"
];
const matchingContacts = getContactsWithEmails(emailFragmentsToSearchWith);
if (matchingContacts.length) {
/** do something with the contacts that matched the search.
* someMethodThatSavesContacts(matchingContacts);
* someMethodThatModifiesContacts(matchingContacts);
* deleteContacts(matchingContacts);
* ...
*/
}
/** do other stuff that doesn't need those contacts. */
}
The Google Calendar v3 GData API, as mentioned in this SO question, does support multiple query parameters. However, there is no simple integration with this API - you will need to write the appropriate URL requests and execute them with UrlFetchApp.
In addition to the Google Contacts API, you could use the Google People REST API, specifically the people.connections#list endpoint.
Both of these APIs require you to associate your Apps Script project with a Google Cloud Project that has the respective API enabled, and will likely require you to manually set the scopes your Apps Script project requires in its manifest file, in addition to providing OAuth2 authorizations of the associated HTTP requests you make to the API endpoints.
References
Accessing External APIs
Enabling Google APIs (steps 3-5)
Google Contacts API v3
Google People API
Array#forEach

Exporting data from Cloud SQL to excel using apps script

Is there a way to export cloud SQL data to excel sheet without copying it to Google Spreadsheet using Google Apps Script.
Since there is limitation of Google Spreadsheet of 4,00,000 cells, I am looking to export data directly to Excel sheet rather than copying it to Spreadsheet.
I specifically want to implement it using Google Apps Script.
Yes, you can serve a CSV file, which you can set up to be downloaded. I attached a sample script to show you how it might work.
Warning: Because this is a Comma Separated Values file, you must ensure that your data does not contain commas.
function doGet(e) {
if (parseInt(e.parameter.download) == 1) {
var someData = [{name:"Jerry",
age:27,
married:true},
{name:"Harry",
age:16,
married:false},
{name:"Gary",
age:65,
married:true},
{name:"Larry",
age:41,
married:false}];
var output = "Name,Age,Married?\n";
for (var i in someData) {
output += someData[i].name + ","+someData[i].age + ","+someData[i].married + "\n";
}
return ContentService.createTextOutput(output).downloadAsFile("data.csv");
} else {
var app = UiApp.createApplication();
var anchor = app.createAnchor("Download the CSV File",ScriptApp.getService().getUrl()+"?download=1");
app.add(anchor);
return app;
}
}
Note: Because you are looking for such a large file, this actually might not work. Your script will probably time out. Suppose each entry that you have is 100 bytes, then you will have a 400 MB excel file? That is just large in general.
Could avoid limits of g apps and spreadsheets by using odbc/jdbc directly from excel https://developers.google.com/cloud-sql/docs/external