SHEEPJS将标题名称更改为动态

发布于 2025-02-03 08:41:08 字数 1259 浏览 3 评论 0原文

我正在尝试将SheepJ中的标题名称更改为我从表单中收到的内容。这是我的表单提交功能:

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”最终以这样的方式结束

我希望他们以下划线和lowercase testsolution_altid,testsolution_firstname,testsolution_lastname

的指示 。例如,下载的下载表可能包含标题:

altid,address1,address2,City,State,State

,应该像 testsolution_altid,testsolution_Address1,testssolution_address2,testsolution_city,testsolution_state_state

任何帮助都将被应用!

I am trying to change the header names in sheetJS to something I recieve from the form. Here is my form submit function:

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);
  }

currently for example my headers come in like this:
AltId, FirstName, LastName
And I want them to end up like this with 'testsolution' apended with underscore and lowercase

testsolution_altid, testsolution_firstname, testsolution_lastname

NOTE: Header names may change depending on what sheet is uploaded. For example the next sheet uploaded may contain headers:

AltId, Address1, Address2, City, State

And should end up like
testsolution_altid, testsolution_address1, testsolution_address2, testsolution_city, testsolution_state

Any help would be appriciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2025-02-10 08:41:08

我最终这样做的话,如果它可以帮助其他任何人:

 // 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);

I ended up doing it like this if it helps anyone else:

 // 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文