【问题标题】:Google Apps Script - Convert cell to SHA256 within an Array FormulaGoogle Apps 脚本 - 在数组公式中将单元格转换为 SHA256
【发布时间】:2021-01-17 10:25:48
【问题描述】:

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

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 表格中,上述脚本允许我使用 SHA526(A2) 进行哈希处理

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

我得到的错误是

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

任何方向将不胜感激!

【问题讨论】:

  • 您是否阅读过官方自定义函数文档或查看过其他类似问题?
  • 很高兴知道我应该在那里寻找什么。
  • 那里只有一页关于自定义功能的页面。搜索。完整阅读该页面。

标签: javascript google-apps-script hash sha256 custom-function


【解决方案1】:

Google Apps Script - Custom Functions Documentation

为了使用数组,您需要映射输入。如果可以测试输入是数组还是单个值,请使用简单的 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;
  }
}

【讨论】:

  • 只有代码的答案对其他人几乎没有用处。考虑editing 来解释为什么以及如何使用官方文档的链接、官方文档的引用等。
猜你喜欢
  • 2016-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-16
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
相关资源
最近更新 更多