0

I am developing an Excel Add-In using the Office-JS Library. When some pages are loaded, the JS is not loaded properly.

The Javascript only works after reloading the page.

I disabled turbolink as suggested in another question. But my problem still appears.

My office_connect.js contains the following:

//= require jquery3
//= require jquery_ujs
//= require_self
//= require select2-full
//= require select2_locale_de

And the head of my layout file:

<%= yield :head_top %>

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

<%= stylesheet_link_tag "office_connect"%>
<%= javascript_include_tag "office_connect_app"%>
<script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js" type="text/javascript"></script>
<%= javascript_include_tag "oc_function"%>
<%= javascript_include_tag "office_excel"%>

<title><%=t('layouts.head.text_immodatacockpit')%></title>
<%= csrf_meta_tag %>

<%= yield :head %>

The oc_function file starts Office JS:

// The initialize function must be run each time a new page is loaded.
(function () {
    Office.initialize = function (reason) {
        // If you need to initialize something you can do so here.
    };
})();

A shortened version of my excel.js:

(function () {
    "use strict";

    var cellToHighlight;
    var messageBanner;
    var config;
    var sheetData;
    let housing_output_template_new;
    let housing_output_template_edit;

    // The initialize function must be run each time a new page is loaded.
    Office.initialize = function (reason) {
        $(document).ready(function () {
            
            $(".spinner").hide();
            // If not using Excel 2016, use fallback logic.
            if (!Office.context.requirements.isSetSupported('ExcelApi', '1.1')) {
              $('#subtitle').text("Opps!");
              $("#template-description").text("Sorry, this sample requires Word 2016 or later. The button will not open a dialog.");
              $('#button-text').text("Button");
              $('#button-desc').text("Button that opens dialog only on Word 2016 or later.");
                return;
            }
            $("#select1").select2({placeholder: 'Please choose' , language:'<%= I18n.locale %>',dropdownAutoWidth:true});
            $(".select4").select2({placeholder: 'Please choose' , language:'<%= I18n.locale %>',dropdownAutoWidth:true});
            });
    }
})();

I think it has something to do with the order I load the JS libraries.

Could you point me in the direction of what I am missing?

4
  • Did you try to run the code under the debugger attached? Have you tried using browser developer tools? What is your host application? Do you get the same results on all supported platforms - browser, desktop and etc.? Commented Jun 5, 2022 at 19:24
  • Hi Eugene, thank you for your comment. I have tried the developer tools, the javascript sources are not completely loaded. It happens on the Desktop Versions, Mac and Windows. I tried it on my localhost for development and a "productive" test environment on a docker container behind nginx... Commented Jun 5, 2022 at 21:16
  • 1
    It looks like you are assigning Office.initialize in two places. If these calls are on two different pages, then that's OK. But you shouldn't be doing this twice on the same page. Commented Jun 5, 2022 at 22:18
  • Hi Rick, thank you it works now :-) Can you put it in as answer, so I can accept it? Commented Jun 6, 2022 at 0:12

1 Answer 1

1

It looks like you are assigning Office.initialize in two places. If these calls are on two different pages, then that's OK. But you shouldn't be doing this twice on the same page.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.