繁体   English   中英

Google Apps 脚本 - 在数组公式中将单元格转换为 SHA256

[英]Google Apps Script - Convert cell to SHA256 within an Array Formula

我有一个简单的脚本来散列一个单元格,但是它在数组公式中不起作用,我很难弄清楚如何添加该功能。

 function SHA256 (input) { var rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, input); 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; }

在 Google Sheets 中,上面的脚本允许我使用 SHA526(A2) 进行散列

我希望能够通过在数组公式中使用 SHA256() 来散列整个列。 =ArrayFormula(SHA256(A2:A))

我得到的错误是

“例外:参数 (DigestAlgorithm,number[]) 与 Utilities.computeDigest 的方法签名不匹配。(第 2 行)。”

任何方向将不胜感激!

Google Apps 脚本 - 自定义函数文档

为了使用数组,您需要映射输入。 如果可以测试输入是数组还是单个值,则使用简单的 else。

 function SHA256 (input) { if(input.map) { return input.map(SHA256); } else { var rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, input); 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; } }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM