MCPcopy Create free account
hub / github.com/javascriptdata/danfojs / $readExcel

Function $readExcel

src/danfojs-base/io/node/io.excel.ts:54–98  ·  view source on GitHub ↗
(filePath: string, options: ExcelInputOptionsNode = {})

Source from the content-addressed store, hash-verified

52 * ```
53 */
54const $readExcel = async (filePath: string, options: ExcelInputOptionsNode = {}) => {
55 const {
56 sheet,
57 method,
58 headers,
59 frameConfig,
60 parsingOptions
61 } = { sheet: 0, method: "GET", headers: {}, frameConfig: {}, parsingOptions: {}, ...options }
62
63 if (filePath.startsWith("http") || filePath.startsWith("https")) {
64
65 return new Promise((resolve, reject) => {
66 fetch(filePath, { method, headers }).then(response => {
67 if (response.status !== 200) {
68 throw new Error(`Failed to load ${filePath}`)
69 }
70 response.arrayBuffer().then(arrBuf => {
71 const arrBufInt8 = new Uint8Array(arrBuf);
72 const workbook = read(arrBufInt8, { type: "array", ...parsingOptions });
73 const worksheet = workbook.Sheets[workbook.SheetNames[sheet]];
74 const data = utils.sheet_to_json(worksheet, { defval: null });
75 const df = new DataFrame(data, frameConfig);
76 resolve(df);
77 });
78 }).catch((err) => {
79 reject(err)
80 })
81 })
82
83 } else {
84 return new Promise((resolve, reject) => {
85 fs.access(filePath, fs.constants.F_OK, (err) => {
86 if (err) {
87 reject("ENOENT: no such file or directory");
88 }
89
90 const workbook = readFile(filePath, parsingOptions);
91 const worksheet = workbook.Sheets[workbook.SheetNames[sheet]];
92 const data = utils.sheet_to_json(worksheet, { defval: null });
93 const df = new DataFrame(data, frameConfig);
94 resolve(df);
95 })
96 });
97 }
98};
99
100/**
101 * Converts a DataFrame or Series to Excel Sheet.

Callers

nothing calls this directly

Calls 1

startsWithMethod · 0.80

Tested by

no test coverage detected