I have a typescript function where I am trying to read a file from an attachment on an Ontology object.
I can do this with Excel’s by having:
import * as XLSX from 'xlsx';
and
const attachmentData: Blob = await doc.attachment!.readAsync();
// Convert the Blob to an ArrayBuffer
const arrayBuffer = await attachmentData.arrayBuffer();
const workbook = XLSX.read(arrayBuffer, { type: 'buffer'});
But I can’t seem to do the equivalent with a plain text file that is actually a tab delimited file of records.
I am trying (based on guidance from AIP Assist):
const attachmentData: Blob = await doc.attachment!.readAsync();
// Convert the Blob to an ArrayBuffer
const arrayBuffer = await attachmentData.arrayBuffer();
// Convert ArrayBuffer to string
const textDecoder = new TextDecoder("utf-8");
const tabDelimitedData = textDecoder.decode(arrayBuffer );
But I’m getting problems because TextDecoder
is not natively available in the platform.
From Googling around i see a library @types/text-encoding
which should be a good Polyfill to allow use of TextDecoder, but I can’t get it to work.
Trying to import it with
import { TextDecoder } from '@types/text-encoding';
give a compile error: Module not found: Error: Can't resolve 'text-encoding'
.
importing with
import { TextDecoder } from 'text-encoding';
fixes the compile error but gives a runtime error of Module not found: Error: Can't resolve 'text-encoding'
.
Overall, I am stumped as to how I should read a simple text file as an ontology attachment via Typescript. Am I over complicating it?
Any advice welcome, please.