簡體   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