Finding matches from 2 ranges and replacing matches with a specific value, any way to optimize? - google-apps-script

This is the sample sheet.
From this
1 Search area Bounty list Bullet
2 a i z a b c abc
3 e b d d e f def
4 y f h g h i ghi
5
6 1 2 3 4 5 6 7 8
7 Column #
To this
1 Search area Bounty list Bullet
2 abc ghi z a b c abc
3 def abc def d e f def
4 y def ghi g h i ghi
5
6 1 2 3 4 5 6 7 8
7 Column #
It will take a value "bounty" from the "bounty list" starting from (2,5) or "a", search around the "Search Area" in a sequence, from a, i, z, e, b, d, y, f, h. Then if it finds a cell or multiple cells that equals the value of "bounty", then it will place the value of "bullet" from column 8 on the current "bounty" row to those cells. The process will repeat in the sequence of a, b, c, d, e, f, g, h, i on the "bounty list". Both process moves to the left, and down.
function menuItem1()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var target = sheet.getDataRange().getValues();
for (var BountyRow = 2; BountyRow<target.length; BountyRow++)//switching rows in bounty list//
{
var bullet = sheet.getRange(BountyRow, 8).getValue(); //cell value to paste on targets//
for (var BountyColumn = 5; BountyColumn<8; BountyColumn++) //switching columns in bounty list//
{
var bounty = sheet.getRange(BountyRow, BountyColumn).getValue(); // cell value to search for//
if (bounty !=0)
{
for (var SearchRow = 1; SearchRow<target.length; SearchRow++) //switching row on search area//
{
for(var SearchColumn = 0; SearchColumn<4;SearchColumn++)//switching column on search area//
{
if(target[SearchRow][SearchColumn] == bounty) //if search target is found//
{
var found = target[SearchRow][SearchColumn];
sheet.getRange(SearchRow+1, SearchColumn+1).setValue(bullet);
Logger.log((found)+ " in "+"row"+(SearchRow+1)+", column"+(SearchColumn+1));
}
}
}
}
}
}
}
It involves thousands of searches which always use more than a minute and I was wondering if there is a more efficient way to do it?

In order to optimize your code you need to do two things:
Instead of using getValue() and setValue() for each cell (which makes you code slow)
retrieve all your bounty list and search are data once, with getValues()
assign the values to an array
replace matches within the the array
set the updated array values back into the range with setValues()
Make use of indexOf() and map()
to find matches and replace them more efficiently
Sample:
function menuItem1(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lastRow=sheet.getLastRow();
var searchValues=sheet.getRange(2,1,lastRow-2+1,3).getValues();
var bountyValues=sheet.getRange(2,5,lastRow-2+1,3).getValues();
var bulletValues=sheet.getRange(2,8,lastRow-2+1,1).getValues();
for (var i = 0; i<bountyValues.length; i++){
for (var j = 0; j<bountyValues[0].length; j++){
if (bountyValues[i][j] !=0){
replaceValues(searchValues, bountyValues[i][j], bulletValues[i][0]);
}
}
}
sheet.getRange(2,1,lastRow-2+1,3).setValues(searchValues)
}
function replaceValues(search, bounty, bullet) {
for(var k=0;k<search.length;k++){
search[k]=search[k].map(function(search) {
var regex=new RegExp("\\b"+bounty+"\\b","g");
return search.toString().replace(regex, bullet);
});
}
}

Related

Google Sheets script - For each used cell in range 1, if cell value exists in range 2, get range 2 match's row number

I feel I have this script almost working as intended, but I am unsure as to where to place certain components of the procedure within the for loop to achieve the desired result. Tunnel vision is very much in effect right now, it's entirely possible I am overthinking a simple task. Please let me know if I can describe the issue more clearly. Any suggestions or pointers towards an existing resource are helpful.
Setup: Sheet one contains a dynamic vertical list of text values starting in cell I3 going down. Sheet two contains a dynamic vertical list of text values starting in range A2 going down, and has a similar set of text values in the same rows in column B.
Goal:
Get the value of each used cell in Sheet1 column I (range one)
Get the value of each used cell in Sheet2 column A (range two)
For each used cell in range one, check the value of each range one
cell to see if the value exists in range two
If a match exists, get the row number of the cell in range two that
contains the value (there will only ever be a single match if a match
exists)
Get the value of the cell on that same row in Sheet2 column B
Set the above value as the cell value in the row containing the value
found in both ranges on Sheet1 Column M
Below is what I have been able to come up with. I am able to get it to function up to the last step. It seems the matching rowNumber variable is not updating with each for loop.
// Get values of range one
var lastRowRangeOne = sheetone.getRange('I3').getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
var rangeOneValues = sheetone.getRange('I3:I' + lastRowClientColumn).getValues();
// Get values of range two
var sheettwo = spreadsheet.getSheetByName('Sheet2');
var sheettwoLastRow = sheettwo.getLastRow();
var sheettwoList = sheettwo.getRange('A2:A' + sheettwoLastRow).getValues();
var sheettwoListFlat = sheettwoList.map(function(row) {return row[0];});
for (var i = 0; i< rangeOneValues.length ; i++){ // for each row in range one
if (sheettwoListFlat[i] == rangeOneValues[i]) { // if the range one value exists in range two
var rowNumber = sheettwoListFlat.indexOf(rangeOneValues[i]) + 3; // get the row number of the matching value found in range two
var sheetOneColumnMValue = sheettwo.getRange('B' + rowNumber).getValue(); // get the cell value of the same row in sheet two column B
var sheetOneRowNumber = i + 3; // get the row number of the range one value
sheetone.getRange('M' + sheetOneRowNumber).setValue(sheetOneColumnMValue); // set the cell value of sheet one column M row x, where x is sheetOneRowNumber
}
}
If you print the value of sheettwoListFlat, it has only 5 elements. Once the iterator of for loop reaches 5 or more it will automatically set if (sheettwoListFlat[i] == rangeOneValues[i]) { to false. Also the statement will only work if the elements of the same index of both arrays are equal.
It would be also more efficient if you search the Sheet1 using the values of Sheet2 since Sheet2 has lesser value.
Here I used TextFinder to find the text within a range.
Using Sheet2 values as search key
Code:
function myFunction() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheetone = spreadsheet.getSheetByName('Sheet1');
var lastRowRangeOne = sheetone.getRange('I2').getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
var rangeOneValues = sheetone.getRange('I2:I' + lastRowRangeOne);
var sheettwo = spreadsheet.getSheetByName('Sheet2');
var sheettwoLastRow = sheettwo.getLastRow();
var sheettwoList = sheettwo.getRange('A2:B' + sheettwoLastRow).getValues();
for (var i = 0; i < sheettwoList.length; i++){
var find = rangeOneValues.createTextFinder(sheettwoList[i][0]).findNext()
if(find){
var sheetOneRowNumber = find.getRow();
sheetone.getRange('M' + sheetOneRowNumber).setValue(sheettwoList[i][1]);
}
}
}
Using Sheet1 values as search key
Code:
function myFunction() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheetone = spreadsheet.getSheetByName('Sheet1');
var lastRowRangeOne = sheetone.getRange('I2').getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
var rangeOneValues = sheetone.getRange('I2:I' + lastRowRangeOne).getValues();
var sheettwo = spreadsheet.getSheetByName('Sheet2');
var sheettwoLastRow = sheettwo.getLastRow();
var sheettwoList = sheettwo.getRange('A2:B' + sheettwoLastRow);
for (var i = 0; i< rangeOneValues.length ; i++){ // for each row in range one
var find = sheettwoList.createTextFinder(rangeOneValues[i][0]).findNext();
if(find){
var rowNumber = find.getRow()
var sheetOneColumnMValue = sheettwo.getRange('B' + rowNumber).getValue(); // get the cell value of the same row in sheet two column B
var sheetOneRowNumber = i + 2;
sheetone.getRange('M' + sheetOneRowNumber).setValue(sheetOneColumnMValue);
}
}
}
Output:
Note: Both produce the same output but using Sheet2 values as search key is more faster than the other.
function compareTwoCols() {
const c1 = 'COL1';//column names
const c2 = 'COL3';
const ss1 = SpreadsheetApp.openById(gobj.globals.datagenid);//data set 1
const sh1 = ss1.getSheetByName('Sheet1');
const [hd1, ...vs1] = sh1.getDataRange().getValues();
let col1 = {};
hd1.forEach((h, i) => { col1[h] = i });
const ds1 = vs1.map((r, i) => {
return r[col1[c1]];
});
const ss2 = SpreadsheetApp.openById(gobj.globals.ssid);//data set 2
const sh2 = ss2.getSheetByName('Sheet0');
const [hd2, ...vs2] = sh2.getDataRange().getValues();
let col2 = {};
hd2.forEach((h, i) => { col2[h] = i });
const ds2 = vs2.map((r, i) => {
return r[col2[c2]]
});
let matches = { pA: [] };
let idx = -1;
ds1.forEach((e, i) => {
let from = '';
do {
idx = ds2.indexOf(e, from);
if (~idx) {
if (!matches.hasOwnProperty(e)) {
matches[e] = [];
matches[e].push({ val1: e, row1: i + 2, col1: col1[c1] + 1, row2: idx + 2, col2: col2[c2] +1 });
matches.pA.push(e);
} else {
matches[e].push({ val1: e, row1: i + 2, col1: col1[c1] + 1, row2: idx + 2, col2: col2[c2] + 1});
}
from = idx + 1;
}
} while (~idx);
});
Logger.log(JSON.stringify(matches));
}
Spreadsheet1 Sheet1:
COL1
COL2
COL3
COL4
COL5
3
4
2
3
4
2
6
6
1
4
5
1
7
5
5
9
8
7
9
5
7
9
0
8
1
8
2
8
7
9
5
8
7
9
9
1
2
0
8
6
2
7
4
0
3
8
2
0
2
6
Spreadsheet2 Sheet0:
COL1
COL2
COL3
COL4
COL5
5
1
2
7
6
4
5
7
8
2
6
3
8
1
5
0
7
6
3
6
4
7
6
1
7
5
6
9
2
1
3
0
2
2
8
4
5
0
8
1
1
3
9
2
2
3
6
7
0
3
Matches Object:
{
"2":[
{
"val1":2,
"row1":3,//ds1 row 3
"col1":1,
"row2":2,//ds2 row 4
"col2":3
},
{
"val1":2,
"row1":3,//ds1 row 3
"col1":1,
"row2":8,//ds2 row 8
"col2":3
},
{
"val1":2,
"row1":10,
"col1":1,
"row2":2,
"col2":3
},
{
"val1":2,
"row1":10,
"col1":1,
"row2":8,
"col2":3
}
],
"7":[
{
"val1":7,
"row1":6,
"col1":1,
"row2":3,
"col2":3
},
{
"val1":7,
"row1":6,
"col1":1,
"row2":11,
"col2":3
}
],
"8":[
{
"val1":8,
"row1":7,
"col1":1,
"row2":4,
"col2":3
},
{
"val1":8,
"row1":11,
"col1":1,
"row2":4,
"col2":3
}
],
"9":[
{
"val1":9,
"row1":5,
"col1":1,
"row2":7,
"col2":3
},
{
"val1":9,
"row1":5,
"col1":1,
"row2":10,
"col2":3
}
],
"pA":[//just an array of all of the matches
2,
9,
7,
8
]
}

Find matches for two columns on two sheets google script

Im am trying to match 2 columns (A:B) in sheet1 with 2 columns in sheet2 (A:B) and if there is a match, copy contents of column C matching row in sheet1 to matching row in sheet2.
I've tried to adapt several scripts without success. The code below comes closest to my requirements, but with my limited knowledge of script I haven't been able to adapt it to my exact needs.
Sheet1
A B C
Week Rotation Working
Week1 11 In
Week1 5 In
Week1 4 In
Week1 3 In
Week1 3 Off
Week1 7 Off
Sheet2
A B C
Week Rotation Working
Week1 6
Week1 5
Week1 4
Week1 3
Week1 3
Week1 11 (In should be copied to here)
My code:
function MatchColumns(){
// gets spreadsheet A and the range of data
var sheetA
=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Testa");
var dataA = sheetA.getRange(2, 1, sheetA.getLastRow(),
2).getValues();
// gets spreadsheet B and the range of data
var sheetB =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test2");
var dataB = sheetB.getRange(2, 1, sheetB.getLastRow(),
1).getValues();
// Added
var res = [];
for (var b in dataB) {
for (var a in dataA) {
if (dataA[a][0] == dataB[b][0]) res.push([dataA[a][3]]);
}
if (b != res.length - 1) res.push([""]);
}
sheetB.getRange(2, 2, res.length, res[0].length).setValues(res);
Note that JavaScript is one of the many languages that use 0-base indexing. So res.push([dataA[a][3]]) is placing the 4th value from the row into the result array, i.e. Column D.
Your dataA and dataB variables don't actually include the column C data, as you initialized them with only 2 columns of data. So dataA[a][2] and dataA[a][3] are both undefined.
You probably don't want to collect these new values into an array via push, as this will lose the correlation between which row you matched in A & B, and which row you write into. To avoid losing existing information in column C, you need to read it from sheet 2 and assign to the specific index:
var destC = sheet2.getRange(2, 3, sheet2.getLastRow() - 1, 1).getValues();
/** Find matched rows */
...
destC[b][0] = dataA[a][2];
...
// Lastly, write values
sheet2.getRange(2, 3, destC.length, 1).setValues(destC);
Try using a nested for to iterate through the Values in Sheet1 and Sheet2 and compare them with
function MatchColumns(){
// gets spreadsheet A and the range of data
var sheetA = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var dataA = sheetA.getRange(2, 1, sheetA.getLastRow() - 1, 2).getValues();
// gets spreadsheet B and the range of data
var sheetB = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
var dataB = sheetB.getRange(2, 1, sheetB.getLastRow() - 1, 2).getValues();
for (var x = 0; x < sheetA.getLastRow() - 1; x++){
for (var y = 0; y < sheetB.getLastRow() - 1; y++){
if (dataA[x][0] == dataB[y][0]){
if (dataA[x][1] == dataB[y][1]){
sheetB.getRange(y + 2, 3).setValue(sheetA.getRange(x + 2, 3).getValue());
}
}
}
}
}
This copies the value of column C in Sheet1 to column C in Sheet2 if the corresponding cells in columns and B match.

Result of JOIN is longer than the limit of 50,000 characters

I am trying to combine text from column A and match it with each possibility of column B.I used the formulas:
in C1:
=transpose(split(join("", arrayformula(rept(filter(A1:A, len(A1:A))&char(9999), counta(B1:B)))), char(9999)))
in D1:
=transpose(split(rept(join(char(9999), filter(B1:B, len(B1:B)))&char(9999), counta(A1:A)), char(9999)))
but when I use it in my list I get these errors in C1 and D1 respectively;
Text result of JOIN is longer than the limit of 50000 characters
Text result of REPT is longer than the limit of 32000 characters
I tested this out with a smaller list of just:
a b c 1 2
and managed to get my list to generate this after combining the two cells:
a 1
a 2
a 3
b 1
b 2
b 3
but the list I am combining has a lot more text in each of the columns.
Any suggestions on how to combine my lists as shown above but with 132 possibilities in column A and 52 possibilities in column B?
Each line has between 70 and 150 characters of text in each row.
Go to menu Tools → Script Editor...
Paste this code:
function crossJoin(arr1, arr2, delim) {
delim = delim || '';
var result = [];
var row = [];
for (var i = 0; i < arr1.length; i++) {
for (var j = 0; j < arr2.length; j++) {
row = [];
row.push('' + arr1[0,i] + delim + arr2[0,j]);
result.push(row);
}
}
return result;
}
Save project.
Use it as regular function in spreadsheet:
=crossJoin(A1:A132,B1:B52)
Optionaly use delimeter:
=crossJoin(A1:A132,B1:B52, "-")

Find cell in another tab

This is a multi-part question: I'm at a loss to explain it succinctly in one clear statement. I'll try to clean it up after getting some feedback:
In Google Docs, I would like to search a different tab (in the same spreadsheet) to find a cell which contains specific data. The data may be moved around, making it impossible to use a static cell reference.
//if 'Sheet2' of my spreadsheet contains the following...
A B C D
1 - - - -
2 - foo bar 6
3 - - - -
//...then some magical function would return C2
=getCell( 'Sheet2', "bar" )
Now that we've got this cell, I want to get the values of the adjacent cells on the same row.
//this would return "foo":
=getLeft( getCell( 'Sheet2', "bar" ) )
//and this would return 6
=getRight( getCell( 'Sheet2', "bar" ) )
I was able to get a function working that satisfies the above task. However, it is PAINFULLY SLOW! I'm using the function in about 100+ places, so this makes the sheet timeout on calculation every time I change something.
Can anyone suggest how to get the same functionality, but with much better performance?
function getCell( sheetname, item, row_offset, default_string )
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
var lastrow = sheet.getLastRow();
var lastcol = sheet.getLastColumn();
for( var c = 1; c <= lastcol; c++ )
{
for( var r = 1; r <= lastrow; r++ )
{
var range = sheet.getRange(r, c);
if( range.getValue() == item )
{
//item found! See if we can get the requested cell...
c = c + row_offset; //adjust column using given offset
if( c < 1 || c > lastcol )
return "E.offset";
else
return sheet.getRange(r,c).getValue();
}
}
}
return default_string;
}
Could you add a row and a column before your data range as follows:
A B C D E F G
1 "bar"
2 - - - -
3 - foo bar 6
4 - - - -
You have a cell where you have the cell content you are looking for, let's say in G1. In cell A2 you place the formula:
=COUNTIF(B2:E2,$G$1)
Which will check row A for the a match on the cell G1. Fill down the other cells in row A and cell A3 will show "1".
Use a similar formula for cell B1:
=COUNTIF(B2:B4,$G$1)
And fill across the row. Cell D1 will show a 1.
Then, use INDEX() with MATCH() to find the contents of the cells on either side:
Left:
=INDEX($B$2:$E$4,MATCH(1,$A$2:$A$4,0),MATCH(1,$B$1:$E$1,0)-1)
Right:
=INDEX($B$2:$E$4,MATCH(1,$A$2:$A$4,0),MATCH(1,$B$1:$E$1,0)+1)
No scripting required!

Count Reoccuring Values in Column of Google Spreadsheet Using Script

I'd like to count the number of times a value reoccurs in a specific column of my spreadsheet using a script so that it populates a different column (same row) with the count when a form is submitted. These are the values in Column B that I'd like to count:
6 ACM
5 ACM
4 ACM
5 CGC
7 CGC
6 ACM
7 ACM
7 ACM
so that if the calculation were working correctly Column C would be populated with these numbers:
1
1
1
1
1
2
1
2
I know how to read the data and how to write to the spreadsheet, but I don't know how to actually count the values. Here is what I have so far:
function countif() {
var ss = null;
try {
ss = SpreadsheetApp.openById("0AliYViHYAwaNdHQyMXlKT2Q5UElQY184T3BWYTRiM2c");
} catch (ex) {
ss = SpreadsheetApp.getActiveSpreadsheet();
}
var sheet = ss.getSheetByName("Sheet1");
var lastLine = sheet.getLastRow();
var data = sheet.getRange("B2:B").getValues();
var count = 0;
//need help here
sheet.getRange(lastLine,3).setValue(count);
}
Thank you!
Put this piece of code in your //need help section.
for (var i = 0 ; i < lastLine ; i++){
var count = 1;
for ( var j = 0 ; j < i ; i ++) {
if (data[j][0] == data[i][0] ){
count++;
}
}
sheet.getRange('C' + (i+1).toString()).setValue(count);
}
Just as an alternative, you can achieve this with a spreadsheet function, entered in C1:
=ArrayFormula(IF(ROW(B:B)=1;"Count";IF(LEN(B:B);MMULT((ROW(B:B)>=TRANSPOSE(ROW(B:B)))*(B:B=TRANSPOSE(B:B));SIGN(ROW(B:B)));IFERROR(1/0))))