Deleting Row Based on Cell Values - editor

I am attempting to create a additional menu option that allow me to automatically delete all rows that contain the text "Complete" in Column D. The script I have runs and finishes with no errors but does not delete the rows. I tried to find help on line and tweaked the code but same result
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 'Complete';
for (var i = 'Complete'; i <= numRows - 1; i++) {
var row = values[i];
if (row[2] == 'Complete' || row[2] == '') {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Delete Rows where column shows Complete Text",
functionName : "readRows"
}];
sheet.addMenu("Script Center Menu", entries);
};

Related

keep Hyperlink when copying row using script

Certain cell in rows have hyperlink. need hyperlink to follow row when copied from sheet to sheet based on other cell value
function moveToTab() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var range = ss.getActiveRange();
var col = range.getColumn();
Logger.log(col);
var aSheet = ss.getActiveSheet();
var header = aSheet.getRange(10,col,1,1).getValue();
Logger.log(header);
if (header.toLowerCase() !== 'issued to') return;
var target = range.getValue();
if (target.toString().trim() === '') return;
if (target.toString().toLowerCase() === aSheet.getName().toLowerCase()) return;
var sheets = ss.getSheets();
var tSheet = sheets.filter(function(sheet) {
if (sheet.getName().toLowerCase() == target.toLowerCase()) return true;
else return false;
})[0];
if (!tSheet) return;
var row = range.getRow();
var values = aSheet.getRange(row, 1, 1, aSheet.getLastColumn()).getValues();
tSheet.appendRow(values[0]);
aSheet.deleteRow(row);
I know I am missing something simple. In my work book column "F" has a hyperlink to files. I am using the above script to move certain rows from sheet to sheet based on value in Column "K" and would like the hyper link in the cell in column "F" to follow teh row from sheet to sheet. What am i missing

How to make a cell in a row show date of most recent edit within that row

I'm working in Google Sheets on what is going to be a regularly accessed and edited document. What I would like is to have a column labelled "Date of Last Edit" and for each cell in that column to reflect the date of each cell row most recent edit. For example, if I edit info in cell C3 then I want the cell in column "Date of Last Edit" Row 3 to reflect the date I made the edit. I've found a few that are run by column but they only work for an individual column. I need it to cover all cells in a row that fall in front of "Date of Last Edit."
add this script to your sheet and change format and time zone if you need so ("GMT+1", "dd.MM.yyyy"):
function onEdit(event)
{
var sheet = event.source.getActiveSheet();
// note: actRng = the cell being updated
var actRng = event.source.getActiveRange();
var index = actRng.getRowIndex();
var cindex = actRng.getColumnIndex();
var dateCol = sheet.getLastColumn();
var lastCell = sheet.getRange(index,dateCol);
var date = Utilities.formatDate(new Date(), "GMT+1", "dd.MM.yyyy");
lastCell.setValue(date);
}
This code will update C:C with the email address of the user, and D:D with the current time of the same row if A:B are edited on the Data Entry tab. You'll need to set up an onChange() trigger to get it to work.
function userUpdate() {
/*
Changed the updated by/on to make it faster
Put in handling to make sure that if they delete a cell, that it only clears out the submitted by/on if ALL the values in A:B are blank
*/
// Main sheet details
var s = SpreadsheetApp.getActiveSheet();
var spreadsheetTimeZone = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
var lastUpdatedString = Utilities.formatDate(new Date(), spreadsheetTimeZone, "MM/dd/yyyy' 'HH:mm:ss");
var sessionEmail = Session.getActiveUser().getEmail();
// Check Sheet
if (s.getName() == "Data Entry") { //checks that we're on the correct sheet
var r = s.getActiveCell();
var row = r.getRowIndex();
var column = r.getColumn();
var activeRange = s.getRange("A" + row + ":B" + row);
//var activeRangeValues =
var activeRangeValuesLength = activeRange.getValues().toString().length;
if (row > 1) {
// Declare variables
var cellValue = r.getValue().toString();
// If the cellValue IS blank
if (activeRangeValuesLength < 5) {
sessionEmail = "";
lastUpdatedString = "";
}
// Update Submitted By / Submitted On
if (column < 5) {
var lastUpdatedBy = s.getRange("C" + row)
lastUpdatedBy.setValue(sessionEmail); // update column C
var lastUpdated = s.getRange("D" + row)
lastUpdated.setValue(lastUpdatedString); // update column D
}
}
}

Wanting auto sort on 3 sheets, when one is locked

Please see the previous post on this question;
Auto Sort not working on Multiple Sheets within one Google Sheet
Here's the good script from that last session;
function onEdit(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = event.source.getActiveSheet().getName()
var editedCell = event.range.getSheet().getActiveCell();
if(sheet=="Loan Inquiries"){
var columnToSortBy = 2;
var tableRange = "A3:G99"; //range to be sorted
if(editedCell.getColumn() == columnToSortBy){
var range = ss.getActiveSheet().getRange(tableRange);
range.sort( { column : columnToSortBy, ascending: true } );
}}
else if(sheet=="Deals in Escrow"){
var columnToSortBy = 7;
var tableRange = "A3:I99"; //range to be sorted
if(editedCell.getColumn() == columnToSortBy){
var tableRange = "A3:I99"; //range to be sorted
var range = ss.getActiveSheet().getRange(tableRange);
range.sort( { column : columnToSortBy, ascending: true } );
}
else{return}
}}
function test_onEdit() {
onEdit({
user : Session.getActiveUser().getEmail(),
source : SpreadsheetApp.getActiveSpreadsheet(),
range : SpreadsheetApp.getActiveSpreadsheet().getActiveCell(),
value : SpreadsheetApp.getActiveSpreadsheet().getActiveCell().getValue(),
authMode : "LIMITED"
});
}
This thread successfully resolved my question, however, now what I want to do is to Auto Sort a 3rd Sheet, which is locked to others, but not to myself. The 3rd Sheet will be titled "Karlan Production for 2017". I also want this 3rd sheet to pull all new data from the 2nd Sheet "Deals in Escrow" and in the same formatting as the 2nd Sheet and to auto sort as the data is imported to the 3rd Sheet, but also be able for me to input new deals manually as well in the 3rd Sheet and still be able to auto update.
I believe this will do what you want:
function onEdit(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = event.source.getActiveSheet().getName()
var editedCell = event.range.getSheet().getActiveCell();
if(sheet=="Loan Inquiries"){
var columnToSortBy = 2;
var tableRange = "A3:G99"; //range to be sorted
if(editedCell.getColumn() == columnToSortBy){
var range = ss.getActiveSheet().getRange(tableRange);
range.sort( { column : columnToSortBy, ascending: true } );
}}
else if(sheet=="Deals in Escrow"){
var columnToSortBy = 7;
var tableRange = "A3:I99"; //range to be sorted
if(editedCell.getColumn() == columnToSortBy){
var range = ss.getActiveSheet().getRange(tableRange);
range.sort( { column : columnToSortBy, ascending: true } );
combineData()
}}
else if(sheet=="Karlan Production for 2017"){
var columnToSortBy = 7;
if(editedCell.getColumn() == columnToSortBy){
combineData()
}}
else{return}
}
function combineData(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s=ss.getSheets()[1]
var lr = s.getLastRow()
var s1=ss.getSheets()[2]
var lr1 = s1.getLastRow()
var lc1=s1.getLastColumn() //get the last column of 'Karlan Production for 2017'
var rng=s.getRange(3, 1, lr,lc1).getValues() //get 'Deals in Escrow' values
var rng1=s1.getRange(3, 1, lr1,lc1).getValues() //get 'Karlan Production for 2017' values
var rng2=[]
var rng2=rng.concat(rng1) //combine 'Deals in Escrow' and 'Karlan Production for 2017' values (all rows)
removeDuplicates(rng2) // call remove duplicates sending combined array
}
function removeDuplicates(data) {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var s1=ss.getSheets()[2]
var newData = new Array();
for(i in data){
var row = data[i];
var duplicate = false;
for(j in newData){
if(row.join() == newData[j].join()){
duplicate = true;
}
}
if(!duplicate){
newData.push(row);//create array of non duplicate rows
}
}
s1.clearContents(); //clear 'Karlan Production for 2017'
var sor=s1.getRange(3, 1, newData.length, newData[0].length).setValues(newData); //set new array
sor.sort( { column : 7, ascending: true } );//sort new data
}
After adding the new sheet, click the arrow on the sheet tab.
Click Protect Sheet. In the popup, enter a description and click
the Sheet button. Click the Set permissions button. In the Range
editing permissions popup click the arrow by 'Only you'. Select
'Custom' and add the email address of the other person you want
to be able to edit the sheet. Click done.

Hide columns in various sheets in Google Sheets on open

I have a few scripts which, individually, all work. However I am unable to get them to all run on opening the sheet. At the moment, only the first script (hideColumn) runs on opening the sheet. The others all work when called directly from the menu I managed to create.
function onOpen() {
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// create menu
var menu = [{name: "Hide Overview Columns", functionName: "hideColumn"},
{name: "Hide Rebalancing Columns", functionName: "hideColumn2"},
{name: "Add/Hide Dividend Data for Chart", functionName: "hideColumn3"}];
// add to menu
ss.addMenu("Check", menu);
// execute function
function hideColumn(){
function hideColumn(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Overview");
// get data
var data = sheet.getDataRange();
// get number of columns
var lastCol = data.getLastColumn()+1;
Logger.log(lastCol);
// itterate through columns
for(var i=1; i<lastCol; i++) {
if(data.getCell(1, i).getValue() == 'HIDE') {
sheet.hideColumns(i);
}
}
}
function hideColumn2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Rebalancing");
// get data
var data = sheet.getDataRange();
// get number of columns
var lastCol = data.getLastColumn()+1;
Logger.log(lastCol);
// itterate through columns
for(var i=1; i<lastCol; i++) {
if(data.getCell(2, i).getValue() == 'HIDE') {
sheet.hideColumns(i);
}
}
}
function showColumn3() {
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Dividends - Calculations");
// get data
var data = sheet.getDataRange();
// get number of columns
var lastCol = data.getLastColumn();
// show all columns
sheet.showColumns(1, lastCol);
}
function hideColumn3() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Dividends - Calculations");
// get data
var data = sheet.getDataRange();
// get number of columns
var lastCol = data.getLastColumn()+1;
Logger.log(lastCol);
// itterate through columns
for(var i=1; i<lastCol; i++) {
if(data.getCell(1, i).getValue() == 'HIDE') {
sheet.hideColumns(i);
}
}
}}}
we can help but won't code for you unless you showed us you tried!
Your best option is to have a look at the Google Apps Script documentation.
Here are the pages you need to check to do your script (you'll see it's well explained)
Create a menu
Hide a column
Get the content of a range
Create a loop to check several cell one by one in JS (Mozilla Doc)

I want to hide all rows in a google drive spreadsheet that do not have any value in the cells in Column A

I want to hide all rows in a google drive spreadsheet that do not have any value in the cells in Column A. Then be able to view them again when needed. What is the script/formula for this function? Thanks
UPDATED to include a menu to Show all Rows:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange(1, 1, sheet.getLastRow());
function onOpen() {
//add menu called Visibility onOpen
ss.addMenu("Visibility", [{
name: "Show All Rows", functionName: "showAllRows"
}]);
//get the values to those rows
var values = range.getValues();
//go through every row
for (var i=0; i<values.length; i++){
//if row value is equal to empty
if(values[i][0] === ""){
//hide that row
sheet.hideRows(i+1);
}
}
}
function showAllRows(){
sheet.showRows(1,sheet.getLastRow());
}
I've modified Blexy's menu to allow hiding of empty rows as a menu item rather than doing so during onOpen.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0]; //Sheet1 is in Array[0]
var range = sheet.getRange(1, 1, sheet.getLastRow());
function onOpen() {
//add menu called Visibility onOpen
ss.addMenu("Visibility",
[{
name: "Show All Rows", functionName: "showAllRows"
},
{
name: "Hide All Empty Rows", functionName: "hideAllEmptyRows"
}
]
);
}
function showAllRows(){
sheet.showRows(1,sheet.getLastRow());
}
function hideAllEmptyRows(){
//get the values to those rows
var values = range.getValues();
//go through every row
for (var i=0; i<values.length; i++){
//if row value is equal to empty
if(values[i][0] === ""){
//hide that row
sheet.hideRows(i+1);
}
}
}

Resources