我正在尝试将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);
}例如,目前我的标题是这样的: AltId、FirstName、LastName和我希望它们以带有下划线和小写的'testsolution‘结束。
testsolution_altid,testsolution_firstname,testsolution_lastname
注意:标题名称可能会根据上载的工作表而改变。例如,上载的下一个工作表可能包含标题:
AltId,Address1,Address2,City,State
应该像testsolution_altid,testsolution_address1,testsolution_address2,testsolution_city,testsolution_state那样结束
任何帮助都是徒劳无功!
发布于 2022-05-31 21:56:47
如果这对其他人有帮助,我就会这样做:
// 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);https://stackoverflow.com/questions/72451494
复制相似问题