My Concatenation function is running excruciatingly slow, on only 28 rows of data it takes almost 4 minutes to run and I have other code I need to run so I hit the Max execution time in Google sheets
If I was running a similar process in Excel this would take maybe 15-30 sec
Everything is in memory, I can't see (with my limited knowledge of javascript) why my code is so slow
Thanks
A Google sheet with data
//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;
}
You want to reduce the process cost of your script.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Modification point:
In your script, HTN() is used as the for loops. And the function includes SpreadsheetApp.getActiveSpreadsheet().getSheetByName(shtName).getDataRange().getValues().shift();. In your script, the Spreadsheet and sheetName are not changed. So headers is always same. I thought that this might be the main modification point.
Modified script:
Please modify your script as follows.
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(); // It seems that this is not used.
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] = '';
}
}
var h = s.getRange(1, 1, 1, s.getLastColumn()).getValues()[0]; // Added
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 = h.indexOf(arrHeaders[col]) + 1; // Modified
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);
};
Note:
In my environment, the process time of the modified script was about 5 seconds.
If this was not the direct solution, I apologize.
Related
I just registered in HackerEarth and trying to solve the first basic problem: Monk and rotation. When I am running the code by entering single input it works fine but when I submit the solution it does not work.
https://www.hackerearth.com/practice/codemonk/
It seems I am reading the input incorrectly
Can someone please help.
process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";
process.stdin.on("data", function (input) {
stdin_input += input; // Reading input from STDIN
});
process.stdin.on("end", function () {
let lines = stdin_input.split('\n');
let len = lines.length;
let inputArr = [];
const numberTestCase = lines[0]
const output = new Array()
for (i = 1; i < lines.length; i++) {
let lineInput = lines[i].split(' ');
let noOfElement = 0;
let stepRotation = 0;
let skipLineUpto = 0;
let inputData = false;
if (lineInput.length === 2) {
inputData = true;
noOfElement = lineInput[0]
stepRotation = lineInput[1]
skipLineUpto = parseInt(i) + 2;
}
if (inputData) {
let stringOfArray = lines[i + 1];
let arrayData = stringOfArray.split(' ');
let mod = 0
mod = stepRotation % noOfElement;
if (mod != 0) {
let unReversedArray = arrayData.splice(-mod);
let ff = unReversedArray.concat(arrayData)
inputArr.push(ff.join(' '))
} else {
let ff = arrayData
console.log(ff.join(' '))
inputArr.push(ff.join(' '))
}
}
}
main(inputArr)
});
function main(input) {
process.stdout.write(input.join("\n")); // Writing output to STDOUT
}
When you submit you can see why your code is not valid.
Your solution is for when scenarios where input is 3 lines (1 test case), like in their example, but you can see their test cases where they have inputs of multiple lines (T test cases).
Without any prototype method something like this would work (not working because time exceeded):
let lines = stdin_input.split('\n');
let loops = lines[0];
for(i = 1; i < lines.length; i+=2) {
let steps = lines[i].split(' ')[1];
let arr = lines[i+1].split(' ');
for (j = 0; j < steps; j++) {
var x = arr[arr.length-1], i;
for (k = arr.length-1; k > 0; k--) {
arr[k] = arr[k-1];
}
arr[0] = x;
}
main(arr);
}
With pop() and unshift() only one test case is failing due to time exceeded but should get you close to the final solution:
let lines = stdin_input.split('\n');
let loops = lines[0];
for(i = 1; i < lines.length; i+=2) {
let steps = lines[i].split(' ')[1];
let arr = lines[i+1].split(' ');
for (j = 0; j < steps; j++) {
arr.unshift(arr.pop());
}
main(arr);
}
I have 4 sheets in my GoogleSheets spreadsheet.
I need the code below to run on every sheet EXCEPT the one called "MRS summary".
I can't seem to get that 'for' loop (the one with the 'g' variable) to work though, because the code only runs for the ACTIVE sheet. But I do know that the Logger.log(g) line is picking up the right indexes...so I think the 'for g' loop is working in the sense that it knows what sheets to look at, but the rest of the code only does what it's supposed to do on the ACTIVE sheet....but I need it to run on every sheet except the one called "MRS summary". Any thoughts?
function SearchCols()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
skip = ['MRS summary'];
for (var g = 0, len3 = sheets.length; g < len3; g++)
{
var name = sheets[g].getName()
if (skip.indexOf(name) > -1) continue;
{
var expen = ss.getSheetByName("MRS summary")
var sheet1 = expen.getRange("A13:A210").getValues();
var searchCol1 = expen.getRange ('B13:B210').getValues();
var searchCol2 = expen.getRange ('A13:A210').getValues();
var searchCol3 = expen.getRange('C13:C210').getValues();
var searchCol4 = expen.getRange('D13:D210').getValues();
var searchCol = ss.getRange('A13:A210').getValues();
var searchVal = ss.getRange('B4').getValue();
for (var i = 0, len = searchCol2.length; i < len; i++)
for (var j = 0, len2 = searchCol1.length; j < len2; j++)
{
if (searchCol2[j][0] == searchVal && searchCol1[j][0] ==
searchCol[i][0])
{
col1[g].getRange(i + 13, 4).setValue(searchCol3[j][0])
col1[g].getRange(i + 13, 5).setValue(searchCol4[j][0])
}
}
}
Logger.log(g);
}
}
I have some small issue in j query. my code is like this
var tt = 8*1;
var n = tt.toString();
var uu1 = n.split('*');
var count = uu1.length;
var table_id44 = '';
for(k = 0,m = 1; k < count-1; k++,m++)
{
var table_id44[m] = uu1[k];
}
when I put that i got error in console log SyntaxError: missing ; before statement.please someone help.
table_id44 should be declared as an array. and you don't need to redeclare the variable in the for loop.
var tt = 8 * 1;
var n = tt.toString();
var uu1 = n.split('*');
var count = uu1.length;
var table_id44 = [];
for (k = 0, m = 1; k < count - 1; k++, m++) {
table_id44[m] = uu1[k];
}
Try this, You are declaring it as string var table_id44 = ''; and using it as an array.
var tt = "8 * 1";//use it as string so that you can further split it on *
var n = tt.toString();
var uu1 = n.split('*');
var table_id44 = [];
for (k = 0, m = 1; k < count - 1; k++, m++)
{
table_id44[m] = uu1[k];//prevent multiple declaration in loop
}
Kindly help me investigate my function below since I'm stuck and still having a hard time figuring it out.
All is well until it reaches the last column on the nested FOR loop. The last column of each row's values are only "0". However, I used the Number() function to make the cell values(i.e. "0") a number but I keep on getting NaN for the last element of the SUM & COUNT arrays.
colCount = 326 while rowCount = 374.
sum.length and count.length should really be ONLY 325 since the headers are unnecessary and the first column is just composed of time stamps. I was able to .push(0) successfully until the nested FOR loop changed the result of the last element to NaN.
function processDataToDictionary(csv) {
var allTextLines = csv.split(/\r\n|\n/);
var csvArray = [];
for (let i = 0; i < allTextLines.length - 1; i++) {
var row = allTextLines[i].split(',');
csvArray.push(row);
}
var colCount = csvArray[0].length;
var rowCount = csvArray.length;
//Arrays of values
var count = [];
var sum = [];
var average = [];
var headers = [];
for (let i = 1; i < colCount; i++) {
var current = csvArray[0][i].replace(/"/g, '');
sum.push(0);
count.push(0);
headers[i] = current;
}
for (let i = 1; i < rowCount; i++) {
for (let j = 1; j < colCount; j++) {
// Remove the quotes from your array
current = csvArray[i][j].replace(/"/g, '');
// Added the Method IsNullOrWhiteSpace
if (!isNullOrWhitespace(current)) {
// Parse as double not int to account for dec. values
sum[j] += Number(current);
count[j]++;
}
}
}
for (let i = 0; i < colCount; i++) {
average.push((sum[i] + 0.0) / count[i]);
}
for (let i = 1; i < colCount; i++) {
// create an empty array
dictionary[headers[i]] = average[i];
}
return dictionary;
}
function isNullOrWhitespace(input) {
if (input == " ") {
return true;
} else {
return false;
}
}
This gives you a dictionary (Object) with the columns names as keys and numbers that appear to be the correct averages as values. But one must still check whether there is a fault in the logic somewhere and the averages are not correct in fact.
function processDataToDictionary(csv) {
function isNullOrWhitespace(input) {
if (input === " ") {
return true;
} else if (input === null) {
return true;
//} else if (input === undefined) {
//return true;
} else {
return false;
}
}
var allTextLines = csv.split(/\r\n|\n/);
var csvArray = [];
for (let i = 0; i < allTextLines.length - 1; i++) {
var row = allTextLines[i].split(',');
csvArray.push(row);
}
var colCount = csvArray[0].length;
var rowCount = csvArray.length;
//Arrays of values
var count = [];
var sum = [];
var average = [];
var headers = [];
for (let i = 1; i < colCount; i++) {
var current = csvArray[0][i].replace(/"/g, '');
sum.push(0);
count.push(0);
headers[i] = current;
}
/**** I added these two lines ****/
sum.push(0);
count.push(0);
for (let i = 1; i < rowCount; i++) {
for (let j = 1; j < colCount ; j++) {
// Remove the quotes from your array
current = csvArray[i][j].replace(/"/g, '');
// Added the Method IsNullOrWhiteSpace
if (!isNullOrWhitespace(current)) {
// Parse as double not int to account for dec. values
sum[j] += Number(current);
count[j]++;
}
}
}
for (let i = 0; i < colCount; i++) {
average.push((sum[i] + 0.0) / count[i]);
}
// I added this line:
dictionary = {};
for (let i = 1; i < colCount; i++) {
dictionary[headers[i]] = average[i];
}
return dictionary;
}
Let me know if this works out for you. You can loop through the values with: for (let key in dictionary) {console.log("key: " + key + " , value: " + dictionary[key]);} . Regards!
I'm trying to get the following code to add each number in the element separately and not the whole array together but the dash seems to stop the loop from calculating the total sum of each element. I can't seem to make it so it'll except any length of number for the variable. Any help is greatly appreciated!
var creditNum = [];
creditNum[0] = ('4916-2600-1804-0530');
creditNum[1] = ('4779-252888-3972');
creditNum[2] = ('4252-278893-7978');
creditNum[3] = ('4556-4242-9283-2260');
var allNum = [];
var total = 0;
var num = 0;
var cnt = 0;
for (var i = 0; i < creditNum.length; i++) {
num = creditNum[i];
for (var j = 1; j <= num.length; j++) {
var num = creditNum[i].substring(cnt, j);
console.log(creditNum[i].charAt(cnt));
console.log(cnt, j);
cnt = cnt + 1;
}
if (num != "-") j = j++;
console.log(parseInt(num));
}
console.log(total);
Assuming the intent is to add '4916-2600-1804-0530' and output the value as 49, then the following modification will achieve that.
var creditNum = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978','4556-4242-9283-2260'];
for (var i = 0; i < creditNum.length; i++) {
var num = creditNum[i].replace(/\-/g, '');
var total = 0;
for (var j = 0; j < num.length; j++) {
total += Number(num[j]);
}
console.log(creditNum[i], total);
}
Using native array methods, the code can be refactored as the following.
var creditNumbers = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978','4556-4242-9283-2260'];
creditNumbers.forEach(function(creditNumber) {
var num = creditNumber.replace(/\-/g, '').split('');
var total = num.reduce(function(tally, val) {
return tally += Number(val);
}, 0);
console.log(creditNumber, total);
});