【问题标题】:hashing a cell in Google Spreadsheet. I have zero experience in this在 Google 电子表格中散列一个单元格。我在这方面的经验为零
【发布时间】:2021-10-06 14:33:29
【问题描述】:
【问题讨论】:
标签:
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 脚本,请在列出的参考资料中了解更多信息,但不限于:
参考资料: