Resize Rows using if else in GAS - google-apps-script

I am working on a script in which if a cell in col10 has a value it will auto resize the the row otherwise it will set the row height, in this case the row height is 72
The following image is a screenshot of what I am trying to achieve here
function reset_rows_auto() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var lastRow = sheet.getMaxRows().toString();
var lastRow = lastRow.replace(".0","");
for (var i = 1; i < lastRow; i++) {
var abc = sheet.getRange(i, 10) //col10
if (abc != "") {
sheet.autoResizeRows([i],1)
}
else if (abc == "") {
sheet.setRowHeight([i], 72)
}
}
}
Every time I run the script its, only auto resizing the row and not setting the row height of rows which have blank cell in Col10. I am not being able to figure the error out here, could anyone please help
Thanks in advance!

You have several typos in your code.
Try this:
function reset_rows_auto() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var lastRow = sheet.getMaxRows();
for (var i = 1; i < lastRow; i++) {
var abc = sheet.getRange(i, 10).getDisplayValue(); //col10
if (abc != "") {
sheet.autoResizeRows(i, 1);
}
else {
sheet.setRowHeight(i, 72);
}
}
}

Related

How to loop through cells in Google Sheets and log information if specific variables exist

You will need this link to solve this: - https://docs.google.com/spreadsheets/d/11PjVSWPfqOBPSej3AlQ-_T8Bws66kevR08Q2NRTmVcE/edit?usp=sharing
So this is a tricky one. I am looking to loop through the name in Sheet1 Column A, and also loop through the type in Column C. Then if Column A in Sheet1 matches the name of column A in Sheet2, AND the type is "pickup" I want to log the other information that is in Sheet 1 inside of Column C on Sheet 2. Log the things like "State", "Location", "City" next to the identical name in Sheet 2. I hope that makes sense.
I know I probably need to use a loop within a loop to do all this, but this is as far as I can get as I cannot figure out how to write the loop and then log the rows where the 2 names match.
function myFunction() {
function pullDeliveryNotes()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
var dataRange = sheet.getDataRange();
var dataRange2 = sheet2.getDataRange();
var rng = sheet.getRange(2,1, dataRange.getLastRow()-1,dataRange.getLastColumn());
var rng2 = sheet.getRange(2,3, dataRange.getLastRow()-1,dataRange.getLastColumn());
var rng3 = sheet2.getRange(2,1, dataRange2.getLastRow()-1,dataRange2.getLastColumn());
var rngA = rng.getValues().toString()
var rngB = rng2.getValues().toString()
var rngC = rng3.getValues().toString()
for(var i = 0; i < rng.length; i++) {
for (var x = 0; x < rng2.length; x++) {
for (var y = 0; y < rng3.length; y++) {
if (rng[i][x][y].includes(rng3[i][x][y]) && rng2[i][x][y] === "pickUp") {
Logger.log("We got it")
}
}
}
}
}
}
Based on your problem statement.
Try this sample script:-
function checkValues() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ssSource = ss.getSheetByName('Sheet1')
const ssTarget = ss.getSheetByName('Sheet2')
const sourceRange = ssSource.getDataRange().getValues().filter(r=> r[2] === 'Pickup'); // Filtering only pickup values
const targetRange = ssTarget.getDataRange().getValues();
for(var i = 0 ; i < sourceRange.length ; i++)
{
for(var j = 1 ; j < targetRange.length ; j++)
{
if(sourceRange[i][0] === targetRange[j][0]) // if name matches
{
ssTarget.getRange(`C${j+1}`).setValue(`${sourceRange[i][3]},${sourceRange[i][4]},${sourceRange[i][5]}`) // set values in column C
}
}
}
}

How to keep a randomization formula from randomizing at each change in the sheet?

I have this cute formula:
=sort(A1:A416,arrayFormula(randbetween(sign(row(A1:A416)),C1)),true)
I thought I could change the randomization event when I changed the value in C1, but now I see this is changing continuously when I update the sheet with other values.
How can I keep this from happening?
I only want the randomization to happen when I change the seed in C1.
Try using this edit trigger in your script:
function onEdit(e) {
if(e.range.getA1Notation() == 'C1') {
var sheet = e.source.getActiveSheet();
var firstRow = 1;
var lastRow = 416;
var col = 1;
var seedValue = Number(e.range.getValue());
if(seedValue > 1) {
for(var row = firstRow; row < lastRow; row++) {
var cellValue = Math.ceil(Math.random() * seedValue);
var cell = sheet.getRange(row, col);
cell.setValue(cellValue);
}
}
var range = sheet.getRange(firstRow, col, lastRow, 1)
range.sort(1);
}
}
I hope this is what you wanted to achieve with your formula.

Drop down to hide rows based on a dropdown

In Google Sheets I want to hide rows 5 to 31 when my drop down in R4 says BLS, and show those cells if R4 is blank or says ALS.
Here is my most recent attempt:
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("Shee1");
var rg = sh.getDataRange();
var vA = rg.getValues();
var R4 = sh.getRange("R4").getValue();
for (var i = 0; i < vA.length; i++) {
var row = i + 1;
switch (R4) {
case "BLS":
if (row >= 5 && row <= 31) {
sh.hideRows(row);
}
break;
case "ALS":
if (row >= 4 && row <= 32) {
sh.showRows(row);
}
break;
default:
}
}
}
I have also tried this, which points to 2 different macros that I recorded but it makes the screen go crazy and jumps around whenever something else is edited:
function onEdit(){
if(SpreadsheetApp.getActiveSheet().getRange('R4').getValue() == "BLS") BLS(); // Launches the script
if(SpreadsheetApp.getActiveSheet().getRange('R4').getValue() == "ALS") ALS(); // Launches the script
}
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("Sheet1"); // Enter sheet name
var row = s.getRange('R:R').getValues();
// Enter column letter that has the text "hide" and "unhide"
s.showRows(1, s.getMaxRows());
for(var i=0; i< row.length; i++){ if(row[i] == 'hide') { s.hideRows(i+1, 27); }
else if(row[i] == 'unhide'){ s.unhideRow(ss.getDataRange()); }
}
}

Find and write to next blank cells in a column using App maker

I need help with finding a number from a specific row in column A and write next to its blank cells in two different columns of the same row. e.g Find a number in COLUMN A2, Write to cells in COLUMN B2 and C2.
I have currently used filters and it hasn't helped.
function filterRange(filteredRange) {
return filteredRange == [2.0];
}
function findCellByValue() {
var ss = getSpreadSheet();
var sheet = ss.getActiveSheet();
var dataRange = sheet.getDataRange();
var values = dataRange.getValues();
for (var i = 0; i < values.length; i++) {
var range = values[i];
var filtered = range.filter(filterRange);
var newValue = filtered.concat(["Tee", "uhuuu"]);
console.log(newValue);
var ssheet = sheet.getRange("B2:C2");
console.log(ssheet.setValues([newValue]));
}
}
Anyways I have managed to find a workaround it though it is not the best possible solution but for this, it works.
function findCellByValue(cellValue) {
console.log(cellValue);
var ss = getSpreadSheet().getActiveSheet();
var range = ss.getRange("A2:A").getValues();
for(var i = 0; i < range.length; i ++)
{
if(range[i] == cellValue)
{
var Arrayvalues = range[i].concat(["test1","test2"]);
ss.getRange(i+2,1,1,3).setValues([Arrayvalues]);
i = range.length;
}
}

How do I pass a number to .hideRow()?

I got the following script from #Mike Grace's website:
// Deletes rows in the active spreadsheet that contain 'Yes' in column A
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 0;
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
if (row[0] == 'Yes') {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
}
The script works well, but I would like to make a change to it, instead of calling .deleteRow() I want to use .hideRow() instead.
Because the .hideRow() method only accepts a range as far as I understand, I'm getting the following error:
How do I modify this script so that it hides the row instead of deleting them?
there are different hideRow / s () methods, one of them takes a row number as argument.
your code can be simplified like this :
function hideRowsWithYes() {
var sheet = SpreadsheetApp.getActiveSheet();
var values = sheet.getDataRange().getValues();
for (var i = 0; i < values.length; i++) {
if (values[i][0] == 'Yes') {
sheet.hideRows(1+i);
}
}
}
As usual, autocomplete makes the job easier...
Edit following comment :
to unhide row we don't have a simple method with row index so we need to define the range... code goes like this (i has a +1 because values is an array that starts from 0 while sheets are indexed from 1)
function unHideRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var values = sheet.getDataRange().getValues();
var row;
var maxCol = sheet.getMaxColumns();
for (var i = 0; i < values.length; i++) {
if (values[i][0] == 'Yes') {
row = sheet.getRange(1+i, 1, 1, maxCol);
sheet.unhideRow(row);
}
}
}