问题
I implemented an MD5 formula as mentioned here: Hash of a cell text in Google Spreadsheet.
function MD5 (input) {
var rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, input);
Utilities.sleep(100)
var txtHash = '';
for (i = 0; i < rawHash.length; i++) {
var hashVal = rawHash[i];
if (hashVal < 0) {
hashVal += 256;
}
if (hashVal.toString(16).length == 1) {
txtHash += '0';
}
txtHash += hashVal.toString(16);
}
return txtHash;
}
Now I want to run this with an ARRAYFORMULA
but I can't get it working. I tried this:
=ARRAYFORMULA(({"FiBu MD5";IF(ISBLANK(AG2:AG),"",(MD5(O2:O)))}))
The error I'm getting is:
"Cannot convert Array to (class)[]. (line 2)."
Does anyone have an idea how to solve this?
回答1:
How about this modification?
For example, when the current script of your question is used, in the case that MD5(O2:O)
is used as a custom function, input
of function MD5(input) {}
is 2 dimentional array like [[value of O2], [value of O3],,,]
. But in your script, the result is returned by inputting a value which is not an array. By this, the error shown in your question occurs. So in order to input and output the array, the script is required to be modified for this situation as follows.
Modified script:
function MD5(input) {
for (var i = input.length - 1; i >= 0; i--) {
if (input[i][0]) {
input.splice(i + 1);
break;
}
}
var result = input.map(function(e) {
if (e[0] != "") {
var rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, e[0]);
// Utilities.sleep(100) // I thought that this might be not required.
var txtHash = '';
for (i = 0; i < rawHash.length; i++) {
var hashVal = rawHash[i];
if (hashVal < 0) {
hashVal += 256;
}
if (hashVal.toString(16).length == 1) {
txtHash += '0';
}
txtHash += hashVal.toString(16);
}
return [txtHash];
}
return [];
});
return result;
}
Note:
- I think that by above modification, you can also use the formula of
={"FiBu MD5";MD5(O2:O)}
instead of=ARRAYFORMULA(({"FiBu MD5";IF(ISBLANK(AG2:AG),"",(MD5(O2:O)))}))
.
If this didn't work, I apologize. At that time, in order to correctly understand your situation, can you provide a sample Spreadsheet? By this, I would like to modify the script.
来源:https://stackoverflow.com/questions/55059334/use-custom-md5-formula-with-arrayformula