我的连接函数运行速度极慢,仅 28 行数据就需要将近 4 分钟才能运行,而且我还有其他代码需要运行,所以我在 Google 表格中达到了最大执行时间
如果我在 Excel 中运行类似的过程,这可能需要 15-30 秒
一切都在内存中,我看不出(由于我对 javascript 的了解有限)为什么我的代码这么慢
谢谢
//Sheet Name
//Values in Range to be overwritten by Header names
//List of Header names
//New Header Name 1
//New Header Name 2
function AsAboveSoBelow_Offers_Asks() {
AsAboveSoBelow('Elements',
["I can offer", "I have a ask"],
["Header1","Header2","Header3","Header4","Header5","Header6",
"Header7","Header8","Header9","Header10","Header11",
"Header12","Header13","Header14"],
"Offers",
"Asks");
}
function AsAboveSoBelow(shtName, arrPBV, arrHeaders, newHeader1, newHeader2) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName(shtName);
var LC = s.getLastColumn();
var r = s.getDataRange();
var v = r.getValues();
var start = new Date();
var temp ="";
//Dim A
var A = [];
for (var i = 0; i < v.length; i++) {
A[i] = [];
for (var j = 0; j <= arrPBV.length - 1; j++) {
A[i][j] = '';
}
}
for(var e = 0; e <= arrPBV.length - 1; e++) {
var search_Term = arrPBV[e];
for(var row = 0; row < v.length; row++) {
for(var col = 0; col <= arrHeaders.length - 1; col++) {
var col2 = HTN(shtName,arrHeaders[col])
var replace_Term = arrHeaders[col];
if(v[row][col2-1].toString().indexOf(search_Term) > -1) {
temp = temp + replace_Term + "|"
}
}
//remove trailing pipe
A[row][e] = temp.replace(/\|(?=\s*$)/, '')
temp = ""
}
}
A[0][0]= newHeader1
A[0][1]= newHeader2
s.getRange(1, v[0].length +1, v.length,A[0].length).setValues(A);
var end = new Date();
var executiontime = end - start;
Logger.log(executiontime);
};
//Helper function
function HTN(shtName,cheader){
var headers =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(shtName).getDataRange().getValues().shift();
var colindex = headers.indexOf(cheader);
return colindex+1;
}
子衿沉夜
相关分类