user2969510
user2969510

Reputation: 21

Java Script - export and import

I am developing a Java script Missing Part evaluation tool for Excel-Add-In. For this purpose firstly I developed a class Part.js in Microsoft Visual Studio 2015:

export class Part {
    function Part(number) {

        if (this instanceof Part) {
            this.number = number;
        } else {
            return new Person(firstName)
        }

    }

}; 

module.exports = Part;

Additionally, I wrote the class MissingPartSummary .js

(function () {
    "use strict";

    import * as Part from 'Part';

    Office.initialize = function (reason) {
        $(document).ready(function () {
            //app.initialize();

            $('#create-difference-maps').click(createDifferenceMaps)
        });
    };

    function createDifferenceMaps() {
        Excel.run(function (context) {
            var part = new Part("N1022181");

            var dataSheet = context.workbook.worksheets.getActiveWorksheet();
            //dataSheet.getRange("A1:C3").values = 7;

            var rangeAddress = "F11 : J12";
            var range = dataSheet.getRange(rangeAddress);
            //range.load('cellCount');
            range.load('values');

            return context.sync().then(function () {
                //console.log("Number of cells: " + range.cellCount);
                console.log("Text: " + range.values);
            });

            context.sync();
        }).catch(function (error) {
            console.log("Error: " + error);
            if (error instanceof OfficeExtension.Error) {
                console.debug("Debug info:" + JSON.stringify(error.debugInfo));
            }
        });
    }

    })();

Unfortunately, if I try to execute this code, MS Visual Studio says that there is a syntax error in line 4. The Import of Part fails. Why? Both files Part.js and MissingPartSummary.js are in the same folder ../App/Home/

There is the error message from Visual Studio that I get, if I try to start the Visual Studio Project:

Upvotes: 1

Views: 79

Answers (2)

user2969510
user2969510

Reputation: 21

I've already solved the problem myself. The point about Javascript there exists two types "text/javascript" and "module". Because I've defined type="text/javascript" in MissingPartSummary.html, it lead to the import error. If I change the type to module, there will be no error. But there will be problems on other places. So I decided in favour of text/javascript rather than module. After this I changed Part.js.

var p = p || {};

p.Part = function (row) {
    this.bestellstatus = row[0];
    this.bezeichnung = row[1];
    this.teilenummer = row[2];
    this.teilenummer = this.teilenummer.concat(row[3]);
    this.teilenummer = this.teilenummer.concat(row[4]);
    this.teilenummer = this.teilenummer.concat(row[5]);
    this.teilenummer = this.teilenummer.concat(row[6]);
    this.prozesssteurer = row[19];
    this.bemerkung = row[22];
};

p.Part.prototype = (function () {

    var _isValid = function () {
        var valid = false;

        if (typeof this.bestellstatus == 'string') {
            valid = (this.bestellstatus != "Info");
        }

        if (typeof this.teilenummer == 'string') {
            valid = valid & (this.teilenummer != "");
        }

        return valid
    };


    return {
        isValid: _isValid
    }; 

}()); 

In the next step, I wrote

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

in MissingPartSummary.html. By this way, I can use the Part.js in MissingPartSummary.js in the following way.

var rangeAddressA = "D8 : Z30";
var rangeA = dataSheet.getRange(rangeAddressA);
rangeA.load(['values', 'text', 'rowCount', 'columnCount']);

return context.sync().then(function () {
    var text = rangeA.text;
    var numOrRows = rangeA.rowCount;

    for (var i = 1; i < numOfRows; i++) {
            var part = new p.Part(text[i]);
    }
 });

Upvotes: 1

trevor-trou
trevor-trou

Reputation: 91

Since you're using ES6 Modules (export class Part and import * as Part from 'Part';), you don't need to specify the module exports in Part.js. So you shouldn't need the last line, module.exports = Part;.

Furthermore, in MissingPartSumary.js, you'll need to specify the relative path to Part.js in your import statement. Instead of import * as Part from 'Part';, it should be import * as Part from './Part.js';.

Upvotes: 0

Related Questions