Using Microsoft Add-ins

Step:1 Get Started Get started with Office Add-ins (formerly Apps for Office). An Office Add-in runs in an Office application and can interact with data in a document or mail item.

Step:2 Select App We can built add-ins for products Like Excel, Outlook, PowerPoint, Word.

Step:3 Built Development tools, tsd, gulp, bower, yo generator-office.

* First install Node.js and Git. now open command prompt/terminal as administrator permission.
  then install Yeoman, copy below command and run it
    npm install -g yo bower
    that will download and install Yeoman and it's dependencies.
  now install the Office generator.
     npm install -g generator-office
     npm install -g tsd
     npm install -g gulp

*   Create new folder for your add-ins project, go to that folder from command prompt. then run Yeoman generator for office add-ins, for this use following command:
    yo office

    after running above command, it will ask you for few details/information:
      -Name of the add-in - myFirstAddin
      -Root folder of the project - The current folder
      -Type of add-in - Task Pane
      -Technology to use - HTML, CSS & JavaScript
      -Supported Office application - Word (and/or others as desired)
   after completing this Yeoman generator will create the structure and basic files for your add-ins
* Host your add-ins locally by using the following command
      gulp serve-static
* To check that your add-ins is running, open your browser and go to the home page at https://localhost:8443/app/home/home.html

* Now we have to load add-in into Office, We can do this by loading add-in in Office online. follow the simple steps to load add-in.
    1. Go to Word Online. Open blank document(If u don't have account on Office then create it).
    2. Go to Insert > Office Add-ins
    3. On the My Add-ins tab you will see a link in the upper-right corner of the dialog box to Upload My Add-in or Manage My Add-ins. Manage My Add-ins will open a menu where you can then choose Upload My Add-in.
    4. In the Upload Add-in Dialog, choose Browse and select the manifest-myFirstAddin.xml file from the root of the sample project folder, and then choose Upload. Your add-in will load in Word Online.

      Congratulations, you created your first Add-in. Now you can continue to develop your add-in and debug it using your browser’s debugging tools.

Creating Add-in for Word documents with User define functionality.

Following code used to getting content of the word document's selected data.

```js
// Reads data from current document selection and displays a notification

function getDataFromSelection(){ Office.context.document.getSelectedDataAsync(Office.CoercionType.Text, function(result){ if (result.status === Office.AsyncResultStatus.Succeeded) { app.showNotification('The selected text is:', '"' + result.value + '"'); } else { app.showNotification('Error:', result.error.message); } } ); }```

Following code is used for getting content of the first paragraph of the word document.

// Run a batch operation against the Word object model.
Word.run(function (context) {

    // Create a proxy object for the paragraphs collection.
    var paragraphs = context.document.body.paragraphs;

    // Queue a commmand to load the text and style properties for all of the paragraphs.
    context.load(paragraphs, 'text, style');

    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    return context.sync().then(function () {

        // Queue a command to get the last paragraph and create a 
        // proxy paragraph object.
        // var paragraph = paragraphs.items[paragraphs.items.length - 1]; 

       // Queue a command to get the first paragraph and create a 
        var paragraph = paragraphs.items[0]; 

        // Queue a command to select the paragraph. The Word UI will 
        // move to the selected paragraph.
        paragraph.select();

        // Synchronize the document state by executing the queued commands, 
        // and return a promise to indicate task completion.
        return context.sync().then(function () {
            console.log('Selected the last paragraph.');      
        });      
    });  
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

Following code used for getting contents of the all paragraphs of the Word document.

function getAllParagraphs() {
    // Run a batch operation against the Word object model.
    Word.run(function (context) {

        // Create a proxy object for the paragraphs collection.
        var paragraphs = context.document.body.paragraphs;

        // Queue a commmand to load the paragraphs and their text property.
        context.load(paragraphs, 'text');

        // Synchronize the document state by executing the queued-up commands, 
        // and return a promise to indicate task completion.
        return context.sync().then(function () {
            console.log("Number of paragraphs : " + paragraphs.items.length); 
            for (var i = 0; i < paragraphs.items.length; i++) {
                console.log("paragraphs[" + i + "].content  = " + paragraphs.items[i].text);
            }   
        });  
    })
    .catch(function (error) {
        console.log('Error: ' + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log('Debug info: ' + JSON.stringify(error.debugInfo));
        }
    });
}

Following code used to getting content selected Table from word document.

function getTableData(){
    Word.run(function (context) { 
        Office.context.document.getSelectedDataAsync(Office.CoercionType.Table,
        function(result){
         if (result.status === Office.AsyncResultStatus.Succeeded) {

            var val = result.value;
            // console.log(val);
            // console.log(result.value);
            console.log("============Table============");
            console.log("============Header===========");
             for(var i=0; i<val.headers.length; i++)
             {
                 var head = val.headers[i];
                 for(var j=0; j<head.length; j++)
                 {
                    console.log(head[j]);
                 }
             }
            console.log("============Rows=============");         
             for(var i=0; i<val.rows.length; i++)
             {
                 var trow = val.rows[i];
                 for(var j=0; j<trow.length; j++)
                 {
                    console.log(trow[j]);
                 }
             }
             app.showNotification(head,trow);
        } else {
          console.log("Plzzz... Select Table Data.");
          app.showNotification("You Are not Selected Table Data. Plzzz... Select Table Data.");
          app.showNotification('Error:', result.error.message);
        }   
        })
    });
}

results matching ""

    No results matching ""