Apps Script Code wont delete a boolean Value - Apps Script / GoogleSheets - google-apps-script

I have this code below that works fine for deleting rows containing certain text.
function DeleteTRUE(e) {
var sheet = SpreadsheetApp.getActive();
sheet.setActiveSheet(sheet.getSheetByName('HOLDING'), true);
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[4] == "TRUE") {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
}
However when I put a TRUE to try to delete a row (that has a checkbox) and someone checks it,
it doesnt work. It is being triggered from an onEdit currently with the word "MOVE" which is working as it should. would prefer if it was a checkbox that they checked and it deleted the row.
Thanks in advance

If I create default checkboxes equal true works for me fine
function deleteTRUE() {
var sheet = SpreadsheetApp.getActive();
sheet.setActiveSheet(sheet.getSheetByName('HOLDING'), true);
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[4] == true) {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
If you need check for "TRUE" you have override validation values.
It's very important you need to type 'TRUE 'FALSE for string interpretation.

Related

Googlesheet adding unwanted rows

I have two sheets (Sheet1 and Mirror) in a single google spreadsheet. I have some columns in the Mirror sheet which are being mirrored from Sheet1 using an array formula (={"Firstname";ARRAYFORMULA(INDIRECT("Sheet1!B2:B"))}).
I have rows being copied automatically from a google form into Sheet1. For some reason, periodically, about 500 empty rows are entered into the Mirror sheet and I have to manually delete them.
I have these googlesheet functions below that trigger on change. I have only deployed the function ReadRows and it works like a charm. Rather than deploying the second function (DeleteBlankRows) which might slow down my spreadsheet, I do like to know how I can figure out what is causing the extra periodic 500 blank rows to appear and stop it or I'd like any professional advice you can give.
Please see the sample scripts below. Thanks in advance.
function readRows() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
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[1] == 0 || row[1] == '') && (row[2] == 0 || row[2] == '')){
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
function DeleteBlankRows() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Mirror');
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[1] == 0 || row[1] == '')){
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
Updated answer:
The issue is that you need to delete the rows backwards for Sheet1 and then you don't need to use DeleteBlankRows for Mirror anymore. Replace your code with this one:
function readRows() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var rows = sheet.getDataRange();
var values = rows.getValues();
for (var i = values.length - 1; i>=0; i--) {
var row = values[i];
if ((row[1] == 0 || row[1] == '') && (row[2] == 0 || row[2] == '')){
sheet.deleteRow(i+1);
}
}
};
To get rid of empty rows, you can simply filter them out before you copy them to the target sheet.
Change:
var values = rows.getValues();
to:
var values = rows.getValues().filter(r=>r[0]!='');
Assuming for every empty row, the cell in column A is also empty.
You can now remove the DeleteBlankRows unless you want to delete also the rows.

Modify Code - Delete Row If Cell Has any Data - Google Sheets / Google Apps Script

I would like to modify this code so that if there is any data(Numbers as well) in column 7, it will delete the entire row.
Currently whatever you put into the inverted commas in the If Statement, it deletes
what could you substitute that would do that ?
function DeleteCOMPLETED2() {
var sheet = SpreadsheetApp.getActive();
sheet.setActiveSheet(sheet.getSheetByName('RELOCATION'), true);
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[7] == "**if there is any data in column 7 then delete entire row**") {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}}}
Delete row if anything in column7
function DeleteCOMPLETED2() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('RELOCATION');
var values=sh.getDataRange().getValues();
var d=0;
values.forEach(function(r,i){if(r[6]){sh.deleteRow(i+1-d++);}});//column7
}

Loop to disable function in onEdit script

I have a script that adds specific values to the last row of the sheet by clicking a checkbox (has yes/no values) and deletes the row by unchecking the same checkbox. It works fine
but as it is onEdit script - it keeps on going adding values. I am trying to loop all this job to check if the value already exists and if yes - do nothing. I developed the following but it does not work - keeps adding values with every edit. Here is the code
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getActiveSheet()
var paramrange = sheet.getRange('A55:C60')
var destn = sheet.getRange(sheet.getLastRow()+1, 1)
var destnorma = sheet.getRange(sheet.getLastRow()+1, 2)
var orpac = sheet.getRange('E23')
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
if (row[0] == 'ORP Ac') {
} else{
if(orpac.getValue() == 'yes') {
var orpacn = 'ORP Ac';
var orpacnorm = '-150';
destn.setValue(orpacn)
destnorma.setValue(orpacnorm)
} else
{
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] == 'ORP Ac') {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}}
}
}}
Your second loop is inside the first loop and it uses the same index. That's a problem.
This might work better. But I can't tell since I don't have your data.
function onEdit() {
var ss=SpreadsheetApp.getActive()
var sheet=ss.getActiveSheet()
var paramrange=sheet.getRange('A55:C60')
var destn=sheet.getRange(sheet.getLastRow()+1, 1)
var destnorma=sheet.getRange(sheet.getLastRow()+1, 2)
var orpac=sheet.getRange('E23')
var orpacvalue=orpac.getValue();
var rows=sheet.getDataRange();
var numRows=rows.getNumRows();
var values=rows.getValues();
for (var i=0;i<numRows;i++) {
var row=values[i];
if (row[0]!='ORP Ac') {
if(orpacvalue=='yes') {
var orpacn = 'ORP Ac';
var orpacnorm = '-150';
destn.setValue(orpacn)
destnorma.setValue(orpacnorm)
} else
{
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 0;
for (var j = 0; j<numRows; j++) {
var row = values[j];
if (row[0] == 'ORP Ac') {
sheet.deleteRow((parseInt(j)+1) - rowsDeleted);
rowsDeleted++;
}
}
}
}
}
}
This appears to be a lot of script for a simple trigger which needs to complete in less than 30 seconds.

(Google Sheets script) Clear Specific cells

I have been tasked by my company to make an update to our sheet. The script needs to clear certain cells in a row based on the value of another cell. I have managed to find a script that deletes the whole required row, however that causes an issue with the rest of the document.
function deleteRows() {
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[14] == '' && row[6] == 'PREBOOKED') { // This searches all cells in columns A (change to row[1] for columns B and so on) and deletes row if cell is empty or has value 'delete'.
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
Is it possible to modify this to instead of deleting the row it just clears the content of the cells D - P?
You can use below code with modifications if necessary.
function deleteRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var values = sheet.getDataRange().getValues();
values.forEach(function(v, i) {
// get row index
var row = i+1;
// look for required values in cells
if (v[14] == '' && v[6] == 'PREBOOKED') {
// might have to modify this line based on Sheets API
sheet.getRange('D'+row+':P'+row).clearContent();
}
});
}
i was able to do what i need with the following:
function deleteRows() {
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[14] == '' && row[6] == 'PREBOOKED') { // This searches all cells in columns A (change to row[1] for columns B and so on) and deletes row if cell is empty or has value 'delete'.
sheet.getRange((parseInt(i)+1), 6).clearContent();
sheet.getRange((parseInt(i)+1), 7).clearContent();
sheet.getRange((parseInt(i)+1), 10).clearContent();
sheet.getRange((parseInt(i)+1), 11).clearContent();
sheet.getRange((parseInt(i)+1), 12).clearContent();
rowsDeleted++;
}
}
};
thanks for your ideas folks!

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);
}
}
}