【问题标题】:hashing a cell in Google Spreadsheet. I have zero experience in this在 Google 电子表格中散列一个单元格。我在这方面的经验为零
【发布时间】:2021-10-06 14:33:29
【问题描述】:

我已关注a question answered in one of the links in StackOverflow。我已复制此代码并保存。但我真的不知道如何运行这段代码来散列我的单元格,范围从 A2:F2360。

我仍然不知道如何散列它们。

另外,你在下面看到的代码,我尝试点击 RUN 但它一直说:

异常:参数不能为空:值 MD5@Code.gs:2

请问我应该在哪个单元格上按什么键?

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    您正在执行没有任何参数的函数,因此您的第二行错误,因为您没有input

    如果您希望散列位于Sheet1 中的A2:F2360 中的所有非空单元格,您需要首先访问这些单元格,读取它们的值,使用MD5 函数修改它们,然后将其写回再次。请参阅下面的代码。

    脚本:

    // function that will hash the values
    function MD5 (input) {
      var rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, 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;
    }
    
    // main function that will call your MD5 function
    // execute this one instead of MD5
    function main() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      // access the values of the sheet name indicated
      var sheet = ss.getSheetByName('Sheet1');
      // access the values of the range indicated
      var range = sheet.getRange('A2:F2360');
      // get the values in the mentioned range
      var values = range.getValues();
    
      // modify each cell to be hashed using MD5 function above.
      // added a condition where it won't write anything for blank cells as MD5 still hashes blank string
      var output = values.map(row => row.map(cell => cell ? MD5(cell) : cell));
    
      // write the modified values to the same range
      range.setValues(output);
    }
    

    样本数据:

    输出:

    注意:

    • 如果您不熟悉 Apps 脚本,请在列出的参考资料中了解更多信息,但不限于:

    参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      相关资源
      最近更新 更多