I have 3 sheets. Example, my main data is in Sheet1 if I input a data on Row 14 which is the ACTIVEROW then in that column let says D14, contains "N/A" or "1" value I want to copy that whole row data and paste to Sheet2 ELSE it paste on Sheet3 to the lastrow first empty cell.
enter image description here
https://docs.google.com/spreadsheets/d/1CWqKF6Ta7_Tdtzzx47z7TZjzdGWPmi_tFbsRtdEWU3k/edit#gid=2100307022
Sorry for the lack of information. I provide an example of sheet or image above for additional info. Example, I want to input new data in Sheet 1 if the value in the cell is "Male" copy the whole row and paste it to the Sheet!Male else if the value is "Female" it will paste it to the Sheet!Female.
function onEdit(e) {
const sh = e.range.getSheet();
if(sh.getName() == "Sheet1" && e.range.columnStart == 14) {
let vs = sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).getValues();
if(e.value == "N/A" || e.value == 1) {
let dsh = e.source.getSheetByName("Sheet2");
dsh.getRange(r.range.rowStart,1,1,vs[0].length).setValues(vs)
} else {
let dsh = e.source.getSheetByName("Sheet3");
dsh.getRange(dsh.getLastRow() + 1,1,1,vs[0].length).setValues(vs);
}
}
}
Related
I am trying to create a drop down list in google sheet using google app script.
Sheet Link: https://docs.google.com/spreadsheets/d/1TESZNYgSJC-ye8r9CcW57-Vt1yU5VcvELiJw4oRwAZQ/edit#gid=0
In this sheet, in column B there is responsible person. Whenever I will select any name from the drop down list of this column, the another drop down list will create in column C and in column D the current date will appear. For example, If I select 'Anushka' in cell B2, a drop down list 'New', 'on going', 'done', 'on hold' will create in cell C2 with the first value from the list of the drop down and a current date will appear in cell D2.
Till now, I can create the drop down menu in cell C2 and also current date in cell D2 based one cell B2. But I don't know how to show always the first item from the drop down list in cell C2. Now it's giving me blank cell and I need to select something from the menu but I want in a way that when drop down menu will create, it will show the first item not empty cell.
I have attached the code also.
Can anyone please help me where I should modify?
var sheet = event.source.getActiveSheet();
var colrange=sheet.getRange("B2:B").isBlank()
var actRng = event.source.getActiveRange();
var index = actRng.getRowIndex();
var cindex = actRng.getColumnIndex();
if(colrange){
sheet.getRange(index,4).clearContent()
sheet.getRange(index,3).clearDataValidations()
} else{
var date = Utilities.formatDate(new Date(),"EST", "MMMM-dd-yyyy");
var list=["New", "Ongoing", "Done", "On-Hold"]
var rule=SpreadsheetApp.newDataValidation().requireValueInList(list).build()
sheet.getRange(index,3).setDataValidation(rule)
sheet.getRange(index,4).setValue(date)
}
Custom Validations
function onEdit(e) {
e.source.toast("Entry");
const sh = e.range.getSheet();
if(sh.getName() == "Sheet0" && e.range.columnStart == 2 && e.value) {
e.source.toast("Flag1");
let rg = e.source.getRangeByName("Status");
e.range.offset(0,1).setDataValidation(SpreadsheetApp.newDataValidation().requireValueInRange(rg));
e.range.offset(0,2).setValue(new Date());
Logger.log(e.source.getRangeByName("Status").getValues()[0][0])
e.range.offset(0,1).setValue(e.source.getRangeByName("Status").getValues()[0][0]);
}
}
Demo:
Named Ranges:
In answer to your comment below:
function onEdit(e) {
//e.source.toast("Entry");
const sh = e.range.getSheet();
if(sh.getName() == "Sheet0" && e.range.columnStart == 2 && e.value) {
//e.source.toast("Flag1");
let rg = e.source.getRangeByName("Status");
e.range.offset(0,1).setDataValidation(SpreadsheetApp.newDataValidation().requireValueInRange(rg));
e.range.offset(0,2).setValue(new Date());
//Logger.log(e.source.getRangeByName("Status").getValues()[0][0])
e.range.offset(0,1).setValue(e.source.getRangeByName("Status").getValues()[0][0]);
}
if(sh.getName() == "Sheet0" && e.range.columnStart == 2 && !e.value) {
//e.source.toast("Flag2");
let rg = e.source.getRangeByName("Status");
e.range.offset(0,1).clearDataValidations();
e.range.offset(0,2).setValue('');
}
}
Here is the script I have so far:
function onEdit(e) {
var row = e.range.getRow();
var col = e.range.getColumn();
e.source.getActiveSheet().getRange(row,30).setValue(new Date())
}
The issue is I need to be able to edit certain fields without it triggering a new date. There's a clean range of cells that I need the script to work (I5:O60) on but can't figure it out.
Try this:
function onEdit(e) {
const sh = e.range.getSheet();
if(sh.getName() == "Enter your sheet name" && e.range.rowStart > "some row number") {
e.range.offset(0,"enter number of column you want the date in").setValue(new Date());
}
}
The goal:
Get the edited checkbox
Get all checkboxes in range (column 2–7, same row as edited checkbox)
If all checkboxes return "TRUE", set backgrounds on range (column 1–7, same row as edited checkbox)
Seems like the code do nothing.
function onEdit(event) {
var sheet = event.source.getActiveSheet(), //get edited sheet
row = event.range.getRow(), // get row needed
column = 2, // set the starting column
test,
userValues = [];
while (column<=7) {
if (sheet.getValue(row,column) == "TRUE") {
test = true
} else {
test = false;
break;
}
column++;
}
if (test) {
sheet.getRange(row,1,1,7).setBackgrounds("#b6d7a8");
}
}
I believe your goal is as follows.
You have the checkboxes to the columns "B" to "G".
When all checkboxes from the columns "B" to "G" are checked, you want to set the background color of the columns "A" to "G" of the same row to #b6d7a8.
In your situation, I guessed that when all checkboxes are not checked, you might want to set the background color to the default color.
When I saw your script, getValue is used in the loop. In this case, the process cost will be high. And, when you use sheet.getRange(row,1,1,7).setBackgrounds("#b6d7a8"), you can use sheet.getRange(row,1,1,7).setBackground("#b6d7a8").
In this case, how about the following modification?
Modified script:
function onEdit(event) {
var sheetName = "Sheet1"; // Please set the sheet name.
var range = event.range;
var sheet = event.source.getActiveSheet();
if (sheet.getSheetName() != sheetName || range.columnStart < 2 || range.columnStart > 7) return;
var r = sheet.getRange(range.rowStart, 1, 1, 7)
var checks = r.offset(0, 1, 1, 6).getValues()[0].every(e => e === true);
r.setBackground(checks ? "#b6d7a8" : null);
}
When you use this script, please check the checkboxes the columns "B" to "G". When all checkboxes from the columns "B" to "G" are checked, the background color of columns "A" to "G" is changed to #b6d7a8.
Note:
This script is run by the OnEdit trigger. So please edit the cells of checkboxes from the columns "B" to "G". By this, the script is run. When you directly run this script with the script editor, an error occurs. Please be careful about this.
When you are not required to check the sheet name, you can also use the following script.
function onEdit(event) {
var range = event.range;
var sheet = event.source.getActiveSheet();
if (range.columnStart < 2 || range.columnStart > 7) return;
var r = sheet.getRange(range.rowStart, 1, 1, 7)
var checks = r.offset(0, 1, 1, 6).getValues()[0].every(e => e === true);
r.setBackground(checks ? "#b6d7a8" : null);
}
References:
every()
setBackground(color)
Is there a way to format a checkbox so that when it is "checked", The data in the column beside it will be moved to another column?
function onEdit(e) {
const sh = e.range.getSheet();
if (sh.getName() == 'Sheet1' && e.range.columnStart == 1 && e.value == 'TRUE') {
e.range.offset(0, 5).setValue(e.range.offset(0, 1).getValue());
e.range.offset(0, 1).setValue('');
e.range.setValue('FALSE');
}
}
As player0 mentioned, for this to work, you need Google Apps Script's Spreadsheet Service with the help of Event Objects.
The code below should work on the sample data below.
Sample data:
Code:
function onEdit(e) {
var sheet = e.source.getActiveSheet();
var range = e.range;
var column = range.getColumn();
// if edited data is in Sheet1 Column A and having value TRUE (checkbox ticked)
if (sheet.getName() == "Sheet1" && column == 1 && e.value == "TRUE") {
// get 1 column to the right of checkbox and assign to dataValue
var dataRange = range.offset(0, 1);
var dataValue = dataRange.getValue();
// get 3 columns to the right of dataValue and assign to transferToRange
var transferToRange = dataRange.offset(0, 3);
// set dataValue as value to transferToRange
transferToRange.setValue(dataValue);
// remove data from dataRange after
dataRange.setValue("");
}
}
Output:
Note:
Above code is just one of the many possible solutions you can create via Apps Script.
Read Apps Script for more information.
References:
Spreadsheet Service
Event Objects
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