In Using Advanced Drive service to convert files I covered how to use the Advanced Drive Service to convert Sheets to Excel and write the result back to Drive. It occurred to me that, since Microsoft OneDrive has a pretty good API too, I might as well do what you probably might want to do in the end - namely convert an Apps file to its Office equivalent, and put the result directly on OneDrive. The OneDrive API uses OAUTH2, just like Google APIS do. In fact I covered how to do this in Microsoft Live service for Goa Examples using the Goa library to help out with the OAUTH2 dance. I'll go through that again, specifically for this example, at the end of the article. Setting things up on OneDriveI recommend you create folders on OneDrive to receive the files you'll be copying over there. I'm using /Documents/conversions/ for all of mine. I don't create this automatically (although I could) as it's better to have a double check that things are going to the place you want them to. Next you need to make a list of the file ids on Google Drive that need converted, and what to convert them to. You'll notice that I'm not using the complex mimetypes to specify this, but the more friendly app names. Here I'm converting sheets, docs and presentation files. I think it's self explanatory what's going on here. var filesToConvert = [{ "id":"1-9A9c8GJAnh7MtKSHmuUmC7dVeogAtKnpCfwOWsSOuM", "to":"powerpoint", "folder":"/Documents/conversions/" }, { "id":"165pHbC6MLbhVdPd2577eS1CKpygtjuFhRHxwoPRueT8", "to":"word" , "folder":"/Documents/conversions/" }, { "id":"1TxZ9Ut5VIOpJF_Zf2KwFtbup4RWCV_8rsCBz4Gkv6SU", "to":"excel", "folder":"/Documents/conversions/" }]; List of known conversionsI've set up these short names for conversions. They are of course specific to certain types of input files (sheets to excel for example), and will fail if the conversion type is not supported (say, slides to csv), but in any case, I may add more to this over time if there's a demand. var MIME_TYPES = { slides:'application/vnd.google-apps.presentation', powerpoint:'application/vnd.openxmlformats-officedocument.presentationml.presentation', pdf:'application/pdf', word:'application/vnd.openxmlformats-officedocument.wordprocessingml.document', text:'text/plain', docs:'application/vnd.google-apps.document', richText:'application/rtf', openDocument:'application/vnd.oasis.opendocument.text', html:'text/html', zip:'application/zip', openSheet:'application/x-vnd.oasis.opendocument.spreadsheet', excel:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', csv:'text/csv', sheets:'application/vnd.google-apps.spreadsheet' }; Main functionThis function works through the list of files to be converted, does the work and logs a link to the file on OneDrive in case you want to do something with it. /** * does the work of the script * the webapp will have been run once already * in order to generate the necessary token */ function convertAndCopy () { // now convert each file and write to oneDrive filesToConvert.forEach (function (d) { // convert the file type var convertedFile = convertFile (d); // write it to onedrive var oneDriveFile = copyToOneDrive (convertedFile); // show the url for accessing it on onedrive Logger.log (oneDriveFile.webUrl); }); } Converting the filesThis converts the file to the chosen format. /** * convert a list of files to requested type * @param {object} conversion the id & requested types * @return {object} the conveted blobs abns various meta information */ function convertFile (conversion) { // get the files metadata var meta = Drive.Files.get (conversion.id); if (!meta) { throw 'Couldnt get file id ' + conversion.id; } // check that we know how to convert that if (!MIME_TYPES[conversion.to]) { throw 'dont know how to convert to ' + conversion.to; } // and that this kind of file supports it var exportLink = meta.exportLinks[MIME_TYPES[conversion.to]]; if (!exportLink){ throw meta.title + '(' + meta.mimeType + ') ' + ' cannot be converted to ' + conversion.to; } // now get the blob return { conversion:conversion, meta:meta, toType:MIME_TYPES[conversion.to], outputName:conversion.folder + meta.title + exportLink.replace(/.*\&exportFormat=(\w+).*/,'.$1'), blob:UrlFetchApp.fetch ( exportLink, { headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() } }).getBlob() }; }
|
Services > Desktop Liberation - the definitive resource for Google Apps Script and Microsoft Office automation > OAuth2 for Apps Script in a few lines of code >