Copy Row if Cell Contains X to Different Sheet then delete row. - google-apps-script

I have been searching all day trying to find out how to do this and get close but never to a finished state.
What I am trying to do is on active spreadsheet, look at Sheet "Happy". If a cell in Colum G contains "Closed" I want to copy the cells between range A:K for that row to sheet "Sad". Once the copy is done and the data is moved I want to then delete the whole row on sheet "Happy" that the data was copied from.
IF cell = closed, than copy to sheet and delete, else nothing.
I will run this script every 5 min which I can do with triggers.
Any help would be greatly appreciated.
Here is what I tried so far
function Copy() {
var sss = SpreadsheetApp.getActive()
var ss = sss.getSheetByName('Happy');
var range = ss.getRange('A:k');
var data = range.getValues();
var tss = SpreadsheetApp.getActive();
var ts = tss.getSheetByName('Sad:');
var valuesToCopy = ss.getRange(2,2,100).getValues();
ts.getRange(2,ts.getLastRows()+1,valuesToCopy.length,1).setValues(valuesToCopy);

function copyrange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Happy'); //source sheet
var testrange = sheet.getRange('G:G');
var testvalue = (testrange.getValues());
var csh = ss.getSheetByName('Sad'); //destination sheet
var data = [];
var j =[];
//Condition check in G:G; If true copy the same row to data array
for (i=0; i<testvalue.length;i++) {
if ( testvalue[i] == 'Closed') {
data.push.apply(data,sheet.getRange(i+1,1,1,11).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
//Copy data array to destination sheet
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
//Delete matched rows in source sheet
for (i=0;i<j.length;i++){
var k = j[i]+1;
sheet.deleteRow(k);
//Alter j to account for deleted rows
if (!(i == j.length-1)) {
j[i+1] = j[i+1]-i-1;
}
}
}

Related

Getting Specific Rows from Another Google Sheet via App Script Based on Multiple Cell Values

I would like to fetch data from another google sheet, then retrieve all columns(been successful with this one). As for the rows, I would only like to retrieve all rows with these values, "Mrm1, Mrm2, Mrm3" from Measurement Column.
For example, I have these on another sheet,Original Data
And on another sheet, I want the result to be like this: Expected Retrieved Data
So far, I have this, but I'm not sure how to apply what I need on the rows. This script is retrieving everything.
function Country A() {
var sss = SpreadsheetApp.openById("19-idD8PWuNxnvzRVqBlKhju2RkKxOe8nUQaf9ZMAcgk"); // Source spreadsheet - replace with actual key
var ss = sss.getSheetByName('Compiled'); // Source sheet - change to actual name
var numRows = ss.getLastRow() // get last row with data
var numColumn = ss.getLastColumn(); // get last column with data
var srange = ss.getRange(2,1,numRows,numColumn); // Source range - change to actual range
var values = srange.getDisplayValues();
var lss = SpreadsheetApp.getActiveSpreadsheet(); // Local spreadsheet - the one this script is in
var targetnumRows = lss.getSheetByName('Country A').getMaxRows();
var targetnumcolumns = lss.getSheetByName('Country A').getMaxColumns();
var ls = lss.getSheetByName('Country A').getRange(2,1,numRows,numColumn); // Local sheet - change to actual name and Local range - change to actual range
var TargetRange = lss.getSheetByName('Country A').getRange(2,1,targetnumRows,targetnumcolumns); // Local sheet - change to actual name and Local range - change to actual range
TargetRange.clear() //Clear Sheet Content
ls.setValues(values); // Copy from source sheet to local sheet
var sheet = lss.getSheetByName('Country A');
var maxColumns = sheet.getMaxColumns();
var lastColumn = sheet.getLastColumn();
var maxRows = sheet.getMaxRows();
var lastRow = sheet.getLastRow();
if (maxColumns-lastColumn != 0){sheet.deleteColumns(lastColumn+1, maxColumns-lastColumn)};
if (maxRows-lastRow != 0){sheet.deleteRows(lastRow+1, maxRows-lastRow)};
var TargetHour = lss.getSheetByName('Country A').getRange(1,1).setValue(Utilities.formatDate(new Date(), "CST", "MM-dd-yyyy HH:mm:ss"));
}
Please help me. Thank you.
The script stores the values in a 2D array. You have to iterate through the elements and delete the elements where column E (Col number 4) (A=0, B=1,...E=4) is not equal to "Mrm1, Mrm2, Mrm3".
Then you paste the modified array in the second sheet.
Here is an example. You need to adapt it to your code.
function copyData() {
var ss = SpreadsheetApp.openById("YOUR-ID");
// Stores data as a 2D Array
var data = ss.getSheetByName("Sheet1").getRange("A1:M10").getValues();
// Remove rows where column E not equals "Mrm1, Mrm2, Mrm3"
for (var i = 0; i < data.length; i++) {
if (data[i][4] !== "Mrm1" && data[i][4] !== "Mrm2" && data[i][4] !== "Mrm3") {
data.splice(i,1);
i--;
}
}
// Paste data in sheet 2
var pasteRange = "A1:M" + (data.length);
ss.getSheetByName("Sheet2").getRange(pasteRange).setValues(data);
}

TypeError: Cannot read property "length" from undefined. (line 20, file "Move")

Running into the above message when trying to run the script below.
My objective is to have each new row from "validated" sheet to be duplicated to the next sheet "processing" once the user has selected "approve" from the dropdown in column L. And also delete from Source(validated) spreadsheet once it's moved over to "processing"
function copyrange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('VALIDATED'); //source sheet
var testrange = sheet.getRange('L:L'); //range to check
var testvalue = (testrange.getValues());
var csh = ss.getSheetByName('PROCESSING'); //destination sheet
var data = [];
var j =[];
//Condition check in L:L; If true copy the same row to data array
for (i=0; i<testvalue.length;i++) {
if ( testvalue[i] == 'Approved') {
data.push.apply(data,sheet.getRange(i+1,1,1,25).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
//Copy data array to destination sheet
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
//Delete matched rows in the source sheet
for (i=0;i<j.length;i++){
var k = j[i]+1;
sheet.deleteRow(k);
//Alter j to account for deleted rows
if (!(i == j.length-1)) {
j[i+1] = j[i+1]-i-1;
}
}
}

How to copy (and then delete) a row from sheet x to sheet y based on value in column 1?

I am working in Google Sheets and trying to set up an Apps Script macro that
Looks for rows in worksheet "Sheet1" that have the value "True" in column "A"
Copies this row and pastes it into the row below the last filled row in worksheet "Sheet2"
Deletes the row in "Sheet1"
I've been trying the code from another post on the forum but I keep getting: TypeError: Cannot read property "length" from undefined. (line 20, file "Code")
Any help would be much appreciated
function copyrange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1"); //source sheet
var testrange = sheet.getRange("A2:AE990");
var testvalue = (testrange.getValues());
var csh = ss.getSheetByName("Sheet2"); //destination sheet
var data = [];
var j =[];
//Condition check in G:G; If true copy the same row to data array
for (i=0; i<testvalue.length;i++) {
if ( testvalue[i] == "TRUE") {
data.push.apply(data,sheet.getRange(i+1,1,1,11).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
//Copy data array to destination sheet
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
//Delete matched rows in source sheet
for (i=0;i<j.length;i++){
var k = j[i]+1;
sheet.deleteRow(k);
//Alter j to account for deleted rows
if (!(i == j.length-1)) {
j[i+1] = j[i+1]-i-1;
}
}
}
Try this:
function moveRows() {
var ss=SpreadsheetApp.getActive();
var ssh=ss.getSheetByName('Sheet1');
var dsh=ss.getSheetByName('Sheet2');
var srg=ssh.getDataRange();//You might want to specify a more unique range. This just gets all of the data on the sheet
var svA=srg.getValues();
var d=0;//deleted row counter
//I assume one row for a header
for(var i=1;i<svA.length;i++) {
if(svA[i][0].toString().toLowerCase()=='true') {
dsh.appendRow(svA[i]);//append entire row to Sheet2
ssh.deleteRow(i-d+1);//accounts for the difference between length of array and number of remaining row.
d++;
}
}
}

How to get and set column number based on value in source spreadsheet

Trying to copy a range (L2:L9) to another spreadsheet based on a data validation selection (ID #) in a specific cell (G11) on the source spreadsheet.
The ID#s are all in alphanumeric order on the target spreadsheet along row 5, so all I need is to get the column number that corresponds to the choice made in the data validation field.
Before I had to convert the ID#s to Alphanumeric values, I simply had numeric values and the script worked fine. Now I'm having trouble converting the script to work with this modification.
function Submit() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); // ss = source spreadsheet
var source_sheet = ss.getActiveSheet();
if (source_sheet.getName() == "Pre-Season CC") {
var SRange = source_sheet.getRange('L2:L9');
var A1Range = SRange.getA1Notation();
var SData = SRange.getValues();
var target = SpreadsheetApp.openById('1MpKdxFyBrVQT3EumePQvtCZpgzcDW5xlYe4B1DKZCA8');
var target_sheet = target.getSheetByName('Pre-Season');
var idNumber = source_sheet.getRange('Pre-Season CC!G11').getValue();
for (var i = 0; i < idNumber.length; i++) {
for (var j = 0; j < idNumber.length;j++){
if (target_sheet[4][j] == idNumber){
Logger.log((j+1))
return j+1;
target_sheet.getRange('B6:B13').offset(0,valueA1 = ss.getRange('Pre-Season!G11').getValue(idNumber)+1).setValues(SData);
}
}
}
}
At that point, I need the data copied from source spreadsheet onto the target spreadsheet in it's designated column, according to the ID#. Please help!
Try below code.
function Submit() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); // ss = source spreadsheet
var source_sheet = ss.getActiveSheet();
if (source_sheet.getName() == 'Pre-Season CC') {
// source sheet items
var SData = source_sheet.getRange('L2:L9').getValues();
var idNumber = source_sheet.getRange('Pre-Season CC!G11').getValue();
// target sheet items
var targetSS = SpreadsheetApp.openById('1MpKdxFyBrVQT3EumePQvtCZpgzcDW5xlYe4B1DKZCA8');
var target_sheet = targetSS.getSheetByName('Pre-Season');
// 5 = row where IDs are
var TData = target_sheet.getRange(5, 1, 1, target_sheet.getLastColumn()).getValues()[0];
// column index of #ID in target sheet
var colIndex = TData.indexOf(idNumber) + 1;
// write data in that column, rows from 6 to 13
target_sheet.getRange(6, colIndex, SData.length, 1).setValues(SData);
}
}

Trying to copy a column range on on sheet and paste it in a row on a different sheet and have it inserted to top with each time the script runs

I am trying to copy a column range on on sheet and paste it in a row on a different sheet. I want it inserted to the top each time the script runs. This is what I got so far:
function Copy() {
var sss = SpreadsheetApp.openById('1UbcIcGJRVxsX9WbzunS96Ijf8c2gRc8UYb40lHpWWQU'); //source ID
var ss = sss.getSheetByName('Container Input'); //source Sheet tab name
var range = ss.getRange('B4:B23'); //assigned range to copy
var data = range.getValues();
var tss = SpreadsheetApp.openById('1UbcIcGJRVxsX9WbzunS96Ijf8c2gRc8UYb40lHpWWQU'); //destination ID
var ts = tss.getSheetByName('Container Log'); //destination sheet tab name
ts.getRange(1, 1, data.length, data[0].length).setValues(data); //you will need to define the size of the copied data see getRange()
//range.clearContent(); //clears var range
}
function Copy()
{
var sss = SpreadsheetApp.openById('1UbcIcGJRVxsX9WbzunS96Ijf8c2gRc8UYb40lHpWWQU');
//source ID
var ss = sss.getSheetByName('Container Input'); //source Sheet tab name
var ts = sss.getSheetByName('Cylinder Tracking'); //destination sheet tab name
//var protection = ts.protect();
//protection.setUnprotectedRanges(2,1,1,21);
ts.insertRowsBefore(2, 1);// Adds a new row before row 2 - Keeps the formatting of the
old row 2 for the new row 2
var source_C = 2;
var source_R = 3;
var dest_C = 2;
var dest_R = 2;
SpreadsheetApp.getActive().getRange('Cylinder Tracking!A2').setValue(new Date())
for (var counter = 1; counter <= 20; counter++)
{
var source_range = ss.getRange(source_R,source_C); //assigned range to copy
var data = source_range.getValues();
var dest_range = ss.getRange(dest_C,dest_R);
ts.getRange(dest_R, dest_C).setValues(data); //you will need to define the size of the
copied data see getRange()
source_R++;
dest_C++;
}
}