我剛剛開始使用 Google Appscript,在這里我正在嘗試創建一個腳本來幫助我解碼 Cloudflare 電子郵件。
我有以下 Javascript 代碼,我已將其與其他代碼融合以從 A 列獲取值,并在轉換后將它們放置在 B 列中。
我的代碼拋出錯誤TypeError: sheetS.getRange(...).cfDecodeEmail is not a function
我有的:
function Decode() {
var sss = SpreadsheetApp.openById("1fDXv1L1YmXzbUXJbzGE6suc5HWToHlUuO-zBzVZDcX0");
var sheetS = sss.getSheetByName("Guide");
var AG1val = sheetS.getRange('A1:A').getValues(); //
function cfDecodeEmail(encodedString) {
var email = "", r = parseInt(encodedString.substr(0, 2), 16), n, i;
for (n = 2; encodedString.length - n; n = 2){
i = parseInt(encodedString.substr(n, 2), 16) ^ r;
email = String.fromCharCode(i);
}
return email;
}
sheetS.getRange("B1:B").cfDecodeEmail(AG1val); // decode to B1:B
}
這是“未觸及”的 Javascript 代碼,它故意做我想做的事情,除了我試圖在 Appscript 中做它。
function cfDecodeEmail(encodedString) {
var email = "", r = parseInt(encodedString.substr(0, 2), 16), n, i;
for (n = 2; encodedString.length - n; n = 2){
i = parseInt(encodedString.substr(n, 2), 16) ^ r;
email = String.fromCharCode(i);
}
return email;
}
console.log(cfDecodeEmail("543931142127353935313e352e7a373b39")); // usage
謝謝。
uj5u.com熱心網友回復:
AG1val 是一個二維陣列(參見:https ://developers.google.com/apps-script/reference/spreadsheet/range#getvalues ),您似乎正試圖將其作為字串傳遞給函式。
您應該首先遍歷該陣列,將該陣列的每個元素傳遞給 cfDecodeEMail 函式,然后將這些值寫入 B1:B 范圍。
類似于下面的東西,做了一個快速測驗,這似乎作業:
function decode() {
var sss = SpreadsheetApp.openById("1fDXv1L1YmXzbUXJbzGE6suc5HWToHlUuO-zBzVZDcX0");
var sheetS = sss.getSheetByName("Guide");
var AG1val = sheetS.getRange('A1:A').getValues(); //
function cfDecodeEmail(encodedString) {
var email = "", r = parseInt(encodedString.substr(0, 2), 16), n, i;
for (n = 2; encodedString.length - n; n = 2){
i = parseInt(encodedString.substr(n, 2), 16) ^ r;
email = String.fromCharCode(i);
}
return email;
}
let results = AG1val.map( (row) =>{ //loop through each row
let cellValue = row[0]; //since there is only one column, get it by the first index (0)
if(cellValue !== ''){ //pass the value to the function if cell contains a value
return [cfDecodeEmail(cellValue)]; //return it in an array to contain the two-dimensional array structure
} else{
return ['']; //otherwise return an empty string
}
})
sheetS.getRange("B1:B").setValues(results) //call the setValues function to write back to the sheet
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/530350.html
