question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] ExcelJS 4.1.0 is not working with Angular 9 in IE11

See original GitHub issue

🐛 Bug Report

Lib version: 4.1.0 angular: 9.0.7

I have used polyfill code for unicode but still its giving error “Invalid range in character set” in polyfill.ts in IE11

unicode-regex-polyfill.js

import 'core-js/modules/es.promise';
import 'core-js/modules/es.object.assign';
import 'core-js/modules/es.object.keys';
import 'regenerator-runtime/runtime';
import rewritePattern from 'regexpu-core';
import { generateRegexpuOptions } from '@babel/helper-create-regexp-features-plugin/lib/util';

// const { RegExp } = global;
try {
  new RegExp('a', 'u');
} catch (err) {
  // @ts-ignore
  global.RegExp = function(pattern, flags) {
    if (flags && flags.includes('u')) {
      return new RegExp(
        rewritePattern(
          pattern,
          flags,
          generateRegexpuOptions({ flags, pattern })
        )
      );
    }
    return new RegExp(pattern, flags);
  };
  // @ts-ignore
  global.RegExp.prototype = RegExp;
}

polyfill.js

....
....
....
import 'zone.js/dist/zone'; // Included with Angular CLI.
import 'unicode-regex-polyfill.js';
import 'exceljs';
....
....

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:9

github_iconTop GitHub Comments

3reactions
simonigreencommented, Aug 23, 2021

I made a poor man’s polyfill that doesn’t require importing any extra modules like babel. It just discards unicode flag in RegExps and erases parts of the patterns used in exceljs that require unicode support. It’s pretty dirty but it allowed our app to work in IE and even exceljs seems to still do its job.

// Add this code to polyfills.ts

const { RegExp } = window;
try {
    new RegExp('a', 'u');
} catch (err) {
    // @ts-ignore
    window.RegExp =
        (pattern: string, flags: string | undefined) => {
            if (flags?.includes('u') === true) {
                // Ignore unicode flag in RegExp
                flags = flags === 'u' ? undefined : flags.replace('u', '');
                // Discard parts of the patterns used by exceljs that error out in non-unicode RegExps.
                pattern = pattern.replace(/\uDC00-\uDBFF/g, '');
                pattern = pattern.replace(/\uDC00-\uDB7F/g, '');

                return new RegExp(pattern, flags);
            }
            return new RegExp(pattern, flags);
        };
    // @ts-ignore
    window.RegExp.prototype = RegExp;
}

I found that this polyfill worked however the last line needed to be changed to avoid breaking other libraries that might access the prototype of RegExp. Changing the line window.RegExp.prototype = RegExp; to the following fixed the issue and allowed me to use version 4.2.1 of ExcelJS:

window.RegExp.prototype = RegExp.prototype;

2reactions
ThomasKrueglcommented, Jul 27, 2020

I got this working using:

polyfill.ts

import 'core-js/es6/promise';
import 'core-js/es7/symbol';
import 'core-js/es7/object';
import 'regenerator-runtime/runtime';
import './unicode-regex-polyfill';
import 'exceljs/dist/exceljs';

unicode-regex-polyfill.ts:

import rewritePattern from 'regexpu-core';
import { generateRegexpuOptions } from '@babel/helper-create-regexp-features-plugin/lib/util';

const { RegExp } = window;
try {
  new RegExp('a', 'u');
} catch (err) {
  // @ts-ignore
  window.RegExp = function(pattern, flags) {
    if (flags && flags.includes('u')) {
      return new RegExp(
        rewritePattern(
          pattern,
          flags,
          generateRegexpuOptions({ flags, pattern })
        )
      );
    }
    return new RegExp(pattern, flags);
  };
  // @ts-ignore
  window.RegExp.prototype = RegExp;
}

Replaced global with window, and adjusted some polyfill imports.

I added “import ‘exceljs/dist/exceljs’;” before the zone polyfill for angular, because zone complained about Promise being changed, and i remembered this as workaround from another issue.

Also i used “import ‘exceljs/dist/exceljs’;” instead of “import ‘exceljs’;” everywhere, not only in the polyfill.ts. I did NOT use “import ‘exceljs/dist/es5’;” because that somehow got a lot of code in node_modules into the typescript compiler scope, and produced errors for missing fs module (which is fine on windows) and other stuff.

BUT:

  • this is experimental
  • when accessing the functionality, IE hung, said something about long running script, and only after >30 seconds, i got my excel. Same thing in Chrome takes 1 or 2 seconds.
  • reading an excel still failed, “Object doesn’t support property or method ‘arrayBuffer’” - still examining that one.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why ExcelJs plugin is not working in IE11? - Stack Overflow
I am using Angular 9 and excelJs 4.1.1, this is working fine in chrome but only in IE11 giving Error: Invalid range in...
Read more >
How To Fix Your Angular App When It's Not Working in IE11
Sometimes, your application may throw errors in IE11, even when it is working fine in other browsers. There can be numerous reasons why...
Read more >
Why Exceljs Plugin Is Not Working In Ie11 - ADocLib
I am using Angular 9 and excelJs 4.1.1 this is working fine in chrome but only in IE11 giving Error: Invalid range in...
Read more >
exceljs - npm
Sometimes (really very often) workbook has worksheets with id not starting from 1. // For instance It happens when any worksheet has been ......
Read more >
exceljs - npm.io
I have just one request; If you submit a pull request for a bugfix, please add a unit-test or integration-test (in the spec...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found