none
Simple getSelecteDataAsync - Not Working RRS feed

  • Question

  • I'm following the MSDN tutorials and for the life of me, I can't get the first example of getSelectedDataAsync to actually return the text to the app.

    Here is my code:

    function getData() {
        Office.context.document.getSelectedDataAsync(Office.CoercionType.Text,
    function (asyncResult) {
    	if (asyncResult.status == Office.AsyncResultStatus.Failed) {
    		write('Action failed. Error: ' + asyncResult.error.message);
     	} else {
    		write('Selected data: ' + asyncResult.value);
    	} // end if
    });
    } //end getData
    
    // Function that writes to a div with id='message' on the page.
    function write(message) {
        document.getElementById('message').innerText += message;
    }

    In my HTML body, I have this to call the function, and div setup to display the result:

     <button id="task1" style="height:10%; width:24%" onclick=getData()>Get Data</button>
    <div id="message"></div>

    and I also reference the javascript file in the <head>

    Any thoughts as to why this isn't working?


    • Edited by AllSyntax Tuesday, August 26, 2014 4:18 PM fixed code tags
    Tuesday, August 26, 2014 4:15 PM

All replies

  • I've made some progress by pasting the javascript directly in the HTML head. This is getting the response from the code. Why would including the javascript in a separate file not work? Maybe I'm doing something wrong that's super simple. Thanks

    <script src="MyScript.js" type="text/javascript"></script>

    Tuesday, August 26, 2014 5:53 PM
  • Hi AllSyntax,

    >>Any thoughts as to why this isn't working?<<

    Did you get any error message? As far as I test, the code in separacter JavaScript works well for me. Here is my test steps:

    1. Create apps for Office project
    2. Create MyScript.js file under the Home folder like figure below and copy the code above to this file

    3. modify the reference of home.html like code below

    <script src="MyScript.js" type="text/javascript"></script>

    4. add an div element named messge and add a button like below

        <button id="task1" style="height:10%; width:24%" onclick=getData()>Get Data</button>
                <div id="message"></div>

    5. run the project and type 'test' word in cell A1 and click the button. The code works well.

    Did I miss any steps? Where did you put the MyScript.js?

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, August 27, 2014 7:23 AM
    Moderator
  • I get an error on the getData() function:

    Unhandled Exception at line 2, column 5 in myScript.js
    
    JavaScript runtime error: Unable to get property 'document' of undefined or null reference.

    I started a new project from scratch and still the same issue.
    Wednesday, August 27, 2014 1:42 PM
  • Hi AllSyntax,

    Since I can't reproduce the issue on  my test enviroment, would you mind sharing the project with us to help us to reprocue this issue?

    You can upload it via OneDrive. Please remove the sensitive information before upload the project.

    Best regards

    Fe


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, August 28, 2014 5:36 AM
    Moderator