Get the spreadsheet row for a matched value - javascript

I'm new to Google Apps Script & I'm trying to make a script for a spreadsheet where I can get the range for the matched value. But there's no function for the cell value like .getRow() or .getRange() or something like this since it's a string value. Here are my codes,
function getInfo() {
var ss1 = sheet1.getSheetByName("Sheet1");
var ss2 = sheet2.getSheetByName("Rates");
var getCountry = ss1.getRange(ss1.getLastRow(), 11).getValue();
var countryRange = ss2.getRange(2, 1, ss2.getLastRow(), 1).getValues();
var getZone = 0;
for (var i = 0; i < countryRange.length; i++) {
if (countryRange[i] == getCountry) {
var getrow_cr = countryRange[i].getRow(); //.getRow() doesn't apply here for string value
getZone = ss2.getRange(getrow_cr + 1, 2).getValue();
}
}
Logger.log(getZone);
}
How can I get the row for the matched string so that I can get the cell value beside the matched cell value?

Your looping variable i starts at 0, which in your case is equivalent to Row 2. To help keep that clear, you could use a named variable to track the first row with data:
var firstrow = 2;
Then when you find a match, the spreadsheet row it's in is i + firstrow.
Updated code:
function getInfo() {
var ss1 = sheet1.getSheetByName("Sheet1");
var ss2 = sheet2.getSheetByName("Rates");
var getCountry = ss1.getRange(ss1.getLastRow(), 11).getValue();
var firstrow = 2;
var countryRange = ss2.getRange(firstrow, 1, ss2.getLastRow(), 1).getValues();
var getZone = 0;
for (var i = 0; i < countryRange.length; i++) {
if (countryRange[i][0] == getCountry) {
var getrow_cr = i + firstrow;
getZone = ss2.getRange(getrow_cr, 2).getValue();
break; // Found what we needed; exit loop
}
}
Logger.log(getZone);
}
Since countryRange is a two-dimensional array with one element in each row, the correct way to reference the value in row i is countryRange[i][0]. If you instead just compare countryRange[i], you are relying on the JavaScript interpreter to coerce the "row" into a String object.

Related

Google App Script: Find & Replace for specific columns

Here is the problem & logic for the find & replace script I am using.
Search Sheet for to_replace string.
If found, replace to_replace with replace_with.
If not found, replace to_replace with to_replace // This is not needed, and causes problems (it replaces all formulas, and replaces it with a string).
My Objective:
I would like the script to only replace cells that match to_replace, and ignore every other cell.
My Rookie Solution:
Exclude specific columns in the foruma by eliminating column C from array using script from here. (only find & replace within Column B & D).
Here is the modified code I added in My Current Script...
const range = sheet.getRange('B2:D'+lastRow).getValues();
range.forEach(a => a.splice(1, 1)); //removes column C.
But I get the error: "TypeError: var data = range.getValues(); is not a function"
Question
Can you help me troubleshoot my rookie solution, or teach me a better way to solve this problem?
My current script
function findAndReplace(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var lastRow = sheet.getLastRow()
var lastColumn = sheet.getLastColumn()
// var range = sheet.getRange(1, 1, lastRow, lastColumn) //REMOVED - Searches all columns.
const range = sheet.getRange('B2:D'+lastRow).getValues(); //ADDED - Searches only B & D
range.forEach(a => a.splice(1, 1)); //ADDED - Searches only B & D
var to_replace = "TextToFind";
var replace_with = ""; //Leave blank to delete Text, or enter text in quotes to add string.
var data = range.getValues();
var oldValue="";
var newValue="";
var cellsChanged = 0;
for (var r=0; r<data.length; r++) {
for (var i=0; i<data[r].length; i++) {
oldValue = data[r][i];
newValue = data[r][i].toString().replace(to_replace, replace_with);
if (oldValue!=newValue)
{
cellsChanged++;
data[r][i] = newValue;
}
}
}
range.setValues(data);
}
From teach me a better way to solve this problem, in your situation, I thought that when TextFinder is used, the process cost might be able to be reduced. When TextFinder is used for achieving your goal, it becomes as follows.
Sample script:
function myFunction() {
var to_replace = "TextToFind";
var replace_with = ""; //Leave blank to delete Text, or enter text in quotes to add string.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var lastRow = sheet.getLastRow();
var ranges = ['B2:B' + lastRow, 'D2:D' + lastRow];
sheet.getRangeList(ranges).getRanges().forEach(r =>
r.createTextFinder(to_replace).matchEntireCell(true).replaceAllWith(replace_with)
);
}
Note:
If you want to replace the part of cell value, please modify r.createTextFinder(to_replace).matchEntireCell(true).replaceAllWith(replace_with) to r.createTextFinder(to_replace).replaceAllWith(replace_with).
As an additional modification, if your script is modified, how about the following modification?
function findAndReplace() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var lastRow = sheet.getLastRow()
var range = sheet.getRange('B2:D' + lastRow);
var data = range.getValues();
var to_replace = "TextToFind";
var replace_with = ""; //Leave blank to delete Text, or enter text in quotes to add string.
for (var r = 0; r < data.length; r++) {
for (var i = 0; i < data[r].length; i++) {
var value = data[r][i].toString();
if (i != 1 && value.includes(to_replace)) {
data[r][i] = data[r][i].replace(to_replace, replace_with);
}
}
}
range.setValues(data);
}
References:
createTextFinder(findText)
Class TextFinder

How to get row number of an array that's being looped through?

Using Google AppScripts and found this script online which I've modified.
NOTE: Spreadsheet has headers.
Run through the values found in 3rd column
If any of the values include the word "USD", copy the entire row into "Target Sheet"
Delete that row after it's finished copying
Continuing looping through until it finds the next "USD"...etc.
What Works:
I'm successfully able to loop through the array and copy the correct rows into the "Target Sheet"
What I Need Help With:
I can't figure out how to delete the row from the original sheet. It always ends up deleting the row before, then skips 1 row every time it loops again. I've tried multiple different formats to this portion of the code, such as i-1, i+1, etc... Not sure what I'm doing wrong here:
if (i == 0) {
ss1.deleteRow(i+2);
} else {
ss1.deleteRow(i)
}
I've pasted the entire script below:
var etfar = ["Cash File"] //This is a string because I have multiple sheets I'm looping through
function cashCopy(etf) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ss1 = ss.getSheetByName(etf);
var ss2 = ss.getSheetByName("Target Sheet");
var lr = ss1.getLastRow();
var lc = ss1.getLastColumn();
// gets the data in Cash File
var range = ss1.getRange(1, 1, lr, lc);
var data = range.getValues();
// loops through rows in data
for (var i = 0; i < data.length; i++) {
var check = data[i][2] // ith row, 3rd column
if (check.includes("USD")) {
var rowToCopy = data[i];
ss2.appendRow(rowToCopy);
if (i == 0) {
ss1.deleteRow(i+2);
} else {
ss1.deleteRow(i)
}
};
}; // end i
}
for (var i = 0; i < etfar.length; i++) {
cashCopy(etfar[i])
}
Explanation:
One way to iteratively delete rows in a sheet is to create a backwards for loop.
Replace:
for (var i = 0; i < data.length; i++)
with:
for (var i = data.length - 1; i >= 0; i--)
In this way, every time you delete a row, the data will still correspond to the correct row.
Another issue is that you get var range = ss1.getRange(1, 1, lr, lc). That means, you start iterating from the first row (including the headers) and then you are using a workaround like that:
if (i == 0) {
ss1.deleteRow(i+2);
} else {
ss1.deleteRow(i)
}
But actually, you don't need to include the headers in the first place. Use this instead: var range = ss1.getRange(2, 1, lr, lc) and replace the if/else statement with just: ss1.deleteRow(i+2).
since data index starts from 0, but your range starts from row 2.
Solution:
var etfar = ["Cash File"] //This is a string because I have multiple sheets I'm looping through
function cashCopy(etf) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ss1 = ss.getSheetByName(etf);
var ss2 = ss.getSheetByName("Target Sheet");
var lr = ss1.getLastRow();
var lc = ss1.getLastColumn();
// gets the data in Cash File
var range = ss1.getRange(2, 1, lr, lc); // <- modification
var data = range.getValues();
// loops through rows in data
for (var i = data.length - 1; i >= 0; i--)
{
var check = data[i][2] // ith row, 3rd column
if (check.includes("USD")) {
var rowToCopy = data[i];
ss2.appendRow(rowToCopy);
ss1.deleteRow(i+2); // <- new code
};
}; // end i
}
for (var i = 0; i < etfar.length; i++) {
cashCopy(etfar[i])
}

Trying to paste Values from formula Google App Script

This is just a snippet of my code from Google App Script which iterates through each row in columns 1, 2, 3. If an edit is made in column 3, an incremental ID will be generated and a concatenation of the same row and different columns will also be generated - in this case Column D, E, and F. I am struggling with figuring out a way to change the formulas into values. What am I missing here?
// Location format = [sheet, ID Column, ID Column Row Start, Edit Column]
var locations = [
["Consolidated Media Plan",1,9,3]
];
function onEdit(e){
// Set a comment on the edited cell to indicate when it was changed.
//Entry data
var range = e.range;
var col = range.getColumn();
var row = range.getRow();
// Location Data
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
function getNewID(){
function IDrange(){
var dataRange = sheet.getDataRange();
var lastRow = dataRange.getLastRow();
return sheet.getRange(IDrowStart,IDcol,lastRow-IDrowStart).getValues();
};
//Get largest Value in range
function getLastID(range){
var sorted = range.sort();
var lastIDval = sorted[sorted.length-1][0];
return lastIDval;
};
//Stores leading letters and zeroes and trailing letters
function getLettersNzeroes(id){
//Get any letters or zeroes.
var re = new RegExp("^([a-zA-Z0])$");
var letterZero = [];
for(char = 0; char < id.length; char++){
if(re.test(id[char])){
letterZero.push([char,id[char]]);// [[position, letter or zero]]
};
};
// Categorize letters and zeroes into start and end blocks
var startLetterZero = "",
endLetter = "",
len = letterZero.length - 1;
for(j = 0; j < letterZero.length; j++){
if(letterZero[j][0] === j){
startLetterZero += letterZero[j][1];
}else if(letterZero[j][1] !== "0" && letterZero[len][0] - (len - j) == letterZero[j][0]){
endLetter += letterZero[j][1];
};
};
var startNend = {"start":startLetterZero,"end":endLetter};
return startNend;
};
//Gets last id number. Adds 1 an checks to set if its new length is greater than the lastNumber.
function getNewNumber(id){
var removeZero = false;
var lastNum = parseInt(id.replace(/\D/g,''),10);//Remove letters
var newNum = (lastNum+1).toString();
if(lastNum.toString().length !== newNum.length){
var removeZero = true;
};
var newNumSet = {"num":newNum, "removeZero": removeZero};
return newNumSet
};
var lastID = getLastID(IDrange());
var lettersNzeroes = getLettersNzeroes(lastID);
var newNumber = getNewNumber(lastID);
//If the number is 9,99,999,9999 etc we need to remove a zero if it exists.
if(newNumber.removeZero === true && lettersNzeroes.start.indexOf("0") !== -1.0){
lettersNzeroes.start = lettersNzeroes.start.slice(0,-1);
};
//Rejoin everything together
var newID = lettersNzeroes.start +
newNumber.num +
lettersNzeroes.end;
return newID;
};
for(i = 0; i < locations.length; i++){
var sheetID = locations[i][0],
IDcol = locations[i][1],
IDrowStart = locations[i][2],
EditCol = locations[i][3];
var offset = IDcol - EditCol;
var cell = sheet.getActiveCell();
if(sheetID === sheet.getName()){
if(EditCol === col){
//ID Already Exists the editing cell isn't blank.
if(cell.offset(0,offset).isBlank() && cell.isBlank() === false){
var newID = getNewID();
cell.offset(0,offset).setValue(newID);
cell.offset(0,-1).setFormulaR1C1('=concatenate(R[0]C[-1],"_",INDEX(Glossary!K:K,MATCH(R[0]C[2],Glossary!J:J,0)))');
};
};
};
};
};
EDIT:
This is my full code, I have been unsuccessful with trying to retrieve just the values of the formula within the same (i.e, If C9 gets edited, a formula with the values specific to the 9th row should be populated)
Also, I've tried to add an index/match formula to the concatenation formula at the bottom of the code - it works as expected on the google sheets, but when I run it with the script it pastes the correct formula but it returns a #NAME? error message. However, when I copy and paste the exact same formula in the cell, it works perfectly, any idea what could be causing this error?
This works for me. I know it's not exactly the same thing but I didn't have access to getNewId()
function onEdit(e) {
var sh=e.range.getSheet();
if(sh.getName()!='Sheet1')return;
//e.source.toast('flag1');
if(e.range.columnStart==3 && e.range.offset(0,1).isBlank() && e.value) {
//e.source.toast('flag2');
e.range.offset(0,1).setValue(e.value);
e.range.offset(0,2).setFormulaR1C1('=concatenate(R[0]C[-1],"_",R[0]C[-2],"_",R[0]C[-3],"_",R[0]C[-4])');
}
}

How should I add the rows (without the headers) after the last row with data (without overwriting anything in the "Master_db")

The code below is overwriting on the existing data.
#OMila helped me with the original code, I could not articulate exactly what I needed hence starting a new question.
function Dom() {
var origin_sheet = SpreadsheetApp.getActive().getSheetByName('Dom_Sum');
var firstRow = 1;
var firstCol = 1;
var numRows = origin_sheet.getLastRow();
var numCols = 22;
var origin_values = origin_sheet.getRange(firstRow, firstCol, numRows, numCols).getValues();
var dest_values = [];
for(var i = 0; i < origin_values.length; i++) {
if(origin_values[i][0] != '') {
dest_values.push(origin_values[i]);
}
}
var dest_id = "1ZGq7L7bvF1INuDgZxhHnVsihkYkYubmncSAE5uC-Pq4";
var dest_sheet = SpreadsheetApp.openById(dest_id).getSheetByName("Master_Db");
var numRowsDest = dest_values.length;
var dest_range = dest_sheet.getRange(1, 1, numRowsDest, 22);
dest_range.setValues(dest_values);
}
I would like to add the data created in the "Dom_Sum" worksheet below the last row of data in the other workbook with the worksheet name "Master_Db"
#OMila I'm really grateful to you, and if you like we could offer you a consultation fee for future projects. (boseav#gmail.com)
Instead of writing your value into the range dest_sheet.getRange(1, 1, numRowsDest, numCols)
retrieve the last row of your destination sheet and write starting with the next row
var destLastRow=dest_sheet.getLastRow();
var dest_range = dest_sheet.getRange(destLastRow+1, 1, numRowsDest, numCols);
dest_range.setValues(dest_values);

Google sheets script - Strip extra data from a second line and add it to the first line

I'm trying to build a script in google sheets script that will iterate down a sheet, row by row, and if it encounters an ID number in the first cell of that row that is identical to the one above it, it strips out the data from every cell EXCEPT Column A and B, and appends it to the line above. Ideally, this would work with an indeterminate number of duplicate row IDs, might be 2, might be 3, might be 4.
After stripping out the data I want to keep (eg colums C and onward), I then want to delete the entire contents of the processed duplicate ID row, but I just haven't put that in my script until after it copies the data correctly.
In this example, sheet rows 6, 7 and 8 have identical ID numbers (Column A)
Here is the result I'm trying to get:
And here is the result I'm getting:
I've tried a number of different ways, and torn down and rebuilt my script a couple of times without getting the result I want:
function stripMiner() {
var ss = SpreadsheetApp.openById("1WDPoTICQvdruxfhAwHLtA51fz05DqyZ-NhNfpAyPO6Y");
var mainSheet = ss.getSheetByName("Main");
var startRow = 5;
var numRows = mainSheet.getLastRow();//obtains the last row in the sheet
var setrgh = mainSheet
var dataRange = mainSheet.getRange(startRow, 1,4,120); //rowStart, columnStart, row count, column count, the columncount needs to be large enough to encompass all your ancillary data
var data = dataRange.getValues();
var iter = 0;
var maxItRow = 4;
var prevIdNum = 0;
var dupCount = 1;
var cc1 = "P5"; //Cells to dump check values into
var cc2 = "P6";
var dumpRow = startRow;
//if (numRows >= maxItRow){var maxIter = maxItRow;}
for (i in data){
if (iter != maxItRow){ //making sure we haven't gone over the iteration limit
var row = data[i];
var idNum = (row[0]);
var jCount = 0; //resets icount if the id number is different icount is used to skip some cells in a row
if (idNum == prevIdNum){//only proceed if we've hit another line with the same ID number
dupCount = +1; //increment the dupcount value
mainSheet.getRange(cc2).setValue("dupCount"+dupCount); //dupcount check value
var rowIterStart = 5; //RowIterStart is used to add to rowiter, EG if your data is 20 columns wide, and you start transposing from column 4, then this will want to be about 17
var rowIter = 1;
for (j in row){
if (jCount >= 2){ //the integer here is the column where it will begin to transpose data
mainSheet.getRange(dumpRow-1,(rowIterStart*dupCount)+(rowIter)).setValue(row[j]); //startRow+(iter-dupCount)
mainSheet.getRange(cc1).setValue("dumprow"+dumpRow);
}
rowIter+=1;
jCount +=1;
}
}
else{
var dupCount = 1;
dumpRow +=1;
}
prevIdNum = (row[0]); //sets the most recently processed rows ID number
}
iter +=1;
}
}
I'm not quite sure where I'm going wrong. Does anyone have any suggestions? Thanks!
(Also I'm still just a beginner with this so if I've overlooked anything obvious or taken the wrong approach to do this, I apologize!)
The results for the questioner's code in the case of copied data arise from a convoluted loop. In essence, though duplicates were identified, there was a mis-counting to assign the copied data to the correct rowID. So far as clearing data, no provision was included.
The following code works to meet the questioner's goals, though it is far from perfect.
At present, the recalculation of the "last column" after copy each duplicate is an absolute rather than than a row-based figure. So, if a duplicate was detected for, say, ID=3, the data would be copied to column 12 rather than column 6. This requires the addition of a simple dupID row counter.
The second factor is the calculation of the last column in the spreadsheet.
var dataRange = mainSheet.getRange(startRow, 1,Rowlast+1,120);
The questioner used 120 columns; and I have retained that number simply for the sake of consistency. The questioner should re-assess whether this is excessive.
function ejb_so_5284922701() {
var ss = SpreadsheetApp.openById("<< insert questioners spreadsheet ID>>");
var mainSheet = ss.getSheetByName("<< insert questioner's sheet name >>");
var startRow = 5;
// calculate the last row containing data
var Rowvals = ss.getRange("A5:A").getValues();
var Rowlast = Rowvals.filter(String).length; //6
Logger.log("last row = " + Rowlast); // DEBUG
// calculate the last column containing data
var cell = mainSheet.getRange("A5"); //or however you determine "cell"
var drCol = mainSheet.getDataRange().getLastColumn();
Logger.log('getLastColumn = ' + drCol); //DEBUG
for (var i = drCol; i >= 1; i--) {
if (mainSheet.getRange(cell.getRow(), i).getValue() != "") {
break;
}
}
var lastColumn = i;
Logger.log("Last column with data = " + lastColumn); //DEBUG
var setrgh = mainSheet
// numColumns neds to be reviewed
var dataRange = mainSheet.getRange(startRow, 1, Rowlast + 1, 120); //rowStart, columnStart, row count, column count, the column count needs to be large enough to encompass all your ancillary data
// start row = 5, 1= column A, 4, rows, 120, columns
Logger.log("startRow = " + startRow + ", and the datarange = " + dataRange.getA1Notation()); //DEBUG
var data = dataRange.getValues();
Logger.log("length of data =" + data.length); //DEBUG
var lastid = 0;
for (i = 0; i < data.length; i++) {
if (i == 0) {
// if this is the first row, then assign anything but zero to last id
lastid = 100;
Logger.log(" this is the first row; set last id to 100");
}
var thisid = data[i][0];
// evaluate whether this is a duplicate ID
if (thisid == lastid) {
// this is a dup
Logger.log("i=" + i + ". This is a dup" + ", name is " + data[i][2]); //DEBUG
var stufftocopyrange = mainSheet.getRange(startRow + i, 3, 1, 3);
var stufftocopy = stufftocopyrange.getValues();
Logger.log("the range to copy is " + stufftocopyrange.getA1Notation()); //DEBUG
var targetrange = mainSheet.getRange(startRow + lastid - 1, lastColumn + 1, 1, 3);
targetrange.setValues(stufftocopy);
lastColumn = lastColumn + 3;
var duprange = mainSheet.getRange(startRow + i, 1, 1, 5);
Logger.log("the range to clear is " + duprange.getA1Notation()); //DEBUG
duprange.clearContent();
} else {
// no dup
//assign lastid value
var lastid = thisid;
Logger.log("i=" + i + ". No dup. Last id set to " + lastid); // DEBUG
} // if
} // end for loop
}
BEFORE
AFTER
The solutions previously posted didn't quite get the result I needed, however I managed to cobble together something that works for my purposes. It expects to see data in the format like:
And turn it into something like this:
Where it uses duplicate ID numbers (with an indeterminite number of duplicates) to pull only certain columns of data from the duplicate lines and append it to the first line.
function stripMiner() {
var ss = SpreadsheetApp.openById("1WDPoTICQvdruxfhAwHLtA51fz05DqyZ-NhNfpAyPO6Y");
var mainSheet = ss.getSheetByName("Main");
var startRow = 5;
var numRows = mainSheet.getLastRow();//obtains the last row in the sheet
var setrgh = mainSheet
var dataRange = mainSheet.getRange(startRow, 1,3,120); //rowStart, columnStart, row count, column count, the columncount needs to be large enough to encompass all your ancillary data
var data = dataRange.getValues();
var iter = 0;
var maxItRow = 4;
var prevIdNum = 0;
var dupCount = 1;
var cc1 = "P5"; //Cells to dump check values into
var cc2 = "P6";
var dumpRow = startRow;
//if (numRows >= maxItRow){var maxIter = maxItRow;}
for (i in data){
if (iter != maxItRow){ //making sure we haven't gone over the iteration limit
var row = data[i];
var idNum = (row[0]);
var jCount = 0; //resets icount if the id number is different icount is used to skip some cells in a row
if (idNum == prevIdNum){//only proceed if we've hit another line with the same ID number
dupCount = +1; //increment the dupcount value
mainSheet.getRange(cc2).setValue("dupCount"+dupCount); //dupcount check value
var rowIterStart = 5; //RowIterStart is used to add to rowiter, EG if your data is 20 columns wide, and you start transposing from column 4, then this will want to be about 17
var rowIter = 1;
for (j in row){
if (jCount >= 2){ //the integer here is the column where it will begin to transpose data
mainSheet.getRange(dumpRow-2,(rowIterStart*dupCount)+(rowIter)).setValue(row[j]); //startRow+(iter-dupCount)
mainSheet.getRange(cc1).setValue("dumprow"+dumpRow);
}
rowIter+=1;
jCount +=1;
}
}
else{
var dupCount = 1;
dumpRow +=1;
}
prevIdNum = (row[0]); //sets the most recently processed rows ID number
}
iter +=1;
}
}
Hopefully someone else who wants to do a similar thing can make use of this too.

Categories

Resources