docs.sheetjs.com/docz/docs/02-getting-started/01-installation/04-amd.md
2024-10-20 13:42:13 -04:00

6.4 KiB

title pagination_prev pagination_next sidebar_position sidebar_custom_props
AMD (define) getting-started/index getting-started/examples/index 4
summary
NetSuite, SAP UI5, RequireJS

import current from '/version.js'; import CodeBlock from '@theme/CodeBlock';

Each standalone release script is available at https://cdn.sheetjs.com/.

xlsx.full.min.js supports AMD with name xlsx out of the box.

{"https://cdn.sheetjs.com/xlsx-" + current + "/package/dist/xlsx.full.min.js"} is the URL for {current}

:::note pass

When referencing by file name, AMD loaders typically omit the file extension.

The actual file name is xlsx.full.min.js, but the examples identify the script using the name xlsx.full.min

:::

:::tip pass

Watch the repo or subscribe to the RSS feed to be notified when new versions are released!

:::

NetSuite

After downloading the script and uploading to the file cabinet, a module alias must be added to the @NAmdConfig configuration file:

{
  "paths": {
    // highlight-next-line
    "xlsx": "/path/to/xlsx.full.min"
  }
}

Once added, SuiteScripts can reference the package using the name xlsx:

/**
 * @NApiVersion 2.x
 * ... more options ...
 // highlight-next-line
 * @NAmdConfig  ./JsLibraryConfig.json
 */
// highlight-next-line
define(['N/file', 'xlsx'], function(file, XLSX) {
  // ... use XLSX here ...
});

More details are included in the NetSuite demo

:::caution Oracle Bugs

NetSuite users reported errors that stem from an Oracle issue. A sample error message is shown below.

Fail to evaluate script: com.netsuite.suitescript.scriptobject.GraalValueAdapter@68d0f09d

This is a NetSuite bug. Only Oracle can fix the bug!

It is strongly encouraged to escalate the issue with Oracle support.

NetSuite users have reported success with the following workaround:

  1. Open the script in a text editor and search for define( in the code.

There will be exactly one instance:

define("xlsx",function(){

Replace the xlsx with sheetjs:

define("sheetjs",function(){
  1. Use the new name in the JSON configuration:
{
  "paths": {
    // highlight-next-line
    "sheetjs": "/path/to/xlsx.full.min"
  }
}
  1. Use the new name in the array argument to the define function call:
/**
 * @NApiVersion 2.x
 * ... more options ...
 // highlight-next-line
 * @NAmdConfig  ./JsLibraryConfig.json
 */
// highlight-next-line
define(['N/file', 'sheetjs'], function(file, XLSX) {
  //               ^^^^^^^                   ^^^^
  //             new module name        same variable
  // ... use XLSX here ...
});

:::

SAP UI5

After downloading the script, it can be uploaded to the UI5 project and loaded in the sap.ui.define call:

sap.ui.define([
  /* ... other libraries ... */
  "path/to/xlsx.full.min"
], function(/* ... variables for the other libraries ... */, XLSX) {
  // use XLSX here
});

:::caution pass

In some deployments, the function argument was undefined.

The standalone scripts add window.XLSX, so it is recommended to use _XLSX in the function arguments and access the library with XLSX in the callback:

sap.ui.define([
  /* ... other libraries ... */
  "path/to/xlsx.full.min"
], function(
  /* ... variables for the other libraries ... */,
  _XLSX // !! NOTE: this is not XLSX! A different variable name must be used
) {
  // highlight-next-line
  alert(XLSX.version); // use XLSX in the callback
});

:::

:::danger pass

Copy and pasting code does not work for SheetJS scripts as they contain Unicode characters that may be mangled. The standalone script should be downloaded and manually uploaded to the project.

:::

RequireJS

:::caution pass

The standalone script must be aliased to the path xlsx.

The requirejs.config function can define aliases through the paths key:

requirejs.config({
  paths: {
    xlsx: [ './xlsx.full.min' ]
  }
});

:::

After configuring the alias, app code can freely require xlsx:

require(['xlsx'], function(XLSX) {
  // ... use XLSX here
});

See the RequireJS demo for details

Dojo Toolkit

Dojo has changed module loading strategies over the years. These examples were tested with Dojo 1.17.3. They are not guaranteed to work with other versions.

Live demos are included in "Dojo Toolkit"

:::caution pass

The standalone scripts add window.XLSX, so it is recommended to use _XLSX in the function arguments and access the library with XLSX in the callback:

require(["xlsx"], function(
  // highlight-next-line
  _XLSX // !! NOTE: this is not XLSX! A different variable name must be used
) {
  // highlight-next-line
  console.log(XLSX.version); // use XLSX in the callback
})

:::

Synchronous Loading

When async is set to false or 0, the scripts can be directly referenced in require calls.

{`\

`}

Asynchronous Loading

When async is enabled, Dojo will only understand the name xlsx. The config object can map package names to scripts:

{`\

`}