Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function _parseExcel(data) {
const fixedData = fixdata(data)
// btoa是binary to ascii 将binary的数据用ascii码表示
const workbook = XLSX.read(btoa(fixedData), {type: 'base64'})
// 读取多个sheet表
let sheetTable = []
const sheetList = workbook.Sheets
if (sheetList) {
Object.keys(sheetList).forEach(key => {
const worksheet = sheetList[key]
if (RANGE_KEY in worksheet) {
if (ignore && ignore.length) {
const wsRefArr = worksheet[RANGE_KEY].split(':')
const startCellRowNum = ignore[0].replace(/[a-zA-Z]+/, '') || 0
const startCellRowStr = ignore[0].replace(/[0-9]+/, '') || 'A'
const endCellRowNum = ignore[1].replace(/[a-zA-Z]+/, '') || 0
const maxNum =
startCellRowNum > endCellRowNum ? startCellRowNum : endCellRowNum
worksheet[RANGE_KEY] = `${startCellRowStr}${+maxNum + 1}:${
wsRefArr[1]
reader.onload = function (e) {
// 数据预处理
var binary = "";
var bytes = new Uint8Array(e.target.result);
var length = bytes.byteLength;
for (var i = 0; i < length; i++) {
binary += String.fromCharCode(bytes[i]);
}
/* 读取 workbook */
var wb = XLSX.read(binary, {
type: 'binary'
});
/* 选择第一个sheet */
var wsname = wb.SheetNames[0];
var ws = wb.Sheets[wsname];
/* excel转换json数组,加上{header:1}是普通数组,不写是对象数组 */
self.data = XLSX.utils.sheet_to_json(ws);
console.log(self.data);
/* 生成html表格 */
var HTML = XLSX.utils.sheet_to_html(ws);
document.getElementById('out-table').innerHTML = HTML;
/* 显示导出Excel按钮 */
const steps = resources.map(async res => {
if (excelFormats.includes(res.descriptor.format)) {
const buffer = await res.buffer
const workbook = XLSX.read(buffer, {type: 'buffer', bookSheets: true})
if (sheets === 'all') {
sheets = Array.from(Array(workbook.SheetNames.length).keys())
// We are using sheet idx starting from 1 so we need to add 1 to each idx:
sheets = sheets.map(value => value + 1)
} else if (sheets) { // When sheets are indexes of sheets separated by ','
sheets = sheets.split(',').map(value => parseInt(value))
} else { // Default case
sheets = [1]
}
for (let i of sheets) {
const rows = await toArray(await xlsxParser(res, false, i-1))
const schema = await infer(rows)
const step = {
input: res.descriptor.name,
output: `${res.descriptor.name}-sheet-${i}`,
tabulator: {
export const read = (data, type) => {
/* if type == 'base64' must fix data first */
// const fixedData = fixdata(data)
// const workbook = XLSX.read(btoa(fixedData), { type: 'base64' })
const workbook = XLSX.read(data, { type: type })
const firstSheetName = workbook.SheetNames[0]
const worksheet = workbook.Sheets[firstSheetName]
const header = get_header_row(worksheet)
const results = XLSX.utils.sheet_to_json(worksheet)
return { header, results }
}
function productKeywordsFromInput(input) {
let keywords = [];
// --- we read the sheet data directly instead of `sheet_to_json` as:
// --- `sheet_to_json` return [] if there is only one row
// --- the type data & row num is lost during conversion
const allSheetsData = Object.values(
XLSX.read(input.array, {
type: "array"
}).Sheets
);
for (let i = 0; i < allSheetsData.length; i++) {
keywords = keywords.concat(
getKeywordsFromWorksheet(
allSheetsData[i],
MAX_KEYWORDS,
false,
true
)
);
keywords = uniq(keywords);
if (keywords.length >= MAX_KEYWORDS && largeTextBlockIdentified) {
return keywords.slice(0, MAX_KEYWORDS);
parseSpreadsheetData (e, data) {
this.processing = true
// debugger
let finalJsonObj = {}
let workbook = _XLSX.read(data, {type: 'binary'})
let sheetName = workbook.SheetNames[0]
this.updateContractName(workbook.SheetNames[0])
let jsonObject = workbook.Sheets[sheetName]
let len = Object.keys(jsonObject).length
let cloneObj = JSON.parse(JSON.stringify(jsonObject))
// debugger
// dirty code to get the rich text values
for (let i = 0; i < len; i++) {
let objKey = Object.keys(jsonObject)[i]
if (objKey !== '!ref') {
let tmp = jsonObject[objKey].h
if (!tmp) {
tmp = jsonObject[objKey].w
}
cloneObj[objKey].w = tmp
}
const convertExcelToJson = function(config = {}, sourceFile) {
_config = config.constructor === String ? JSON.parse(config) : config;
_config.sourceFile = _config.sourceFile || sourceFile;
// ignoring empty lines by default
_config.includeEmptyLines = _config.includeEmptyLines || false;
// at least sourceFile or source has to be defined and have a value
if (!(_config.sourceFile || _config.source)) {
throw new Error(':: \'sourceFile\' or \'source\' required for _config :: ');
}
let workbook = {};
if (_config.source) {
workbook = XLSX.read(_config.source, {
sheetStubs: true,
cellDates: true
});
} else {
workbook = XLSX.readFile(_config.sourceFile, {
sheetStubs: true,
cellDates: true
});
}
let sheetsToGet = (_config.sheets && _config.sheets.constructor === Array) ?
_config.sheets :
Object.keys(workbook.Sheets).slice(0, (_config && _config.sheets && _config.sheets.numberOfSheetsToGet) || undefined);
let parsedData = {};
sheetsToGet.forEach(sheet => {
reader.onload = e => {
const data = e.target.result
const workbook = XLSX.read(data, { type: 'array' })
const firstSheetName = workbook.SheetNames[0]
const worksheet = workbook.Sheets[firstSheetName]
const header = get_excel_header_row(worksheet)
const body = XLSX.utils.sheet_to_json(worksheet)
if (callback) callback({
header,
body
})
}
reader.readAsArrayBuffer(file)
reader.onload = e => {
const data = e.target.result
const fixedData = this.fixdata(data)
const workbook = XLSX.read(btoa(fixedData), { type: 'base64' })
const firstSheetName = workbook.SheetNames[0]
const worksheet = workbook.Sheets[firstSheetName]
const header = this.get_header_row(worksheet)
const results = XLSX.utils.sheet_to_json(worksheet)
this.generateDate({ header, results })
}
reader.readAsArrayBuffer(itemFile)
stream.fetch({method:'GET', type:'text', url:this.fileUrl}, function(res){
const wb = XLSX.read(res.data, {type:'base64'});
const ws = wb.Sheets[wb.SheetNames[0]];
self.data = XLSX.utils.sheet_to_json(ws, {header:1});
});
},