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.
I 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.
I'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.
This 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.
This converts the file to the chosen format.
Finally this copies each file to OneDrive to the specified folder.
If you get an error about Drive being undefined, it means you still haven't done this: 'you'll need to authorize the Advanced Drive service in Apps Script and in the developer console', so go and do that now. It's in the resource menu.
The first step then is to get some credentials from the Microsoft developer console. It's a little primitive compared to its Google equivalent, and one problem is that it only allows one set of credentials per application, and also checks the originating url is not being used by another application - so since all apps script apps originate from script.google.com, that can be a little limiting. But in any case - it's workable.
For details about how Goa works, you can go to Goa Setup.The library, cGoa, is available under this project key, or on github.
Next we need to commit the credentials to a property store. I'm assuming that any app you create would be published as the user accessing the app, and therefore would need a doGet() to initiate an authorization dialog. However, once this is done once, it will automatically refresh tokens so your main app can be triggered or otherwise run without a doGet.
In any case - here's how to setup your one time credentials - just substitute in the ones you get from the Microsoft developer console
Next you'll need to authorize the Google Advanced Drive service through the Apps Script menu (remember to authorize in the developer console as part of that too).
Finally, you'll need a doGet pattern to get authorized with Microsoft - so you can use the code below exactly as written, publish the app - and run it. The first authorization dialog will look like this, so copy the redirect URI to the MS dev console before continuing. The origin should be set to be script.google.com.
That should take you into a Microsoft authorization dialog which will give the script authorization to access your Drive, and then we can forget all about this stuff.
Here's the pattern to do all that you can use. It doesn't need any changes.
Now we're talking to OneDrive, and we can go and do the real work.
For more like this, see OAuth2 for Apps Script in a few lines of code and Google Apps Scripts snippets. Why not join our forum, follow the blog or follow me on twitter to ensure you get updates when they are available.
Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, All formats are available from O'Reilly, Amazon and all good bookshops. You can also read a preview on O'Reilly
Services > Desktop Liberation - the definitive resource for Google Apps Script and Microsoft Office automation > OAuth2 for Apps Script in a few lines of code >