get.Range with EQUIV Function ? (Or Vlookup or Match?) - google-apps-script

I'm new to Google Apps script, so sorry if this is a too easy question.
I've searched in the existing questions but could not make this work.
I have this function :
SpreadsheetApp.getActiveSheet().getRange(5,1).activate(); that works fine.
But instead of (5,1) I would like to have a dynamic value for the row number.
I would like to return the row number of a specific text that I chose in a list in A1.
Here is what I do :
function GoTo () {
var sheet = SpreadsheetApp.getActiveSheet();
var c = "EQUIV($A$1,A2:A100,0)+1";
{ SpreadsheetApp.getActiveSheet().getRange(c,1).activate(); }
}
If I put = EQUIV($A$1,A2:A100,0)+1 in a cell, i do get the right number I'm looking for.
Thanks a lot !
Could you help me ?

If I understand what you want the following function looks in column A starting at A2 and searches for what you entered in A1. If found, it activates the cell of the found value. It also returns the row number of the found row in cell B1.
function findText() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var find = sheet.getRange("A1").getValue()// cell of value to find in column A
var values = sheet.getRange(2, 1, sheet.getLastRow()-1).getValues();
for (var i = 0; i < values.length; i++) {
var row = "";
for (var j = 0; j < values[i].length; j++) {
if (values[i][j] == find) {
row = i+2;
sheet.getRange(1, 2).setValue(row); //row number found to C2
var range=sheet.getRange("A"+row)
range.activate() //activate cell of found value
}
}
}}
You could run this with an onEdit trigger.

Related

Google Script - Hide column if a cell within the column contains a specific value

I am trying to hide all columns in Google Sheets if a cell within that column contains the letter "S", I am checking row 6 which has the initials of each day of the week and want the ability to show and hide the weekends
Columns A6:G6 have M,T,W,T,F,S,S
Reason for the 9999 is due to this sheet containing multiple weeks and I am trying to look through all of them.
function Hide() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var data = sheet.getDataRange();
var lastCol = data.getLastColumn()+1;
for(var i = 1; i < lastCol - 9999; i++) {
if(data.getCell(6, 9999 + i).getValue() === "S") {
sheet.hideColumns(9999 + i);
}
}
};
This is what I have so far but it doesn't seem to do anything, can anyone help me understand what I am missing please as I have been looking around and cannot find anything that has helped me with the issue?
Thank you!!
Try this. Google Apps scripts is basically Javascript. This is how I would loop through the columns since the used area of the sheet is dynamically changed, then it will stop at the last used column.
function Hide() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// get the entire 6th Row
var range = sheet.getRange("6:6");
// get number of columns to loop through
var num_cols = range.getNumColumns();
// loop through columns and check value one by one
// if value is equal to "S", then hide the column
for (var i = 1; i <= num_cols; i++) {
var value = sheet.getRange(6,i).getValue();
if (value == "S") {
sheet.hideColumns(i);
};
}
}

Cant figure out IF(number) in Google Sheets macro to hide rows with a zero value

I'm brand new at this, so I may be way off base here. this is my code:
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("To Order");
var maxRows = sheet.getMaxRows();
//show all the rows
sheet.showRows(1, maxRows);
//get data from clumn B
var data = sheet.getRange('B:B').getValues();
//iterate over all rows
for(var i=0; i< data.length; i++){
//compare first character, if asterisk, then hide row
if(Number == "0"){
sheet.hideRows(i+1);
}
}
}
My trouble seems to be that the if(number) isn't only selecting cells with 0 to hide. please help!
The code in the question compares an undefined variable called Number with "0" using the abstract equality comparator. This comparison always will return false.
Instead of Number, use data[i][0].

Implement vlookup between two ranges in Google Apps Script

I'm looking to find a way to write a script that behaves similarly to a vLookup in google sheets.
Here is a link to a simplified example document.
What I am trying to do is use "Sheet1" as a form of sorts. When I enter information in "Sheet1" column B I want to be able to hit a custom menu button and have the information in "Sheet1" column B automatically populate correspondingly based of the values in column A into the first empty column on "Sheet 2".
I can write the script to create the custom menu and execute the function but I'm unsure of how to write the function itself.
Here is how such a function can look like. It gets pointers to each sheet, then the appropriate range from each: columns A and B, ignoring empty rows at the bottom. Then gets the values and begins comparing them: when columns A match, column B is assigned to. The final line puts the modified array values back into Sheet2.
function vl() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Sheet1');
var sheet2 = ss.getSheetByName('Sheet2');
var range1 = sheet1.getRange(1, 1, sheet1.getLastRow(), 2);
var range2 = sheet2.getRange(1, 1, sheet2.getLastRow(), 2);
var values1 = range1.getValues();
var values2 = range2.getValues();
for (var i = 0; i < values1.length; i++) {
for (var j = 0; j < values2.length; j++) {
if (values1[i][0] === values2[i][0]) {
values2[i][1] = values1[i][0];
}
}
}
range2.setValues(values2);
}

Simple conditional formatting in Google Sheets Script Editor

I am trying to create some basic conditional formatting rules with Scripts instead of the conditional formatting editor in sheets, because when a new row is added or removed, it breaks the rules already set. For example, when a row is deleted, the condition may be for the range A:C but then it adds "A1:C5,A6:C898". This causes some rows to be skipped by the rules so I am hoping a script will solve this.
So I want to simply change the cell background to green if the cell text is exactly "Y". I want to change it to red if text is exactly "N". I have other rules that I want to use but I am having trouble with the basics on this.
What I have so far in my script:
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("PRECLOSING");
var range = sheet.getRange("E:E,I:J,M:M,P:P,S:V,AB:AC,AF:AF");
range.activate();
var values = rangeY.getValues();
//for each row that data is present
for(var i = 0; i < values.length; i++) {
var cell = sheet.getRange(i + 1, 2);
if ( values == "X" )
{
cell.setBackground('black');
return;
} else {
cell.setBackground('white');
}
}
}
I made some modifications to your code.
The function getRange i don't think it can accept the ranges in that way, instead i think it can get just ranges that are continue (eg. A1:E10)
For that I created an array with the ranges you need and then I loop through them.
The function "getValues" returns a two dimensional array so you will need a second loop to get the actual value from the cell.
Then as you already have the range, you need to get the cell inside that range and not a new complete range.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("PRECLOSING");;
var columns = ['E:E','I:J','M:M','P:P','S:V','AB:AC','AF:AF'];
for(var col in columns){ //loop to iterate the desired ranges
var range = sheet.getRange(columns[col]);
range.activate();
var values = range.getValues();
//for each row that data is present
for(var i = 0; i < values.length; i++) { //loop to iterate the rows
for( var j = 0; j< values[i].length ; j++){ //loop to iterate the columns
var cell = range.getCell(i+1, j+1);
if ( values[i][j] == "X" )
{
cell.setBackground('black');
}
}
}
}
}
Hope this helps.

Google Apps Script - Find Row number based on a cell value

I'm looking for some assistance on find the row number for a cell that contains a specific value.
The spreadsheet has lots of data across multiple rows & columns. I'm looping over the .getDataRange and can located the cell containing the value I'm looking for. Once found I'd like to know the row that his cell is in, so that I can further grab additional cell values since I know exactly how many rows down and/or columns over the additional information is from the found cell.
Here is a bit of the code for finding the cell containing a specific string.
function findCell() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var dataRange = sheet.getDataRange();
var values = dataRange.getValues();
for (var i = 0; i < values.length; i++) {
var row = "";
for (var j = 0; j < values[i].length; j++) {
if (values[i][j] == "User") {
row = values[i][j+1];
Logger.log(row);
}
}
}
}
When outputting Logger.log(row) it gives me the values I'm looking for. I want to determine what row each value is in, so I can then go down X number of rows and over X columns to get the contents of other cells.
I don't know much about google apps but it looks like [i] is your row number in this circumstance.
So if you did something like:
function findCell() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var dataRange = sheet.getDataRange();
var values = dataRange.getValues();
for (var i = 0; i < values.length; i++) {
var row = "";
for (var j = 0; j < values[i].length; j++) {
if (values[i][j] == "User") {
row = values[i][j+1];
Logger.log(row);
Logger.log(i); // This is your row number
}
}
}
}
This method finds the row number in a particular column for a given value:
function rowOf(containingValue, columnToLookInIndex, sheetIndex) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets();
var dataRange = sheet[sheetIndex].getDataRange();
var values = dataRange.getValues();
var outRow;
for (var i = 0; i < values.length; i++)
{
if (values[i][columnToLookInIndex] == containingValue)
{
outRow = i+1;
break;
}
}
return outRow;
}
But if you do it like that, it won't refresh unless you change any of the parameters, that's because a custom function should be deterministic, that is, for the same parameters, it should always give the same result.
So instead, it's better to do it this way:
function rowOf(containingValue, range) {
var outRow = null;
if(range.constructor == Array)
{
for (var i = 0; i < range.length; i++)
{
if(range[i].constructor == Array && range[i].length > 0)
{
if (range[i][0] == containingValue)
{
outRow = i+1;
break;
}
}
}
}
return outRow;
}
In this case, you need to pass the full column range your looking for like so:
rowOf("MyTextToLookFor", 'MySheetToLookIn'!A1:A)
Where you would replace A by the colum of your choice, and MySheetToLookIn by your sheet's name and MyTextToLookFor by the text you are looking for.
This will allow it to refresh on adding rows and removing rows.
Since 2018 this can be done without a loop using flat().
function findRow(searchVal) {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var columnCount = sheet.getDataRange().getLastColumn();
var i = data.flat().indexOf(searchVal);
var columnIndex = i % columnCount
var rowIndex = ((i - columnIndex) / columnCount);
Logger.log({columnIndex, rowIndex }); // zero based row and column indexes of searchVal
return i >= 0 ? rowIndex + 1 : "searchVal not found";
}
ES6 gave us a one liner for this (but expanded for full detail).
function findRow() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss. getActiveSheet(); // OR GET DESIRED SHEET WITH .getSheetByName()
const dataRange = sheet.getDataRange();
const values = dataRange.getValues();
const columnIndex = 3 // INDEX OF COLUMN FOR COMPARISON CELL
const matchText = "User"
const index = values.findIndex(row => row[columnIndex] === matchText)
const rowNumber = index + 1
return rowNumber
}
This can be done with the Text Finder:
function findRow(searchValue){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var thisSheet = ss.getActiveSheet();
var tf = thisSheet.createTextFinder(searchValue)
var thisRow = tf.findNext().getRow()
return thisRow
}
Discovered through this post: Google App Scripts find text in spreadsheet and return location index
Implementing: https://developers.google.com/apps-script/reference/spreadsheet/text-finder
To find a row in a Google Sheets sheet based on the value of a specific column (in this case, the invoice_id column) and edit another column (in this case, the send column) in that row, you can use the following script:
// Replace "Sheet1" with the name of your sheet
var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet1");
// Replace "A" with the column letter for the invoice_id column
// and "B" with the column letter for the send column
var invoiceIdColumn = "A";
var sendColumn = "B";
// Replace "12345" with the invoice_id you want to search for
var invoiceIdToSearch = "12345";
// Find the row number of the row with the matching invoice_id
var data = sheet.getDataRange().getValues();
var row = data.findIndex(row => row[invoiceIdColumn - 1] == invoiceIdToSearch) + 1;
// If a row with the matching invoice_id was found
if (row) {
// Set the value of the send column in that row to "true"
sheet.getRange(row, sendColumn).setValue(true);
}
This script will search through the entire sheet for a row with an invoice_id value that matches the invoiceIdToSearch variable, and then set the value of the send column in that row to true.
Note that this script assumes that the invoice_id column is the first column in the sheet (column A) and the send column is the second column (column B). If your columns are in different positions, you will need to adjust the invoiceIdColumn and sendColumn variables accordingly.