js-xlsx未捕获TypeError:console.log不是函数

时间:2016-01-13 12:11:37

标签: javascript excel bookmarklet

我正在使用xlsx来阅读Excel文件。它有点工作......至少第一排。我不知道问题是什么,所以我在这里。

这是我的代码:

  /* set up XMLHttpRequest */
  var url = "http://localhost/test.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.read(bstr, {type:"binary"});

    /* DO SOMETHING WITH workbook HERE */
    var alphabet = "ABC".split("");
    var first_sheet_name = workbook.SheetNames[0];

    /* Get worksheet */
    var worksheet = workbook.Sheets[first_sheet_name];
    var address_of_cell, desired_cell, desired_value;
    var descript;

    for (i=1;i<30;i++) {
      for (j=0;j<alphabet.length;j++) {
        if (alphabet[j] == "A") {
          console.log("This will be title: "+getValue(alphabet[j], i));
        } else if (alphabet[j] == "B") {
          descript = getValue(alphabet[j], i);
          console.log(""+descript);
        } else if (alphabet[j] == "C") {
          console.log="This will be description: " + descript+" - "+getValue(alphabet[j], i);
        }
      }
    }

    function getValue (column, row) {
      address_of_cell = column+''+row;
      console.log(address_of_cell);
      /* Find desired cell */
      desired_cell = worksheet[address_of_cell];
      /* Get the value */
      desired_value = desired_cell.v;
      return(desired_value);
    }
  };
  oReq.send();

现在我在控制台得到的结果是:

A1
This will be title: VI/46/1998
B1
30-12-1998
C1
Uncaught TypeError: console.log is not a function Bookmarklet.js:43

正如你在C1中看到的,我得到的是console.log不是一个功能,但为什么呢?哪里是我的错?我做错了什么?

此致 托马斯

1 个答案:

答案 0 :(得分:0)

console.log由运行代码的 environment 提供。或不。这取决于环境。现代浏览器提供它(至少如果你有开放的devtools;如果你没有,某些版本的IE不提供它)。 NodeJS提供它。

两种可能性:

  1. 您的环境无法提供。

  2. 确实如此,但是你运行了这行代码:

    console.log="This will be description: " + descript+" - "+getValue(alphabet[j], i);
    

    用字符串覆盖。字符串不是函数,因此尝试在正确使用它的任何行上调用它(如console.log(address_of_cell);)现在将开始失败。

    该行应与其他行类似,函数调用

    console.log("This will be description: " + descript+" - "+getValue(alphabet[j], i));