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:

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='';
var fetchArgs = googleOAuth_('docs', base);

//target attribute defines the export format of documents

var rawXml="<entry xmlns='' xmlns:docs=''>"+
      "<docs:archiveConversion source='application/' target='application/msword'/>"+
      "<docs:archiveConversion source='application/' target='application/pdf'/>"+
      "<docs:archiveConversion source='application/' target='application/pdf'/>"+
      "<docs:archiveConversion source='application/' 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'/>"+
var url=""+userId+"/private/archive?v=3&alt=json"
var urlFetch=UrlFetchApp.fetch(url,fetchArgs);

function googleOAuth_(name,scope) {
var oAuthConfig = UrlFetchApp.addOAuthService(name);
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

Experienced working in Scala/ Java as a Sr. Software Consultant at Knoldus Software LLP for around 4 years. Having good understanding of various technologies includes Play, AngularJS, MongoDB, LiftWeb, Akka, jQuery, Javascript, CSS etc. Certified in RAD (Rational Application Developer) for WebSphere Software and RFT (Rational Funtional Tester) for Java. Skilled professional focused on the basic business functions and creation of software. Having firm understanding of design methodology. Likes to work with responsibility, dedication and perseverance.
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: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s