Search code examples
javascriptexcelbookmarklet

js-xlsx Uncaught TypeError: console.log is not a function


I am using xlsx for reading Excel file. It kinda works... At least first row. I don't know what's the problem so here I am.

this is my code:

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

Now the result I get in console is:

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

As You can see in C1 I get that console.log is not a fucntion, but why? Where's my mistake? What am I doing wrong?

Sincerely, Thomas


Solution

  • console.log is provided by the environment in which your code runs. Or not. It depends on the environment. Modern browsers provide it (at least if you have devtools open; some versions of IE don't provide it if you don't). NodeJS provides it.

    Two possibilities:

    1. Your environment doesn't provide it.

    2. It does, but then you run this line of code:

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

      which overwrites it with a string. Strings aren't functions, so the attempt to call it on any of your lines that use it correctly (like console.log(address_of_cell);) will now start failing.

      That line should be like the others, a function call:

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