Google Apps Script: Creating Archives of Documents and Files of User in Google Apps Domain


The Google Documents List API allows developers to create, retrieve, update, and delete Google Docs (including text documents, spreadsheets, presentations, and drawings), files, and collections. It also provides some advanced features like resource archives, Optical Character Recognition, translation, and revision history.

Documents List API is useful if we need to store data in the cloud, perform resource management, convert document formats, etc. All interactions with the API require a valid Google Account, which can be a “consumer” account (e.g. Gmail), or a Google Apps account.

Here, we are going to create archives of documents and files of user.
To initiate an archive request, send an authenticated POST request to the archive feed’s URI:

https://docs.google.com/feeds/default/private/archive

The archive feed can be used to export resources as a .zip file. User determines the desired export format for each type of resource and downloads the archive once the process has finished.

The following example archives all documents, spreadsheet, presentation, drawing, images, csv files and pdf, and a collection’s contents, and emails the link to ”<userId>” when the archive operation has finished.


var userId='<user_emailId>';
var consumerKey='<domain_name>';
var consumerSecret='<domain_consumerSecretKey>';

function archiveDocuments() {

var base='https://docs.google.com/feeds/';
var fetchArgs = googleOAuth_('docs', base);

//target attribute defines the export format of documents

var rawXml="<entry xmlns='http://www.w3.org/2005/Atom' xmlns:docs='http://schemas.google.com/docs/2007'>"+
      "<docs:archiveConversion source='application/vnd.google-apps.document' target='application/msword'/>"+
      "<docs:archiveConversion source='application/vnd.google-apps.spreadsheet' target='application/pdf'/>"+
      "<docs:archiveConversion source='application/vnd.google-apps.presentation' target='application/pdf'/>"+
      "<docs:archiveConversion source='application/vnd.google-apps.drawing' target='application/pdf'/>"+
      "<docs:archiveConversion source='application/pdf' target='application/pdf'/>"+
      "<docs:archiveConversion source='image/jpeg' target='application/pdf'/>"+
      "<docs:archiveConversion source='image/jpg' target='application/pdf'/>"+
      "<docs:archiveConversion source='image/png' target='application/pdf'/>"+
      "<docs:archiveConversion source='image/bmp' target='application/pdf'/>"+
      "<docs:archiveConversion source='image/gif' target='application/pdf'/>"+
      "<docs:archiveConversion source='text/csv' target='text/csv'/>"+
      "<docs:archiveNotify>"+userId+"</docs:archiveNotify></entry>";
fetchArgs.payload=rawXml;
var url="https://docs.google.com/feeds/"+userId+"/private/archive?v=3&alt=json"
var urlFetch=UrlFetchApp.fetch(url,fetchArgs);
}

function googleOAuth_(name,scope) {
var oAuthConfig = UrlFetchApp.addOAuthService(name);
oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oAuthConfig.setConsumerKey(consumerKey);
oAuthConfig.setConsumerSecret(consumerSecret);
return {oAuthServiceName:name, oAuthUseToken:"always",
method : "POST",
contentType: "application/atom+xml"};
}

Here, user can archives all documents and files in .zip format. A admin user can archive all domain user’s documents and files by giving user’s email id, once the process has finished, the User can downloads the archive from his account.

About Ruchi Agarwal

Software Consultant at Knoldus Software LLP having around 2 year experience working in Scala and java. She has good understanding of various technologies like LiftWeb, Akka, Amazon EC2, jQuery, javascript, CSS etc. She is RAD (Rational Application Developer) for WebSphere Software and RFT (Rational Funtional Tester) for Java certified. She is a skilled professional focused on the basic business functions and creation of software. She has a firm understanding of design methodology. She likes to work with responsibility, dedication and perseverance, she has a facility to establish good interpersonal relationships and foremost she is loyal to her principles, ethics and always giving to the best of her caliber.
This entry was posted in Web and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s