Set Value Based on Another Cell's Value in the Same Row - google-apps-script

New to writing Google scripts. Have found similar questions but none with answers that I could make work. I want the value of the cell in column "H" of a given row on sheet "main" to be set to "Y" when the cell in column "G" of the same row is edited to be "y". I can not use a formula for this, as I need the values in "H" to remain after those in "G" are deleted. Current code I am working with is below. Returns no errors but also does not change anything in the sheet. It is not written as onEdit because I am using a separate onEdit function to call multiple functions (including this one when it is correct).
function myFunction3() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("main");
var range = sheet.getActiveCell();
var columnNumberToWatch = 7; // column A = 1, B = 2, etc.
var valueToWatch = "y";
var right = range.offset(0,1);
if (range.getColumn() == columnNumberToWatch && range.getValue() ==
valueToWatch) {
sheet.getRange(right).setValue('Y');
}
}

Try this -
function myFunction3() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('main');
var range = sheet.getActiveCell();
var columnNumberToWatch = 7; // column A = 1, B = 2, etc.
var valueToWatch = 'y';
if (range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
range.offset(0, 1).setValue('Y');
}
}

In your description you seem to be saying that if the value in column G is "y" then you want to make the value in column "H" equal to "Y". The range = activeCell(), column number is 7, and valueToWatch = "y".
So what you need to do is get the value in range (e.g. the active cell) and change the value in the cell next to it to "Y".
So your conditional statement needs to start with the thing you are testing (e.g. range.getValue() and if that value is in the columnToWatch then set the value in the column next to it to "Y".
First thing you need to so is to get the column number of the active cell.
try range.getColumn() and assign it to a new variable (i.e. activeColumnNumber)
then start your if statement with range.getValue().
if (range.getValue == 'y' && activeColumnNumber == 7) etc., etc. it should work.

Related

How can I move a column to a new sheet once row 1 has a certain value?

I am using the code posted here that moves a row based on the value of a column, but I want to edit it so that it does the opposite: moves a column to a new sheet based on the value of a row.
This is what I have so far:
function onEdit(event) {
// source data in sheet named Blocks
// sheet column is moved to named Ordered
// test row with word "Done" is row 1
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Blocks" && r.getRow() == 1 && r.getValue() == "Done") {
var column = r.getColumn();
var numRows = s.getLastRow();
var targetSheet = ss.getSheetByName("Ordered");
var target = targetSheet.getRange(targetSheet.getLastColumn() + 1, 1);
s.getRange(column, 1, 1, numRows).copyTo(target);
s.deleteColumn(column);
}
}
The problem is, when I tested it, it only moved the first row of that column. I want it to move the entire column and then delete it from the original sheet.
Can anyone tell me where I went wrong?
Moving Selected Columns
function onEdit(e) {
//e.source.toast('Entry');
var sh = e.range.getSheet();
if(sh.getName() == "Blocks" && e.range.rowStart == 1 && e.value == "Done") {
//e.source.toast('Ordered1')
var tsh = e.source.getSheetByName("Ordered");
var target = tsh.getRange(1,tsh.getLastColumn() + 1);
sh.getRange(2, e.range.columnStart, sh.getLastRow() -1).copyTo(target);
sh.deleteColumn(e.range.columnStart);
}
}
I used Active/Done datavalidation on row one and I only moved from row two to last row and then deleted the entire column including the validated cell.

How to create a Google apps script array list of sheet names and use it in if statement?

I am trying to get my Google sheet to fill in the date in column A when I fill in column B and have that function work on multiple sheets within the spreadsheet.
So far I have a working script that works on my first sheet. I have tried several things like making an array out of the variable (but that makes only the last array name work), trying an OR statement with 2 variables (but that fucked up the column it should watch), ...
Can you guys help me with this?
this is the one that works for 1 sheet name:
function onEdit() {
// writes the current date to the cell to the right on the row when a cell in a specific column is edited
// adjust the following variables to fit your needs
var sheetNameToWatch = "2021";
var columnNumberToWatch = /* column A */ 2; // column A = 1, B = 2, etc.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
var val=sheet.getActiveCell().getValue()
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && val!= "" /* I changed this var value so if I input anything, it will update the date */) {
var targetCell = sheet.getRange(range.getRow(), range.getColumn()-1 /* changed this to "-1" so it will input to the left column */
);
targetCell.setValue("" + Utilities.formatDate(new Date(), "CET", "dd/MM/yyyy"));
}
}
Demo can be found here: https://docs.google.com/spreadsheets/d/1mLBJrx3VCCwtcfUk-q_1DhwZFdA1cdrqFcySfC0cAi0/edit?usp=sharing
Just a minor edit on the code you provided:
function onEdit() {
// writes the current date to the cell to the right on the row when a
cell in a specific column is edited
// adjust the following variables to fit your needs
var columnNumberToWatch = /* column A */ 2; // column A = 1, B = 2, etc.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
var val=sheet.getActiveCell().getValue()
if (range.getColumn() ==
columnNumberToWatch && val!= "") {
var targetCell = sheet.getRange(range.getRow(), range.getColumn()-1
);
targetCell.setValue("" + Utilities.formatDate(new Date(), "CET", "dd/M
M/yyyy"));
}
}
The following code snippet has been removed:
var sheetNameToWatch = "2021";
sheet.getName() == sheetNameToWatch &&
Technically your code is already good, we just need to remove the condition based on your IF Statement since SpreadsheetApp.getActiveSheet(); will detect the current sheet you are working on.

Transferring (Cut and Paste) Rows from One sheet to another based on a cell value dependent on another cell in Google Sheets

So I have a Google Sheet which has two sheets 'Enquiry' and 'Follow up'. I want a script that automatically transfers rows whose Zth column contains the word 'Complete' from the sheet 'Enquiry' to the 'Follow up' sheet. By transfer I mean rows should be deleted from 'Enquiry' and pasted in Follow up'. Also the Zth column in 'Enquiry' is dependent on the value of the column U of 'Enquiry'. I've put a condition in Z that if U = condition, then Z="Complete".
I want this to be done automatically and I don't have to run the function again and again.
So basically how it would work is,
Column U is updated in 'Enquiry'. If condition matches, column Z in 'Enquiry would become "Complete". Then this row should be deleted from here and pasted in the sheet 'Follow up'.
I've been using this code:
function onEdit() {
var sheetNameToWatch = "ENQUIRY";
var columnNumberToWatch = 26;
var valueToWatch = "Complete";
var sheetNameToMoveTheRowTo = "Follow up";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(range.getRow());
}
}
But this only works when I type in Complete in the column Z of Enquiry sheet and enter. Not how I'd like it to work. Please help

If AND statements in google script to move data to another sheet based on change in one cell and value in another

OK, I'm fairly new at doing this. I have a google spreadsheet that I populate and then run a script to create forms are created to complete tasks. My end users mark the task as completed. I am wanting entries in the spreadsheet that change to yes AND are Validate tasks to copy to another sheet (Log). I can get the entries to copy if I just have a watcher for yes but am having problems putting the other if statement for the task in there. Here is what I have for a test...
function onChange() {
// moves a row from any sheet to an archive sheet when a magic value is entered in a column
var columnNumberToWatch = /* column D */ 4; // column A = 1, B = 2, etc.
var valueToWatch = "yes";
var sheetNameToMoveTheRowTo = "LOG";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet2");
var cell = sheet.getActiveCell();
if ( sheet.getName() != sheetNameToMoveTheRowTo && cell.getColumn() == columnNumberToWatch
&& cell.getValue().toLowerCase() == valueToWatch) {
if (cell.getColumn() == 2 && cell.getValue() == "Validate") {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(cell.getRow(), 1, 1, sheet.getLastColumn())
.copyTo(targetRange);
}
}
}
It looks like you are trying to watch two separate locations with the same cell variable. You start by initializing cell as the active cell, then check that the column is #4, then in the second if statement you use cell to getColumn 2.
Maybe create a new set of variables to use for the validation check:
var activeRow=cell.getRow();
var valCheck=sheet.getRange(activeRow, 2);
then the if statement changes to:
if (valCheck.getValue() == "Validate") {

GoogleScript: How to CopyRow before Delete the same

Situation:
I find this script, that delete the rows when the text in the column B change to "Closed". I want to copy the row before to delete into other sheet called historic.
Action:
The script should be copy the row when the the status is "Closed" in other sheet called "Historic" in the same spreadsheeet.
Then when the first point is completed should be delete the rows in the activesheet.
SCRIPT DELETE ROWS:
function DeleteRow(e) {
try {
var ss = e.source; // Just pull the spreadsheet object from the one already being passed to onEdit
var s = ss.getActiveSheet();
// Conditions are by sheet and a single cell in a certain column
if (s.getName() == 'ISP1' && // change to your own
e.range.columnStart == 2 && e.range.columnEnd == 2 && // only look at edits happening in col 2 which is 2
e.range.rowStart == e.range.rowEnd ) { // only look at single row edits which will equal a single cell
checkCellValue(e);
}
} catch (error) { Logger.log(error); }
};
function checkCellValue(e) {
if ( e.value == 'Closed') { // Delete if value is zero or empty
e.source.getActiveSheet().deleteRow(e.range.rowStart);
}
}
SCRIPT HISTORIC:
This script when somone update the column 5 will copy the row to other sheet called "historic".
When somone change the column 9 and select the status "Closed" the script copy the row to the historic and insert the closed date.
Now, does exist any why to do the same and improve the perfomance? I think this script is very complex and use many line.. I don't know if possible do the same better.
function Historic(e) { //CopyRows
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var r = SpreadsheetApp.getActiveRange();
var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm"); // Function Date + Format
// Get the row and column of the active cell.
var rowIndex = r.getRowIndex();
var colIndex = r.getColumnIndex();
// Get the number of columns in the active sheet.
var colNumber = s.getLastColumn();
// Move row based on criteria in column 5, and if row is not the header.
if (colIndex == 5 && rowIndex != 1) {
// Get value from column 5, in the active row.
var status = s.getRange(rowIndex, colIndex).getValue();
// Do nothing if criteria value is not actually changed to something else.
if (s.getName() != 'Historic') {
// The target sheet is the one with the same name as the criteria value.
var targetSheet = ss.getSheetByName('Historic');
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(rowIndex, 1, 1, colNumber).copyTo(target);
//s.deleteRow(rowIndex); // This line delete the source row
targetSheet.getRange(targetSheet.getLastRow(), 11).setValue(s.getSheetName()); //Insert in the Column 11 the SheetName where was modify that line
}
}
else if (colIndex == 9 && rowIndex != 1 && e.value == 'Closed' ) {
// Get value from column 5, in the active row.
var status = s.getRange(rowIndex, colIndex).getValue();
// Do nothing if criteria value is not actually changed to something else.
if (s.getName() != 'Historic') {
// The target sheet is the one with the same name as the criteria value.
var targetSheet = ss.getSheetByName('Historic');
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(rowIndex, 1, 1, colNumber).copyTo(target);
targetSheet.getRange(targetSheet.getLastRow(),10).setValue(date); //This Line Insert Close Date when the user select option Close
targetSheet.getRange(targetSheet.getLastRow(),11).setValue(s.getSheetName()); //Insert in the Column 11 the SheetName where was modify that line
e.source.getActiveSheet().deleteRow(e.range.rowStart); // This line delete the source row
}
}
}
I see somone vote this question -1. I'm using the Historic the second part, this part copy and then delete the row.
If anyone have a better idea, I will appreciate your help.
Thanks So much