Click here to Skip to main content
15,887,027 members
Articles / Productivity Apps and Services / Microsoft Office
Tip/Trick

Enhance Your Office App Development

Rate me:
Please Sign up or sign in to vote.
4.20/5 (3 votes)
12 Jan 2016CPOL1 min read 5.6K   3  
Edit, Run and Develop JS Code directly within JavaScript

Introduction

This tip is about a new app available in the office store. "Office Developer Sandbox" The App is similar to JSFiddle and allows you to edit, develop and run JavaScript, HTML and CSS code directly inside Office. The app can dramatically reduce development time for Microsoft Office JavaScript apps.

Background

Developing a JS App for Microsoft Office 2013 or newer has one big limitation. Once you changed something in your code, you need to completely reload the add-in. This has an end now. :)

Using the App

The App allows you to edit HTML, CSS and JS code directly within your Office Application at runtime. No reinitialization of your add-in is needed. Just write your JavaScript code, test it and save it for later use or export it to save it as a .js file for your add-in itself. Using the app is really straight forward:

(Example Sourcecode is for Word 2016 but you should be able to use Excel 2013 or newer, Word 2013 or newer and Powerpoint 2013 or newer both local or browser versions.)

Switch to HTML mode and add at least one button to your HTML code:

Image 1

Now add JavaScript code for the button and always remember to wrap your initialization code within $(document).ready().

JavaScript
    $(document).ready(function () {
        // Use this to check whether the new API is supported in the Word client.
        if (Office.context.requirements.isSetSupported("WordApi", "1.1")) {
            $('#emerson').click(insertEmersonQuoteAtSelection);
        }
        else {
            console.log('This code requires Word 2016 or greater.');
        }
    });

function insertEmersonQuoteAtSelection() {
    Word.run(function (context) {

        // Create a proxy object for the document.
        var thisDocument = context.document;

        // Queue a command to get the current selection.
        // Create a proxy range object for the selection.
        var range = thisDocument.getSelection();

        // Queue a command to replace the selected text.
        range.insertText('"Hitch your wagon to a star."\n', Word.InsertLocation.replace);

        // Synchronize the document state by executing the queued commands,
        // and return a promise to indicate task completion.
        return context.sync().then(function () {
            console.log('Added a quote from Ralph Waldo Emerson.');
        });
    })
}

Once added to the JavaScript Tab, it should look similar to the below:

Image 2

Finally, press "Run" and use your code without any reloading or restarting.

Points of Interest

While writing the code for the add-in, I learned a lot about how JavaScript performs inside Office.
You can even take complete parts of C# vsto code and put it into JavaScript. Chances are high that your code is working.

You can get the Add-In here:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Teamwork.com
Ireland Ireland
I´m a german software engineer, passionate microsoft fan and working with everything .net framework offers.

While working on Integrating Microsoft and other Tools into our Product suite i´ll keep writing articles and share my knowledge with fellow developers.I published a couple of articles in print mediums and now start to write guides and articles for code project.

We´re hiring! just drop me a line!

Comments and Discussions

 
-- There are no messages in this forum --