infinite loading in google spreadsheet in function script - google-apps-script

i use function script in google spread sheet.
this function is used maybe... 150,000 cell.
my question is... infinite loading.
when i use my custom function in sheet, infinite loading appear
how can i resolve that?
here is my script code :
function s2hex(str1){
var s2hex = 0;
var byte_check = 0;
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("tbl");
var range1 = sheet1.getRange(1, 1, sheet1.getMaxRows(),
sheet1.getMaxColumns());
for(var i = 0; i < str1.length; ++i){
for(var k = 1; k < sheet1.getMaxRows(); ++k){
if(str1[i] == range1.getCell(k, 2).getValue() || str1[i] == " "){
s2hex = s2hex + 1;
byte_check = 1;
break;
}
}
if(byte_check == 0){
s2hex = s2hex + 2;
}
byte_check = 0;
}
return s2hex;
};

getMaxRows() will return all rows in the sheet. If you have content in A1:A10 and there are empty rows from A10 to A100000. It'll return 100,000 instead of 10. Use getLastRow() instead.
If you're only using the second column,Specify 2 as column number and 1 instead of getMaxColumns()
Use getValues() to get a single array instead of making multiple calls to spreadsheet.
Try Modifying from:
var range1 = sheet1.getRange(1, 1, sheet1.getMaxRows(), sheet1.getMaxColumns());
for(var i = 0; i < str1.length; ++i)
{
for(var k = 1; k < sheet1.getMaxRows(); ++k)
{
if(str1[i] == range1.getCell(k, 2).getValue() || str1[i] == " ")
{
s2hex = s2hex + 1;
To
var lr =sheet1.getLastRow(); //One last row call to be used in multiple places
var range = sheet1.getRange(1, 2, lr,1);
var range1 = range.getValues(); //get all values in a single call
for(var i = 0; i < str1.length; ++i)
{
for(var k = 0; k < lr; ++k) //k to 0.Array index start at 0
{
if(str1[i] == range1[k][0] || str1[i] == " ") //Check the 2D value of already retrieved array
{
s2hex = s2hex + 1;

150,000 function calls will slow down any sheet.
That said, your function is in dire need of optimization: you need to remove the need for extensive calls over the Spreadsheet interface. Each such call can take as much as 0.1 second. You can do this simply, by reading the entire range of your values to search with getValues(), and then iterating that in the same manner as you do currently.

Related

Reduce script execution time - Google script

I made a script that works properly (does what I want it to), however, it's painfully slow and at this pace, it will finish in about 20 days. I can't wait for 20 days and I'm not good enough at this to make it faster on my own.
Here's a brief description of the task:
Masterlist - it's a sheet with 23 columns and 29000+ rows.
Seed - it's an empty sheet that I'm to copy the Masterlist to.
Duplicates - it's an empty sheet where I will store any duplicate rows.
The process:
Get the first line from Masterlist. Check if line already in Seed. If line not in Seed, add line. If line already in Seed, add line to Duplicates. Either way, delete the original line from the Masterlist.
The definition of duplicate:
Each line has an emails column. Column can be either a single email address, or multiple email addresses separated by "; ". If an email is found within line in Masterlist and already exists within line in Seed, this whole line is considered a duplicate.
Example:
"aaa#gmail.com" is not a duplicate of "a#gmail.com; aa#gmail.com"
"bbb#gmail.com" is a duplicate of "b#gmail.com; bbb#gmail.com"
Furthermore, if the emails cell is empty in the Masterlist, this is not considered a duplicate.
Here comes my code - it works but is not fast enough.
function getSheet(name){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(name);
return sheet;
}
function getRowByID(sheet, rowID) {
var range = sheet.getRange(rowID, 1, 1, 23);
var value = range.getValues();
return [range, value];
}
//main executes the entire thing
function main(){
var sourceSheet = getSheet('Masterlist');
var targetSheet = getSheet('Seed');
var remainingSheet = getSheet('Duplicates');
var counter = sourceSheet.getLastRow();
var start = new Date();
while(counter >= 2){
var sourceLine = getRowByID(sourceSheet, 2)[1];
var duplicates = checkEmailMatch(sourceLine, targetSheet);
if(duplicates == 0){
targetSheet.appendRow(sourceLine[0]);
sourceSheet.deleteRow(2);
}
else{
remainingSheet.appendRow(sourceLine[0]);
sourceSheet.deleteRow(2);
}
counter--;
}
}
//iterates through existing lines in the Seed sheet (locates the email cell and reads its contents)
function checkEmailMatch(row, seed){
var sourceEmail = row[0][7];
var counter = seed.getLastRow();
var result = [];
if(!counter){
return 0;
}
else{
var j = 0;
var i = 2;
for(i; i <= counter; i++){
var seedLine = getRowByID(seed, i)[1];
var seedEmail = seedLine[0][7];
if(!seedEmail){}
else if(compareEmails(seedEmail, sourceEmail) == true) {
result[j] = i;
j++;
}
}
return result;
}
}
//Compares each email in Masterlist ("; " separated) with each email in Source ("; " separated)
function compareEmails(emailSeedCell, emailSourceCell){
var seedEmails = emailSeedCell.split("; ");
var sourceEmails = emailSourceCell.split("; ");
for(var i = 0; i < seedEmails.length; i++){
for(var j = 0; j < sourceEmails.length; j++){
if(seedEmails[i] == sourceEmails[j]) return true;
}
}
return false;
}
Please help me - if you need any additional info, I'd be happy to provide! Please note that this is my third script ever, so any feedback is welcome!
Thanks to everyone who chipped in to help, I managed to come up with this code that reduced the execution time more than 10000 times! Thanks, everyone - here's the code:
function sheetToArray(name){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(name);
var counter = sheet.getLastRow();
var columns = sheet.getLastColumn();
var array = sheet.getRange(2, 1, counter, columns).getValues();
return array;
}
function compareEmails(emailSeedCell, emailSourceCell){
var seedEmails = emailSeedCell.split("; ");
var sourceEmails = emailSourceCell.split("; ");
var result = false;
for(var i = 0; i < seedEmails.length; i++){
for(var j = 0; j < sourceEmails.length; j++){
if(seedEmails[i] == sourceEmails[j]) result = true;
}
}
return result;
}
function save2DArrayToSpreadsheet(name, array){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(name);
sheet.getRange(2, 1, array.length, array[0].length).setValues(array);
}
function main(){
var masterArray = sheetToArray('Masterlist');
var seedArray = [];
var duplicateArray = [];
for(var i = 0; i < masterArray.length; i++){
Logger.log(i);
if(!seedArray.length){
seedArray.push(masterArray[i]);
}
else if(!masterArray[i][7]){
seedArray.push(masterArray[i]);
}
else{
var result = false;
for(var j = 0; j < seedArray.length; j++){
if(compareEmails(seedArray[j][7], masterArray[i][7]) == true){
result = true;
}
}
if(result == true){
duplicateArray.push(masterArray[i]);
}
else{
seedArray.push(masterArray[i]);
}
}
}
save2DArrayToSpreadsheet("Seed", seedArray);
save2DArrayToSpreadsheet("Duplicates", duplicateArray);
}

SpreadsheetApp.getActiveSpreadsheet() returning null

I need a quick function to loop through the rows of one of my sheets so I constructed this:
function getTotalsOf(name, type) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(name);
var data = sheet.getDataRange().getValues();
var total = 0.00;
for (var i = 0; i < data.length; i++) {
if (data[i][0]== "") break;
if (data[i][2] == type) {
var temp = (Number(data[i][4]) + Number(data[i][5]) + Number(data[i][6])) * data [i][3];
total = Number(total) + total;
}
}
return total;
}
SpreadsheetApp.getActiveSpreadsheet() seems to return null when I try to execute the function. It also returns null when I try to get the spreadsheet by id. Am I missing something?
EDIT:
I retried using my spreadsheet id (https://docs.google.com/spreadsheets/d/STRINGFROMHERE/edit#gid=1235572150) with the same results.
function getTotalsOf(name, type) {
var sheet = SpreadsheetApp.openById("**STRINGFROMURL**").getSheetByName(name);
var data = sheet.getDataRange().getValues();
var total = 0.00;
for (var i = 0; i < data.length; i++) {
if (data[i][0]== "") break;
if (data[i][2] == type) {
var temp = (Number(data[i][4]) + Number(data[i][5]) + Number(data[i][6])) * data [i][3];
total = Number(total) + temp;
}
}
return total;
}
I think you just missed a few concepts with regard to looping through spreadsheets. Hope this demo will help and get you what you want. Always remember that rows and cols in spreadsheets starts with 1,1 not 0,0.
function loopThroughCells() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues();
Logger.log(data[0].length);
for(var i=0; i< data.length; i++){
for(var j=0; j<data[i].length; j++){
if(data[i][j] == "HELIOS"){
if (data[i][j]== "") break;
Logger.log("HELIOS data found in range "+ (i+1) + " "+(j+1));
}
}
}
Log
[17-07-07 21:45:17:157 HKT] HELIOS data found in range 3 3

Setting the setActiveSelection where there is zero

I have a tremendous volume of sparse matrix data, where i want to programmatically set as active cell wherever there is (0) when I run the below snippet (function).
Which would ease me for manipulation of code labels
The data is purely random. The process has to begin from the current active cell and it has to loop again from beginning if end of the data is found.
I get unexpected results,
function getzero() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var Values = range.getValues();
for (var i = 0; i < Values.length; i++) {
for (var j = 0; j < Values[i].length; j++) {
if (Values[i][j] == 0) {
sheet.setActiveSelection(sheet.getRange(i + 1, j + 1));
break;
}
}
}
}
This should take care of finding all the zeros. Also added a menu for you to run the command from the sheets directly. Just paste code in the script editor and reload the sheet.
// This function addes a menu Zero and submenu getzero to access your function directly from spreadsheet
function onOpen(){
var ss = SpreadsheetApp.getActive()
var menu = [{name:"Find Zero",functionName: "getzero"}]
ss.addMenu("Zeroes", menu)
}
function getzero() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var Values = range.getValues();
var selectedRange = sheet.getActiveRange()
// find active cells row and column
var startRow = selectedRange.getRow() -1
var startCol = selectedRange.getColumn()
var notFoundZero = true
//Use the active cells row and column to start the loop
for (var i = startRow; i < Values.length; i++) {
if (i == startRow){
var j = startCol
}
else {
j =0
}
for (j ; j < Values[i].length; j++) {
// Using "===" makes sure the type is also match else blank is considered as zero too.
if (Values[i][j] === 0) {
Logger.log("Values Row X Col:" + i + " X " + j)
//The below line works as well as sheet.setActiveSelection
sheet.getRange(i + 1, j + 1).activate()
//Below code escapes the outer loop
i = Values.length;
// this boolean is used to runs or stops the next loop
notFoundZero = false;
// breaks inner loop
break;
}
}
}
if(notFoundZero){
for (var i = 0; i <= startRow; i++) {
if (i == startRow){
var runTill = startCol
}
else {
runTill = Values[i].length
}
for (var j=0 ; j < runTill; j++) {
if (Values[i][j] === 0) {
sheet.getRange(i + 1, j + 1).activate()
// same as above
i = Values.length;
//Used to alert if no more zeros found
notFoundZero = false;
break;
}
}
}
}
if(notFoundZero)
{
var ui = SpreadsheetApp.getUi()
ui.alert("No More zero Found")
}
}
This will check cell values after selection if no zeroes are found it will check above the selection. But after that it will stop at the selection and alert no zeroes found, this prevents a runaway loop.
Give it a go and let me know how it goes.
Edit: Below Code for searching in reverse
Below Code Has not been Tested, Might have errors. This was not the primary Question, hence did not check for errors.
function getzeroRev() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var Values = range.getValues();
var selectedRange = sheet.getActiveRange()
// find active cells row and column
var startRow = selectedRange.getRow() -1
var startCol = selectedRange.getColumn()
var notFoundZero = true
//Use the active cells row and column to start the loop
for (var i = startRow; i >=0; i--) {
if (i == startRow){
var j = startCol
}
else {
j =values[i].length
}
for (j ; j >=0; j--) {
// Using "===" makes sure the type is also match else blank is considered as zero too.
if (Values[i][j] === 0) {
Logger.log("Values Row X Col:" + i + " X " + j)
//The below line works as well as sheet.setActiveSelection
sheet.getRange(i + 1, j + 1).activate()
//Below code escapes the outer loop
i = Values.length;
// this boolean is used to runs or stops the next loop
notFoundZero = false;
// breaks inner loop
break;
}
}
}
if(notFoundZero){
for (var i = values.length; i >= startRow; i--) {
if (i == startRow){
var runTill = startCol
}
else {
runTill = 0
}
for (var j=0 ; j >= runTill; j--) {
if (Values[i][j] === 0) {
sheet.getRange(i + 1, j + 1).activate()
// same as above
i = Values.length;
//Used to alert if no more zeros found
notFoundZero = false;
break;
}
}
}
}
if(notFoundZero)
{
var ui = SpreadsheetApp.getUi()
ui.alert("No More zero Found")
}
}

Optimizing google apps script for replacing/appending values

I have a function that loops through array C:D to find a match in A:B, if there is it replaces the value in B with D and if there's no match it appends C:D to A:B. This function is using loops. I know there's a way to optimize this, but I'm lost. How else can this script run without loops?
function moveValues() {
var ss = SpreadsheetApp.openById('open_id');
var source = ss.getRange('sheet2!D:C');
var destination = ss.getRange('sheet2!A:B');
var destCount = 0;
for (var j = 1; j <= destination.getLastRow(); j++) {
if (destination.getCell(j,1).getValue() == "") {
destCount = j;
break;
}
}
for (var i = 1; i <= source.getLastRow(); i++) {
Logger.log(source.getLastRow());
var added = false;
var targetName = source.getCell(i,1).getValue();
var copyValue = source.getCell(i,2).getValue();
if (targetName == "") {
break;
}
for (var j = 1; j <= destCount; j++) {
var curName = destination.getCell(j,1).getValue();
if (copyValue != "" && targetName == curName) {
destination.getCell(j, 2).setValue(copyValue);
added = true;
break;
}
}
if (!added) {
destination.getCell(destCount, 1).setValue(targetName);
destination.getCell(destCount, 2).setValue(copyValue);
destCount += 1;
}
}
source.clear();
};
You will still need to use loop(s), but the code can be optimized. Use getValues() at the beginning. That returns a 2D array. You can use .indexOf() to determine whether there is a match in the other array.
function moveValues() {
var i,L,sh,ss,srcRng,destRng,srcData,targetData,v;
ss = SpreadsheetApp.openById('open_id');
sh = ss.getSheetByName('sheet2');//Get sheet2
lastRow = sh.getLastRow();//Get the row number of the last row
srcRng = sh.getRange(1,1,lastRow);//Get the range for all the values in column 1
destRng = sh.getRange(3,1,lastRow);//Get the range for all the values in column 3
srcData = srcRng.getValues();//Get a 2D array of values
targetData = destRng.getValues();//Get a 2D array of values
srcData = srcData.toString().split(",");//Convert 2D to 1D array
targetData = targetData.toString().split(",");//Convert 2D to 1D array
L = srcData.length;
for (i=0;i<L;i++) {//Loop the length of the source data
v = srcData[i];//Get this value in the array
if (targetData.indexOf(v) !== -1) {//This value was found in target array
}
}
This is not a complete answer to your question, but hopefully it will give you some ideas.
In this example the code is getting just the columns of data to compare, and not the columns of data to change.

Apps Script: Construct range of rows from array of row numbers

I have a list of row numbers in a spreadsheet which I need to change the background colour of. As the spreadsheet is quite large (10+ sheets, each with almost 5000 rows), I am trying to construct a range so I can batch set the background, as doing each row individually was taking over the max time of 6 minutes.
Here's the code I have:
// highlight required rows
var first = -1, last = -1;
for(var j = 0; j < rowNumsToHighlight.length; j++) {
if(first == -1) {
first = rowNumsToHighlight[j];
continue;
}
// if the current row number is one more than the previous, update last to be the current row number
if(rowNumsToHighlight[j] - 1 == rowNumsToHighlight[j - 1]) {
last = rowNumsToHighlight[j];
continue;
}
// otherwise the last row should be the previous one
else {
last = rowNumsToHighlight[j - 1];
}
var numRows = (last - first) + 1;
var range = sheet.getRange(first, 1, numRows, 4);
if(range.getBackground().toUpperCase() != highlightColour.toUpperCase()) {
range.setBackground(highlightColour);
}
first = -1;
last = -1;
}
rowNumsToHighlight is just an array that looks like: [205,270,271,272,278,279]. So, with that as an example, setBackground should be ran on row 205, on rows 270-272, and on 278-279.
I'm fairly sure the solution is simple, but just can't see it. Thanks for any help.
==== Updated Code ====
Based on Serge's code below, I made it more efficient again by reducing the number of getRange() calls made. Time is down from 78 to 54 seconds.
function updateColours(sheet, array, colour){
var columns = sheet.getLastColumn();
var rows = sheet.getLastRow();
var range = sheet.getRange(1, 1, rows, columns);
Logger.log("Resetting highlight on all rows...");
range.setBackground(null);
var backgrounds = range.getBackgrounds();
for(var n = 0; n < backgrounds.length; n++){
var rowIdx = n + 1;
if(array.indexOf(rowIdx) > -1){
for(var c = 0; c < columns; c++){
backgrounds[n][c] = colour;
}
}
}
Logger.log("Highlighting non-translated rows...");
range.setBackgrounds(backgrounds);
}
Maybe this one is faster(?) and built in a way that will make your work easier (function with arguments).
It writes only once to the sheet (or 2 if you clear colors before writing)...
use like below :
function testBG(){
updateColors(0,[7,8,9,18,19,23]);
}
function updateColors(sheetNum,array){
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheets()[sheetNum];
var columns = sh.getMaxColumns();
var range = sh.getRange(1,1,sh.getMaxRows(),columns);
sh.getRange(1,1,sh.getMaxRows(),columns).setBackground(null);// use this if you want to clear all colors before setting them
var backGrounds = range.getBackgrounds();// get all cells BG
for(var n=0;n<backGrounds.length;n++){
var rowIdx = n+1;
if(array.indexOf(rowIdx)>-1){
for(c=0;c<columns;c++){
backGrounds[n][c]="#F00";// if row number is in the array fill in red
}
}
}
sh.getRange(1,1,sh.getMaxRows(),columns).setBackgrounds(backGrounds);//update sheet in one call
}
test sheet in view only, make a copy to test.
This is how I would do it:
function createRanges() {
var rowNumsToHighlight = [5,7,8,9,18,19];
var arrayLength = rowNumsToHighlight.length;
var loopCounter = 0, thisNumberInArray=0, nextNumberInArray=0, crrentNmbrPlusOne=0;
var currentRangeBegin=0, numberOfRowsInRange=1;
currentRangeBegin = rowNumsToHighlight[0];
for(loopCounter=0; loopCounter < arrayLength; loopCounter+=1) {
thisNumberInArray = rowNumsToHighlight[loopCounter];
nextNumberInArray = rowNumsToHighlight[loopCounter+1];
crrentNmbrPlusOne = thisNumberInArray+1;
if (nextNumberInArray===undefined) {
workOnTheRange(currentRangeBegin, numberOfRowsInRange);
return;
};
if (nextNumberInArray!==crrentNmbrPlusOne) {
workOnTheRange(currentRangeBegin, numberOfRowsInRange);
numberOfRowsInRange = 1; //Reset to 1
currentRangeBegin = nextNumberInArray;
} else {
numberOfRowsInRange+=1;
};
};
};
function workOnTheRange(first,numRows) {
var range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet11').getRange(first, 1, numRows, 4);
range.setBackground("red");
};
I've tested the code and it works.