SheetJS将Header名称更改为dynamic



我正试图将sheetJS中的标题名称更改为我从表单收到的内容。下面是我的表单提交函数:

onFormSubmit() {
const fileReader = new FileReader();
const solution = 'testsolution'; // normally i get this from a form input
fileReader.onload = (e) => {
this.arrayBuffer = fileReader.result;
const data = new Uint8Array(this.arrayBuffer);
const arr = [];
for (let i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
const bstr = arr.join('');
const workbook = XLSX.read(bstr, {type: 'binary'});
const first_sheet_name = workbook.SheetNames[0];
const worksheet = workbook.Sheets[first_sheet_name];
// change the column names to match entity

const records = XLSX.utils.sheet_to_json(worksheet, {raw: true});
console.log('records - ', records);
};
fileReader.readAsArrayBuffer(this.file);
}

例如,目前我的头文件是这样的:姓名,姓,姓我希望它们像这样以'testsolution'结尾加上下划线和小写

testsolution_altid, testsolution_firstname, testsolution_lastname

注意:页眉名称可能会根据上传的工作表而更改。例如,下一个上传的工作表可能包含标题:

AltId, Address1, Address2, City, State

结果应该是Testsolution_altid, testsolution_address1, testsolution_address2, testsolution_city, testsolution_state

任何帮助将不胜感激!

如果对其他人有帮助,我就这样做:

// change the column names to match entity
const newHeaders = [];
const columnCount = XLSX.utils.decode_range(ws['!ref']).e.c +1;
console.log(columnCount);
for (let i = 0; i < columnCount; ++i) {
newHeaders[i] = solution+'_'+ ws[`${XLSX.utils.encode_col(i)}1`].v.toLowerCase();
// console.log(header[i]);
}
// console.log(newHeaders);

相关内容

  • 没有找到相关文章

最新更新