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:
我有一个Google表单,可以提交到Google电子表格。在电子表格方面,我有一个Google Apps脚本,可以加密提交的密码。但无论出于何种原因,它会将空字符串写入加密密码所在的位置。这真的开始强调我了。这是我的代码:
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.
只要表单提交到电子表格,encryptPassword(e)函数就会设置为运行。另请注意,我已修改了chars数组的内容,试图将加密保密。这应该没有区别,因为其余的代码保持不变。
How do I fix my script so it actually writes an encrypted password to the spreadsheet rather than an empty string?
如何修复我的脚本,以便它实际上将加密的密码写入电子表格而不是空字符串?
1 个解决方案
#1
You've got two For/In loops:
你有两个For / In循环:
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.
For / In循环用于循环访问对象的属性。当您使用它循环遍历数组时,就像您正在做的那样,“属性”是数组的索引号。
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".
如果要获取密码中某个字符的索引位置,可以使用indexOf()。例如,如果密码中的字符是字母“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.
indexPosition的值为9.外部数组chars中的第三个数组在第9个索引位置有一个元素“i”。
#1
You've got two For/In loops:
你有两个For / In循环:
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.
For / In循环用于循环访问对象的属性。当您使用它循环遍历数组时,就像您正在做的那样,“属性”是数组的索引号。
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".
如果要获取密码中某个字符的索引位置,可以使用indexOf()。例如,如果密码中的字符是字母“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.
indexPosition的值为9.外部数组chars中的第三个数组在第9个索引位置有一个元素“i”。