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

Knoldus Inc.
Knoldus - Technical Insights
2 min readJan 6, 2013

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.

[code language=”html”]

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”};
}
[/code]

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.

--

--

Knoldus Inc.
Knoldus - Technical Insights

Group of smart Engineers with a Product mindset who partner with your business to drive competitive advantage | www.knoldus.com