GoogleSheet Script to Move to Sheet with same name as cell value - google-apps-script

I have a sheet with 6 columns. Row 1 is titles and each other sheet has the same titles and columns.
I have tried a couple codes like this that i found online with edits but can get it to correctly work for multiple sheets.
function copyrange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Source');
var testrange = sheet.getRange('F:F');
var testvalue = (testrange.getValues());
var csh = ss.getSheetByName('target');
var data = [];
var j =[];
for (i=0; i<testvalue.length;i++) {
if ( testvalue[i] == 'Completed') {
data.push.apply(data,sheet.getRange(i+1,1,1,25).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
for (i=0;i<j.length;i++){
var k = j[i]+1;
sheet.deleteRow(k);
if (!(i == j.length-1)) {
j[i+1] = j[i+1]-i-1;
}
}
Any Help would be greatly appreciated

Deleting Rows as you collect data that meet criteria
function moveRows() {
var ss=SpreadsheetApp.getActive();
var sheet=ss.getSheetByName('Source');
var vA=sheet.getRange(1,1,sheet.getLastRow(),sheet.getLastColumn()).getValues();
var csh=ss.getSheetByName('target');
var d=0;//deleted row counter
var data=[];
for(var i=0;i<vA.length;i++) {
if (vA[i][5]=='Completed') {
data.push(vA[i]);
sheet.deleteRow(i+1-d++);//increment deleted row counter as you delete rows
}
}
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}
Sheet getLastRow()
Sheet getLastColumn

Related

How to make this script run faster?

Hello so I am trying to get values from the 'book' sheet then copy it to 'Completed' sheet. It takes the values form book stores it in the data array and copy them to the 'Completed' sheet. Its working fine but very slow which jeopardizes my work in time-wise. How can I make this run faster ?
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var clear_sheet = spreadsheet.getSheetByName('Completed_Orders'); // clear the destination sheet first
clear_sheet.getRange('A2:X').clear();
var sheet = spreadsheet.getSheetByName('book'); //source sheet
var Datarange = sheet.getRange('Q3:Q'); //range to check
var Datavalue = (Datarange.getValues());
var dest = spreadsheet.getSheetByName('Completed_Orders'); //destination sheet
var data = [];
for (i=0; i<Datavalue.length;i++) {
if ( Datavalue[i] == "Completed") {
data.push.apply(data,sheet.getRange(i+3,1,1,24).getValues());
}
}
dest.getRange(2,1,data.length,data[0].length).setValues(data);
var column = dest.getRange('A3:A');
var values = column.getValues(); // get all data in one call
var ct = 0;
while ( values[ct][0] != "" ) { // to find the last row correctly, getLastRow is not working perfectly
ct++;
}
var endRow = ct+2;
}
Try this:
Don't use this syntax var values=sheet.getRange('A3:A').getValues() because it get's the data all the way down to getMaxRows(). Instead use var values=sheet.getRange(3,1,sheet.getLastRow()-2,1).getValues().
Also Datavalue[i] is a whole row
function myfunction() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var clear_sheet = spreadsheet.getSheetByName('Completed_Orders');
clear_sheet.getRange(2,1,clear_sheet.getLastRow()-1,24).clear();
var sheet = spreadsheet.getSheetByName('book'); //source sheet
var Datarange = sheet.getRange(3,17,sheet.getLastRow()-2,1); //range to check
var Datavalue = Datarange.getValues();
var dest = spreadsheet.getSheetByName('Completed_Orders'); //destination sheet
var data = [];
for (var i=0;i<Datavalue.length;i++) {
if (Datavalue[i]["************You need another index here**************"] == "Completed") { //need another index Datavalue is 2d
data.push(sheet.getRange(i+3,1,1,24).getValues());
}
}
dest.getRange(2,1,data.length,data[0].length).setValues(data);
}
I think this version will be a lot faster.
function myfunction() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var ssh=ss.getSheetByName('book');
var dsh=ss.getSheetByName('Completed_Orders');
dsh.getRange(2,1,dsh.getLastRow()-1,24).clear();
var dv=ssh.getRange(3,1,ssh.getLastRow()-2,24).getValues();//this version just gets this data one time so it should a lot faster.
var data=[];
for (var i=0;i<dv.length;i++) {
if (dv[i][16]=="Completed") {
data.push(dv[i]);
}
}
dsh.getRange(2,1,data.length,data[0].length).setValues(data);
}

Matching cell values in different Google Sheets

Users input text in cell A1 of sheet1. I want to find an exact match in column G of sheet2, and get the row of that match. I'm new to GAPS and wrote a script to find matches all on one sheet. How do I modify it to look at sheet2?
function rowOfMatch(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues();
var inputName = sheet.getRange("A1").getValue();
for(var i = 0; i<data.length;i++){
if(data[i][6] == inputName){ //[6] to search column G
Logger.log((i+1))
return i+1;
}
}
}
function runOne(){
var ss=SpreadsheetApp.getActive();
var sh1=ss.getSheetByName('Sheet1');
var sh2=ss.getSheetByName('Sheet2');
var rg1=sh1.getRange(1,1,sh1.getLastRow(),1);
var rg2=sh2.getRange(1,7,sh2.getLastRow(),1);
var v1=rg1.getValues();
var v2=rg2.getValues().map(function(r){return r[0];});
for(var i=0;i<v1.length;i++) {
var row=v2.indexOf(v1[i][0])+1;
if(row>0) {
sh1.getRange(i+1,2).setValue(row);
}else{
sh1.getRange(i+1,2).setValue('Not Found');
}
}
}
Animation:

Google App Script, Moving row onto another sheet if date has passed

I've been working on this code and doing some research the last couple of weeks and not having much luck. I'm looking to setup a trigger that will scan Col C of dates and if the date has passed move that specific row to another sheet.
Below is where I'm currently at.
Note that I have today's date stored in cell 'O2' of the 'Contract Language' sheet.
I've gotten this code to work with text, but not with dates.
Any advise is greatly appreciated!
function MovetoPastShows2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Confirmed Deals'); //source sheet
var testrange = sheet.getRange('C:C');
var testvalue = testrange.getValues();
var todaysheet = ss.getSheetByName('Contract Language');
var todaycol = todaysheet.getRange('O2');
var today = todaycol.getValues().valueOf();
var csh = ss.getSheetByName('Confirmed (Past)'); //destination sheet
var data = [];
var j =[];
for (i=0; i<testvalue.length;i++) {
if ( testvalue[i] = today) {
data.push.apply(data,sheet.getRange(i+1,1,1,187).getValues());
j.push(i);
}
}
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
for (i=0;i<j.length;i++){
var k = j[i]+1;
sheet.deleteRow(k);
if (!(i == j.length-1)) {
j[i+1] = j[i+1]-i-1;
}
}
};
Move Past Rows to Destination Sheet
function MovetoPastShows2() {
var ss = SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Confirmed Deals');
var rg=sh.getDataRange();
var vA=rg.getValues();
var today=new Date(ss.getSheetByName('Contract Language').getRange('O2').getValue()).valueOf();
var dsh=ss.getSheetByName('Confirmed (Past)');
var d=0;
for(var i=0;i<vA.length;i++) {
if (new Date(vA[i][2]).valueOf()<=today) {
dsh.appendRow(vA[i])
sh.deleteRow(i+1-d);
d++;
}
}
}

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

Copy Row if Cell Contains X to Different Sheet then delete row.

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