Search code examples
javascriptencryptiongoogle-apps-scriptgoogle-sheetsgoogle-forms

Google Spreadsheets Script Keeps Writing Empty Strings


I have a Google form that gets submitted to a Google spreadsheet. On the spreadsheet side, I have a Google Apps Script that is supposed to encrypt the submitted password. But for whatever reason, it writes an empty string to where the encrypted password should be. This is really starting to stress my out. Here is my code:

function encryptPassword(e) {
  var password = e.values[6];
  var split = password.split("");
  password = "";
  var char;

  for(char in split) {
    password = password.concat(getBinary(split[char]));
  }
  var spreadsheet = SpreadsheetApp.openById("1CywforbyBmPDHt2Uw9lJtyhqeklAAJp0IG7GfVV6U5U");
  spreadsheet.getRange("G" + spreadsheet.getLastRow().toString()).setValue(password);
  spreadsheet.getRange("H" + spreadsheet.getLastRow().toString()).setValue(password);
}

function getBinary(char) {
  var binary = "";
  var numValue;
  var range;
  var value;
  var chars = [
    ["#", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"],
    ["@", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"],
    ["&", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"],
    ["%", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"],
    ["$", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
  ];

  for(range in chars) {
    for(value in range) {
      if(value > 0) {
        if(char == chars[range][value]) {
          numValue = value - 1;
          binary = binary + chars[range][0];
          if(numValue / 8 >= 1) {
            numValue = numValue - 8;
            binary = binary.concat("1");
          } else {
            binary = binary.concat("0");
          }
          if(numValue / 4 >= 1) {
            numValue = numValue - 4;
            binary = binary.concat("1");
          } else {
            binary = binary.concat("0");
          }
          if(numValue / 2 >= 1) {
            numValue = numValue - 2;
            binary = binary.concat("1");
          } else {
            binary = binary.concat("0");
          }
          if(numValue / 1 >= 1) {
            binary = binary.concat("1");
          } else {
            binary = binary.concat("0");
          }
        }
      }
    }
  }
  return binary;
}

The encryptPassword(e) function is set to run whenever a form is submitted to the spreadsheet. Also please be aware that I have modified the contents of the chars array in an attempt to keep my encryption private. This shouldn't make a difference though since the rest of the code stays the same.

How do I fix my script so it actually writes an encrypted password to the spreadsheet rather than an empty string?


Solution

  • You've got two For/In loops:

    for(range in chars) {
      for(value in range) {
    

    A For/In loop is meant to loop through the properties of an object. When you use it to loop through an array, like you are doing, the "property" is the index number of the array.

    So the line:

    for(range in chars) {
    

    Is causing the variable range to be a number value on every loop. On the first loop, the value of range is zero.

    The second loop:

    for(value in range) {
    

    is never looping. There is nothing to loop through. The value of the variable range is just a single number. You can't loop through a single number. If you use the debugger, you can watch what every line is doing, and execute one line of code at a time.

    If you want to get the index position of one of the characters in the password, you could use indexOf(). For example, if the character in the password was the letter "i".

    var indexPosition = chars[2].indexOf(char);
    

    The value of indexPosition would be 9. The third array in the outer array chars has an element "i" in the 9th index position.