Creating Calculator Office Apps using Visual Studio 2012

In this article we are explaining how to create a calculator separately in Office Apps and we can make it in ExcelSheet as well as in Microsoft Word 2013. It depends on your choice. In this article we will make the natural calculator that will perform the calculation on the numbers in a natural way. It will perform +, - , * and / .

Let's have a look at the following steps:

  1. First start Visual Studio 2012 RC.
  2. Now click on the File menu and choose New Project.

  3. A New Project Dialog box opens like this:


  4. In this just expand the Visual C# and under it just expand the Office/SharePoint:


  5. Now select the Apps option from it and select the App for Office 2013 in the center pane as given below:


  6. Give the name to the application and click on OK button.


  7. The Create App for office dialog box will display. In it by default the Task Pane app option is selected and now just click on the Finish button:


  8. Now Visual Studio creates the project and it will display in the Solution Explorer like this:


  9. Now just develop the application and to design the appearance of the app we will add the following HTML code in the default page of the project
    and to do this just remove the default HTML code under the body tag and replace it with the following code as shown below:

    <!DOCTYPE html>

    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge"/>

    <link rel="stylesheet" type="text/css" href="../Content/Office.css" />
    <link rel="stylesheet" type="text/css" href="../Content/App.css" />

    <script src="../Scripts/jquery-1.6.2.js"></script>
    <script src="../Scripts/Office/MicrosoftAjax.js"></script>
    <script src="../Scripts/Office/Office.js"></script>

    <!-- Add your JavaScript to the following file -->
    <script src="../Scripts/calculator.js"></script>
    <div id="SectionContent">
    <div id="result" class="result">
    <span id="storeddata" style="font-size:medium"></span><br />
    <span id="inserteddata">0</span>
    <hr />
    <br />
    <div id="buttons" style="text-align:center;">
    <!-- First row of buttons: C/EC, MSR, M-, M+ -->
    <input id="cleardata" type="button" class="button" value="CE" onclick="clearDatas()"/>
    <input id="memory"  type="button" class="button" value="MSR" />
    <input id="memoryminus"  type="button" class="button" value="M-" />
    <input id="memoryplus"  type="button" class="button" value="M+" />
    <br />
    <!-- Second row of buttons: 7, 8, 9, '/' -->
    <input id="7"  type="button" class="button" value="7" onclick="EnterNumber(this)"/>
    <input id="8"  type="button" class="button" value="8" onclick="EnterNumber(this)"/>
    <input id="9"  type="button" class="button" value="9" onclick="EnterNumber(this)"/>
    <input id="divide"  type="button" class="button" value="/" onclick="includeOperator(this)"/>
    <br />
    <!-- Third row of buttons: 4, 5, 6, '*' -->
    <input id="4"  type="button" class="button" value="4" onclick="EnterNumber(this)"/>
    <input id="5"  type="button" class="button" value="5" onclick="EnterNumber(this)"/>
    <input id="6"  type="button" class="button" value="6" onclick="EnterNumber(this)"/>
    <input id="multiply"  type="button" class="button" value="*" onclick="includeOperator(this)"/>
    <br />
    <!-- Fourth row of buttons: 1, 2, 3, '-' -->
    <input id="1"  type="button" class="button" value="1" onclick="EnterNumber(this)"/>
    <input id="2"  type="button" class="button" value="2" onclick="EnterNumber(this)"/>
    <input id="3"  type="button" class="button" value="3" onclick="EnterNumber(this)"/>
    <input id="minus"  type="button" class="button" value="-" onclick="includeOperator(this)"/>
    <br />
    <!-- Fifth row of buttons: 0, '.', '+', '=' -->
    <input id="0"  type="button" class="button" value="0" onclick="EnterNumber(this)"/>
    <input id="decimal"  type="button" class="button" value="." onclick="EnterNumber(this)"/>
    <input id="plus"  type="button" class="button" value="+" onclick="includeOperator(this)"/>
    <input id="equals"  type="button" class="button" value="=" onclick="evaluate();"/>
    <hr />
    <br />
    <div id="buttonsofcontent" style="text-align:center"></div>

  10. Now to handle the events of all the buttons appearing in the calculator we will write some functions for it and to to this just expand the Scripts
    option in the Solution Explorer as shown below:


  11. Now under the Office folder just open the calculator.js to show the default JavaScript file for the application as shown below:


  12. Now add the following functions to calculator.js like this:

    var result;
    savedOperation = [];

    Office.initialize =
    function (reason) {
    // Use the following API wherever you want access to the Office document object.
    // Office.context.document;

    // Check if setSelectedData method is supported and include sample content to insert data. 
    if (Office.context.document.setSelectedDataAsync) {
    var setDataDiv = "<input type='button' style='width:47%;margin:2px;' onclick='setData()' value='Insert Data' />";
    "buttonsofcontent").innerHTML += setDataDiv;

    // Check if getSelectedData method is supported and include sample content to get data. 
    if (Office.context.document.getSelectedDataAsync) {
    var getDataDiv = "<input type='button' style='width:47%;margin:2px;' onclick='getData()' value='Get Data'/>";
    "buttonsofcontent").innerHTML += getDataDiv;

    // Capture a reference to the output div onload.
    result = document.getElementById(
    savedData = document.getElementById(
    insertedData = document.getElementById(
    savedOperation[0] =
    savedOperation[1] =

    // Add numbers to the number display.

    EnterNumber(node) {

    var newEntry = node.value;
    var dataEntry = insertedData.innerHTML;
    dataEntry = String(dataEntry);

    // Check to see if the decimal button was clicked
    // and that there are no other decimals in the entry.
    if (newEntry == "decimal") {
    if (dataEntry.indexOf(".") == -1) {
    newEntry =
    else {
    newEntry =

    // Add the new entry to the entered data.
    if (insertedData.innerHTML == "0") {
    insertedData.innerHTML = newEntry;
    else {
    insertedData.innerHTML += newEntry;

    // Convert the information in the display to a number

    // and store both operand and operator.

    includeOperator(node) {

    if (savedOperation[1] == "undefined") {

    // Parse the value and operator for the operation.
    var operator =;
    var operand = insertedData.innerHTML;
    var operation;

    operand = parseFloat(operand);

    // Store the operation as an anonymous function
    // in the global variable storedOperation.
    switch (operator) {
    case "divide":
    operation =
    function (a, b) { return a / b };
    savedData.innerHTML = operand +
    " / ";

    case "multiply":
    operation =
    function (a, b) { return a * b };
    savedData.innerHTML = operand +
    " * ";

    case "minus":
    operation =
    function (a, b) { return a - b };
    savedData.innerHTML = operand +
    " - ";

    case "plus":
    operation =
    function (a, b) { return a + b };
    savedData.innerHTML = operand +
    " + ";

    // Store the entered data and the operator into a global variable.
    // Reset the innerHTML of the enteredData span.
    savedOperation[0] = operand;
    savedOperation[1] = operation;
    insertedData.innerHTML =

    // Removes all entries from the output and sets the value to '0'.

    clearEntries() {
    insertedData.innerHTML =
    storedData.innerHTML =

    // Gets the data and operation stored in the global array,

    // evaluates the expression, and returns the results.

    evaluate() {

    // Ensure that some data is stored already before attempting to evaluate.
    if (savedOperation[0] != "undefined") {

    // Define the two operands and operation to perform.
    var x = savedOperation[0];
    var y = insertedData.innerHTML;
    y = parseFloat(y);
    var operation = savedOperation[1];
    var result;

    result = operation(x, y);
    result = result.toFixed(5);

    // Display results and clear the stored data span.
      insertedData.innerHTML = result;
    savedData.innerHTML =
    // Clear the data out of the storedOperation array.
    savedOperation[0] =
    savedOperation[1] =

    // JavaScript has some issues with evaluating floating point
    // decimal numbers, so they need to be evaluated differently.

    evaluateFloat(x, y, operation) {

    var stringX = String(x);
    var stringY = String(y);
    var decimalPlace = 0;
    var result;

    // Determine the level of precision needed for the float operation.
    // Check the x variable to see if it is the float.
    if (stringX.indexOf(".") > -1) {
    decimalPlace = (stringX.length - stringX.indexOf(
    ".")) - 1;

    // Check to see if the y variable is the float, and if so,
    // if it has more decimal places.
    if (stringY.indexOf(".") > -1) {
    if (decimalPlace < ((stringY.length - stringY.indexOf(".")) - 1)) {
    decimalPlace = (stringY.length - stringY.indexOf(
    ".")) - 1;

    // Transform the numbers into integers by multiplying them
    // by 10 to a power equal to the number of decimal places.
    result = operation((x * Math.pow(10, decimalPlace)), (y * Math.pow(10, decimalPlace)));
    result = Math.round(result) / Math.pow(10, decimalPlace);

    return result;

    // Inserts the current entered data/ result into

    // the currently selected place in the document.

    setData() {

    // Get the current value in the entereddata div.
    var value = insertedData.innerHTML;

    // Set the selected data into the document as text.
    function () { });

    // Gets data from the current document and returns it as a number.

    getData() {

    var dataValue = 0;

    // Get the current selection from the Word document.
    function (result) {
    if (result.status != "failed") {
    var results = result.value;
    results = parseFloat(results);

    if (!isNaN(results)) {
    dataValue = results;
    insertedData.innerHTML = dataValue;


  13. Now if we want to see the output in the Word document then just click on the name of the application and under the properties window just see
    the Start Action property, as in:


  14. From the Dropdownlist we will choose the Microsoft Word option.


  15. Now to run the application just press the F5 key or under the Debug menu just click on start debugging like this:


  16. The output window will look like this:


  17. In this if we will click on any number digit and click on +, -, *, / then it will perform the operation on the digits accordingly.