在XMLHttpRequest

时间:2019-11-28 16:09:47

标签: typescript

我的组件FillTeacherChanges777Component中有一个数组,而此类有一个数组:

teachersList: TeacherModel[] = [];

我正在阅读一个Excel文档,并对其进行写入。 我的问题是,当我尝试在XMLHttpRequest中获取this.teachersList时,未定义数组。 我试图定义全局数组,但它没有帮助,或者我没有正确执行它。 我张贴了我的代码,请有人帮我吗?

var url = "./../assets/gg.xlsx";
    var oReq = new XMLHttpRequest();
    oReq.open("GET", url, true);
    oReq.responseType = "arraybuffer";

    oReq.onload = function (e) {
      var arraybuffer = oReq.response;
      /* convert data to binary string */
      var data = new Uint8Array(arraybuffer);
      var arr = new Array();
      for (var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
      var bstr = arr.join("");

      /* Call XLSX */
      var workbook: XLSX.WorkBook = XLSX.read(bstr, { type: "binary", cellStyles: true });
      /* DO SOMETHING WITH workbook HERE */
      var first_sheet_name = workbook.SheetNames[0];

      /* Get worksheet */
      var worksheet: XLSX.WorkSheet = workbook.Sheets[first_sheet_name];

------------------here is the problem----------------------------
      for (let row = 8, i = 0; row < 1000; row = row + 2, i++) {
        var t = 'B' + row;
        XLSX.utils.sheet_add_json(worksheet, [
          { B: this.teacheList[i].id, C: i + 1 }
        ], { skipHeader: true, origin: t });

      }
      XLSX.writeFile(workbook, "sheetjs.xlsx", { cellStyles: true });
      console.log(XLSX.utils.sheet_to_json(worksheet, { raw: true }));

0 个答案:

没有答案